001    package railo.commons.io.auto;
002    
003    import java.io.IOException;
004    import java.io.OutputStream;
005    
006    /**
007     * Close the Stream automaticlly when object will destroyed by the garbage
008     */
009    public final class AutoCloseOutputStream extends OutputStream {
010            
011            private final OutputStream os;
012    
013            /**
014             * constructor of the class
015             * @param os
016             */
017            public AutoCloseOutputStream(OutputStream os) {
018                    this.os=os;
019            } 
020            
021            @Override
022            public void write(int b) throws IOException {
023                    os.write(b);
024            }
025    
026            @Override
027            public void close() throws IOException {
028                    os.close();
029            }
030    
031            @Override
032            public void flush() throws IOException {
033                    os.flush();
034            }
035    
036            @Override
037            public void write(byte[] b, int off, int len) throws IOException {
038                    os.write(b, off, len);
039            }
040    
041            @Override
042            public void write(byte[] b) throws IOException {
043                    os.write(b);
044            }
045            
046            @Override
047            public void finalize() throws Throwable {
048                    super.finalize();
049                    try {
050                            os.close();
051                    }
052                    catch(Exception e) {}
053            }
054    }