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            /**
024             * @see java.io.OutputStream#write(int)
025             */
026            public void write(int b) throws IOException {
027                    os.write(b);
028            }
029    
030            /**
031             *
032             * @see java.io.OutputStream#close()
033             */
034            public void close() throws IOException {
035                    try {
036                            os.close();
037                    }
038                    finally {
039                            res.getResourceProvider().unlock(res);
040                    }
041            }
042    
043            /**
044             *
045             * @see java.io.OutputStream#flush()
046             */
047            public void flush() throws IOException {
048                    os.flush();
049            }
050    
051            /**
052             *
053             * @see java.io.OutputStream#write(byte[], int, int)
054             */
055            public void write(byte[] b, int off, int len) throws IOException {
056                    os.write(b, off, len);
057            }
058    
059            /**
060             *
061             * @see java.io.OutputStream#write(byte[])
062             */
063            public void write(byte[] b) throws IOException {
064                    os.write(b);
065            }
066    
067            /**
068             * @return the os
069             */
070            public OutputStream getOutputStream() {
071                    return os;
072            }
073    
074            /**
075             * @return the res
076             */
077            public Resource getResource() {
078                    return res;
079            }
080    
081    }