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.Writer;
023
024/**
025 * Close the Writer automaticlly when object will destroyed by the garbage
026 */
027public final class AutoCloseWriter extends Writer {
028        
029        private final Writer writer;
030
031        /**
032         * constructor of the class
033         * @param writer
034         */
035        public AutoCloseWriter(Writer writer) {
036                this.writer=writer;
037        }
038
039        @Override
040        public void close() throws IOException {
041                writer.close();
042        }
043
044        @Override
045        public void flush() throws IOException {
046                writer.flush();
047        }
048
049        @Override
050        public void write(char[] cbuf, int off, int len) throws IOException {
051                writer.write(cbuf,off,len);
052        }
053
054        @Override
055        public void write(char[] cbuf) throws IOException {
056                writer.write(cbuf);
057        }
058
059        @Override
060        public void write(int c) throws IOException {
061                writer.write(c);
062        }
063
064        @Override
065        public void write(String str, int off, int len) throws IOException {
066                writer.write(str,off,len);
067        }
068
069        @Override
070        public void write(String str) throws IOException {
071                writer.write(str);
072        }
073        
074        @Override
075        public void finalize() throws Throwable {
076                super.finalize();
077                try {
078                        writer.close();
079                }
080                catch(Exception e) {}
081        }
082        
083
084}