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            @Override
022            public void close() throws IOException {
023                    writer.close();
024            }
025    
026            @Override
027            public void flush() throws IOException {
028                    writer.flush();
029            }
030    
031            @Override
032            public void write(char[] cbuf, int off, int len) throws IOException {
033                    writer.write(cbuf,off,len);
034            }
035    
036            @Override
037            public void write(char[] cbuf) throws IOException {
038                    writer.write(cbuf);
039            }
040    
041            @Override
042            public void write(int c) throws IOException {
043                    writer.write(c);
044            }
045    
046            @Override
047            public void write(String str, int off, int len) throws IOException {
048                    writer.write(str,off,len);
049            }
050    
051            @Override
052            public void write(String str) throws IOException {
053                    writer.write(str);
054            }
055            
056            @Override
057            public void finalize() throws Throwable {
058                    super.finalize();
059                    try {
060                            writer.close();
061                    }
062                    catch(Exception e) {}
063            }
064            
065    
066    }