001    package railo.runtime.img;
002    import java.io.IOException;
003    import java.io.OutputStream;
004    
005    import javax.imageio.stream.ImageOutputStreamImpl;
006    
007    import railo.commons.io.res.Resource;
008    
009    public class ResourceImageOutputStream extends ImageOutputStreamImpl {
010    
011        private Resource res;
012            private OutputStream os;
013    
014        public ResourceImageOutputStream(Resource res) throws IOException {
015            this.res=res;
016            os=res.getOutputStream();
017        }
018        public ResourceImageOutputStream(OutputStream os) {
019            this.os=os;
020        }
021    
022        public int read() throws IOException {
023            throw new IOException("not supported");
024        }
025    
026        public int read(byte[] b, int off, int len) throws IOException {
027            throw new IOException("not supported");
028        }
029    
030        public void write(int b) throws IOException {
031            os.write(b);
032        }
033    
034        public void write(byte[] b, int off, int len) throws IOException {
035            os.write(b,off,len);
036        }
037    
038        public long length() {
039            if(res==null) throw new RuntimeException("not supported");
040            return res.length();
041        }
042    
043        /**
044         * Sets the current stream position and resets the bit offset to
045         * 0.  It is legal to seeking past the end of the file; an
046         * <code>EOFException</code> will be thrown only if a read is
047         * performed.  The file length will not be increased until a write
048         * is performed.
049         *
050         * @exception IndexOutOfBoundsException if <code>pos</code> is smaller
051         * than the flushed position.
052         * @exception IOException if any other I/O error occurs.
053         */
054        public void seek(long pos) throws IOException {
055            throw new IOException("not supported");
056        }
057    
058        @Override
059        public void close() throws IOException {
060            try {
061                super.close();
062            }
063            finally {
064                os.close();
065            }
066        }
067    }