001    package railo.commons.io.res.type.datasource;
002    
003    import java.io.IOException;
004    import java.io.OutputStream;
005    import java.sql.SQLException;
006    
007    import railo.runtime.exp.AlwaysThrow;
008    
009    public class DatasourceResourceOutputStream extends OutputStream {
010    
011            private final DataWriter dw;
012            private final OutputStream os;
013    
014            /**
015             * Constructor of the class
016             * @param res
017             * @param os
018             */
019            public DatasourceResourceOutputStream(DataWriter dw, OutputStream os) {
020                    this.dw=dw;
021                    this.os=os;
022            }
023            
024            @Override
025            public void write(int b) throws IOException {
026                    os.write(b);
027            }
028    
029            @Override
030            public void close() throws IOException {
031                    os.close();
032                    try {
033                            dw.join();
034                    } catch (InterruptedException e) {
035                            throw new AlwaysThrow(e.getMessage());
036                    }
037    
038             
039                    SQLException ioe=dw.getException();
040                    if(ioe!=null) {
041                            throw new AlwaysThrow(ioe.getMessage());
042                    }
043            }
044    
045            @Override
046            public void flush() throws IOException {
047                    os.flush();
048            }
049    
050            @Override
051            public void write(byte[] b, int off, int len) throws IOException {
052                    os.write(b, off, len);
053            }
054    
055            @Override
056            public void write(byte[] b) throws IOException {
057                    os.write(b);
058            }
059    
060            /**
061             * @return the os
062             */
063            public OutputStream getOutputStream() {
064                    return os;
065            }
066    
067    }