001    package railo.commons.io.auto;
002    
003    import java.io.IOException;
004    import java.io.Writer;
005    
006    /**
007     * Close the Writer automaticlly when object will destroyed by the garbage
008     */
009    public final class AutoCloseWriter extends Writer {
010            
011            private final Writer writer;
012    
013            /**
014             * constructor of the class
015             * @param writer
016             */
017            public AutoCloseWriter(Writer writer) {
018                    this.writer=writer;
019            }
020    
021            /**
022             * @see java.io.Writer#close()
023             */
024            public void close() throws IOException {
025                    writer.close();
026            }
027    
028            /**
029             * @see java.io.Writer#flush()
030             */
031            public void flush() throws IOException {
032                    writer.flush();
033            }
034    
035            /**
036             * @see java.io.Writer#write(char[], int, int)
037             */
038            public void write(char[] cbuf, int off, int len) throws IOException {
039                    writer.write(cbuf,off,len);
040            }
041    
042            /**
043             * @see java.io.Writer#write(char[])
044             */
045            public void write(char[] cbuf) throws IOException {
046                    writer.write(cbuf);
047            }
048    
049            /**
050             * @see java.io.Writer#write(int)
051             */
052            public void write(int c) throws IOException {
053                    writer.write(c);
054            }
055    
056            /**
057             * @see java.io.Writer#write(java.lang.String, int, int)
058             */
059            public void write(String str, int off, int len) throws IOException {
060                    writer.write(str,off,len);
061            }
062    
063            /**
064             * @see java.io.Writer#write(java.lang.String)
065             */
066            public void write(String str) throws IOException {
067                    writer.write(str);
068            }
069            
070            /**
071             * @throws Throwable 
072             * @see java.lang.Object#finalize()
073             */
074            public void finalize() throws Throwable {
075                    super.finalize();
076                    try {
077                            writer.close();
078                    }
079                    catch(Exception e) {}
080            }
081            
082    
083    }