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.Reader;
023
024/**
025 * Close the Reader automaticlly when object will destroyed by the garbage
026 */
027public final class AutoCloseReader extends Reader {
028        
029        private final Reader reader;
030
031        /**
032         * constructor of the class
033         * @param reader
034         */
035        public AutoCloseReader(Reader reader) {
036                this.reader=reader;
037        }
038
039        @Override
040        public void close() throws IOException {
041                reader.close();
042        }
043
044        @Override
045        public void mark(int readAheadLimit) throws IOException {
046                reader.mark(readAheadLimit);
047        }
048
049        @Override
050        public boolean markSupported() {
051                return reader.markSupported();
052        }
053
054        @Override
055        public int read() throws IOException {
056                return reader.read();
057        }
058
059        @Override
060        public int read(char[] cbuf, int off, int len) throws IOException {
061                return reader.read(cbuf,off,len);
062        }
063
064        @Override
065        public int read(char[] cbuf) throws IOException {
066                return reader.read(cbuf);
067        }
068
069        @Override
070        public boolean ready() throws IOException {
071                return reader.ready();
072        }
073
074        @Override
075        public void reset() throws IOException {
076                reader.reset();
077        }
078
079        @Override
080        public long skip(long n) throws IOException {
081                return reader.skip(n);
082        }
083        
084        @Override
085        public void finalize() throws Throwable {
086                super.finalize();
087                try {
088                        reader.close();
089                }
090                catch(Exception e) {}
091        }
092
093}