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.datasource;
020
021import java.io.IOException;
022import java.io.OutputStream;
023import java.sql.SQLException;
024
025import lucee.runtime.exp.AlwaysThrow;
026
027public class DatasourceResourceOutputStream extends OutputStream {
028
029        private final DataWriter dw;
030        private final OutputStream os;
031
032        /**
033         * Constructor of the class
034         * @param res
035         * @param os
036         */
037        public DatasourceResourceOutputStream(DataWriter dw, OutputStream os) {
038                this.dw=dw;
039                this.os=os;
040        }
041        
042        @Override
043        public void write(int b) throws IOException {
044                os.write(b);
045        }
046
047        @Override
048        public void close() throws IOException {
049                os.close();
050                try {
051                        dw.join();
052                } catch (InterruptedException e) {
053                        throw new AlwaysThrow(e.getMessage());
054                }
055
056         
057                SQLException ioe=dw.getException();
058                if(ioe!=null) {
059                        throw new AlwaysThrow(ioe.getMessage());
060                }
061        }
062
063        @Override
064        public void flush() throws IOException {
065                os.flush();
066        }
067
068        @Override
069        public void write(byte[] b, int off, int len) throws IOException {
070                os.write(b, off, len);
071        }
072
073        @Override
074        public void write(byte[] b) throws IOException {
075                os.write(b);
076        }
077
078        /**
079         * @return the os
080         */
081        public OutputStream getOutputStream() {
082                return os;
083        }
084
085}