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