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.res.type.cfml;
020
021import java.io.ByteArrayOutputStream;
022import java.io.IOException;
023import java.io.OutputStream;
024
025import lucee.commons.lang.ExceptionUtil;
026
027public final class CFMLResourceOutputStream extends OutputStream {
028        private ByteArrayOutputStream baos;
029        private CFMLResource res;
030        
031        public CFMLResourceOutputStream(CFMLResource res) {
032                this.res=res;
033                baos = new ByteArrayOutputStream();
034        }
035        
036        @Override
037        public void close() throws IOException {
038                baos.close();
039                
040                try {
041                        res.setBinary(baos.toByteArray());
042                } 
043                catch (Throwable t) {
044                ExceptionUtil.rethrowIfNecessary(t);
045                        throw ExceptionUtil.toIOException(t);
046                }
047                finally {
048                        res.getResourceProvider().unlock(res);
049                }
050        }
051
052        @Override
053        public void flush() throws IOException {
054                baos.flush();
055        }
056
057        @Override
058        public void write(byte[] b, int off, int len) throws IOException {
059                baos.write(b, off, len);
060        }
061
062        @Override
063        public void write(byte[] b) throws IOException {
064                baos.write(b);
065        }
066
067        @Override
068        public void write(int b) throws IOException {
069                baos.write(b);
070        }
071}