001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.runtime.img;
020import java.io.IOException;
021import java.io.OutputStream;
022
023import javax.imageio.stream.ImageOutputStreamImpl;
024
025import lucee.commons.io.res.Resource;
026
027public class ResourceImageOutputStream extends ImageOutputStreamImpl {
028
029    private Resource res;
030        private OutputStream os;
031
032    public ResourceImageOutputStream(Resource res) throws IOException {
033        this.res=res;
034        os=res.getOutputStream();
035    }
036    public ResourceImageOutputStream(OutputStream os) {
037        this.os=os;
038    }
039
040    public int read() throws IOException {
041        throw new IOException("not supported");
042    }
043
044    public int read(byte[] b, int off, int len) throws IOException {
045        throw new IOException("not supported");
046    }
047
048    public void write(int b) throws IOException {
049        os.write(b);
050    }
051
052    public void write(byte[] b, int off, int len) throws IOException {
053        os.write(b,off,len);
054    }
055
056    public long length() {
057        if(res==null) throw new RuntimeException("not supported");
058        return res.length();
059    }
060
061    /**
062     * Sets the current stream position and resets the bit offset to
063     * 0.  It is legal to seeking past the end of the file; an
064     * <code>EOFException</code> will be thrown only if a read is
065     * performed.  The file length will not be increased until a write
066     * is performed.
067     *
068     * @exception IndexOutOfBoundsException if <code>pos</code> is smaller
069     * than the flushed position.
070     * @exception IOException if any other I/O error occurs.
071     */
072    public void seek(long pos) throws IOException {
073        throw new IOException("not supported");
074    }
075
076    @Override
077    public void close() throws IOException {
078        try {
079            super.close();
080        }
081        finally {
082            os.close();
083        }
084    }
085}