001/** 002 * 003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved. 004 * 005 * This library is free software; you can redistribute it and/or 006 * modify it under the terms of the GNU Lesser General Public 007 * License as published by the Free Software Foundation; either 008 * version 2.1 of the License, or (at your option) any later version. 009 * 010 * This library is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 * Lesser General Public License for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public 016 * License along with this library. If not, see <http://www.gnu.org/licenses/>. 017 * 018 **/ 019package lucee.commons.io.auto; 020 021import java.io.IOException; 022import java.io.OutputStream; 023 024/** 025 * Close the Stream automaticlly when object will destroyed by the garbage 026 */ 027public final class AutoCloseOutputStream extends OutputStream { 028 029 private final OutputStream os; 030 031 /** 032 * constructor of the class 033 * @param os 034 */ 035 public AutoCloseOutputStream(OutputStream os) { 036 this.os=os; 037 } 038 039 @Override 040 public void write(int b) throws IOException { 041 os.write(b); 042 } 043 044 @Override 045 public void close() throws IOException { 046 os.close(); 047 } 048 049 @Override 050 public void flush() throws IOException { 051 os.flush(); 052 } 053 054 @Override 055 public void write(byte[] b, int off, int len) throws IOException { 056 os.write(b, off, len); 057 } 058 059 @Override 060 public void write(byte[] b) throws IOException { 061 os.write(b); 062 } 063 064 @Override 065 public void finalize() throws Throwable { 066 super.finalize(); 067 try { 068 os.close(); 069 } 070 catch(Exception e) {} 071 } 072}