001    package railo.commons.io.res.util;
002    
003    import java.io.IOException;
004    import java.io.OutputStream;
005    
006    import railo.commons.io.res.Resource;
007    
008    public class ResourceOutputStream extends OutputStream {
009    
010            private final Resource res;
011            private final OutputStream os;
012    
013            /**
014             * Constructor of the class
015             * @param res
016             * @param os
017             */
018            public ResourceOutputStream(Resource res, OutputStream os) {
019                    this.res=res;
020                    this.os=os;
021            }
022            
023            @Override
024            public void write(int b) throws IOException {
025                    os.write(b);
026            }
027    
028            @Override
029            public void close() throws IOException {
030                    try {
031                            os.close();
032                    }
033                    finally {
034                            res.getResourceProvider().unlock(res);
035                    }
036            }
037    
038            @Override
039            public void flush() throws IOException {
040                    os.flush();
041            }
042    
043            @Override
044            public void write(byte[] b, int off, int len) throws IOException {
045                    os.write(b, off, len);
046            }
047    
048            @Override
049            public void write(byte[] b) throws IOException {
050                    os.write(b);
051            }
052    
053            /**
054             * @return the os
055             */
056            public OutputStream getOutputStream() {
057                    return os;
058            }
059    
060            /**
061             * @return the res
062             */
063            public Resource getResource() {
064                    return res;
065            }
066    
067    }