001    package railo.commons.io.auto;
002    
003    import java.io.IOException;
004    import java.io.Reader;
005    
006    /**
007     * Close the Reader automaticlly when object will destroyed by the garbage
008     */
009    public final class AutoCloseReader extends Reader {
010            
011            private final Reader reader;
012    
013            /**
014             * constructor of the class
015             * @param reader
016             */
017            public AutoCloseReader(Reader reader) {
018                    this.reader=reader;
019            }
020    
021            @Override
022            public void close() throws IOException {
023                    reader.close();
024            }
025    
026            @Override
027            public void mark(int readAheadLimit) throws IOException {
028                    reader.mark(readAheadLimit);
029            }
030    
031            @Override
032            public boolean markSupported() {
033                    return reader.markSupported();
034            }
035    
036            @Override
037            public int read() throws IOException {
038                    return reader.read();
039            }
040    
041            @Override
042            public int read(char[] cbuf, int off, int len) throws IOException {
043                    return reader.read(cbuf,off,len);
044            }
045    
046            @Override
047            public int read(char[] cbuf) throws IOException {
048                    return reader.read(cbuf);
049            }
050    
051            @Override
052            public boolean ready() throws IOException {
053                    return reader.ready();
054            }
055    
056            @Override
057            public void reset() throws IOException {
058                    reader.reset();
059            }
060    
061            @Override
062            public long skip(long n) throws IOException {
063                    return reader.skip(n);
064            }
065            
066            @Override
067            public void finalize() throws Throwable {
068                    super.finalize();
069                    try {
070                            reader.close();
071                    }
072                    catch(Exception e) {}
073            }
074    
075    }