001    package railo.commons.io.res.type.cfml;
002    
003    import java.io.ByteArrayOutputStream;
004    import java.io.IOException;
005    import java.io.OutputStream;
006    
007    import railo.commons.lang.ExceptionUtil;
008    
009    public final class CFMLResourceOutputStream extends OutputStream {
010            private ByteArrayOutputStream baos;
011            private CFMLResource res;
012            
013            public CFMLResourceOutputStream(CFMLResource res) {
014                    this.res=res;
015                    baos = new ByteArrayOutputStream();
016            }
017            
018            @Override
019            public void close() throws IOException {
020                    baos.close();
021                    
022                    try {
023                            res.setBinary(baos.toByteArray());
024                    } 
025                    catch (Throwable t) {
026                            throw ExceptionUtil.toIOException(t);
027                    }
028                    finally {
029                            res.getResourceProvider().unlock(res);
030                    }
031            }
032    
033            @Override
034            public void flush() throws IOException {
035                    baos.flush();
036            }
037    
038            @Override
039            public void write(byte[] b, int off, int len) throws IOException {
040                    baos.write(b, off, len);
041            }
042    
043            @Override
044            public void write(byte[] b) throws IOException {
045                    baos.write(b);
046            }
047    
048            @Override
049            public void write(int b) throws IOException {
050                    baos.write(b);
051            }
052    }