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