001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.commons.io.auto;
020
021import java.io.IOException;
022import java.io.InputStream;
023
024import lucee.commons.io.IOUtil;
025
026/**
027 * Close the Stream automaticlly when object will destroyed by the garbage
028 */
029public final class AutoCloseInputStream extends InputStream {
030        
031        private final InputStream is;
032
033        /**
034         * constructor of the class
035         * @param is
036         */
037        public AutoCloseInputStream(InputStream is) {
038                this.is=is;
039        }
040        
041        @Override
042        public int read() throws IOException {
043                return is.read();
044        }
045
046        @Override
047        public int available() throws IOException {
048                return is.available();
049        }
050
051        @Override
052        public void close() throws IOException {
053                is.close();
054        }
055
056        @Override
057        public synchronized void mark(int readlimit) {
058                is.mark(readlimit);
059        }
060
061        @Override
062        public boolean markSupported() {
063                return is.markSupported();
064        }
065
066        @Override
067        public int read(byte[] b, int off, int len) throws IOException {
068                return is.read(b, off, len);
069        }
070
071        @Override
072        public int read(byte[] b) throws IOException {
073                return is.read(b);
074        }
075
076        @Override
077        public synchronized void reset() throws IOException {
078                is.reset();
079        }
080
081        @Override
082        public long skip(long n) throws IOException {
083                return is.skip(n);
084        }
085
086        
087        @Override
088        public void finalize() throws Throwable {
089                super.finalize();
090                IOUtil.closeEL(is);
091        }
092}