rlm@3: /* rlm@3: * @(#)ImageOutputStreamAdapter.java 1.1 2011-01-07 rlm@3: * rlm@3: * Copyright © 2010 Werner Randelshofer, Immensee, Switzerland. rlm@3: * All rights reserved. rlm@3: * rlm@3: * You may not use, copy or modify this file, except in compliance with the rlm@3: * license agreement you entered into with Werner Randelshofer. rlm@3: * For details see accompanying license terms. rlm@3: */ rlm@3: package com.aurellem.capture; rlm@3: rlm@3: import java.io.FilterOutputStream; rlm@3: import java.io.IOException; rlm@3: import java.io.OutputStream; rlm@3: import javax.imageio.stream.ImageOutputStream; rlm@3: rlm@3: /** rlm@3: * Adapts an {@code ImageOutputStream} for classes requiring an rlm@3: * {@code OutputStream}. rlm@3: * rlm@3: * @author Werner Randelshofer rlm@3: * @version 1.1 2011-01-07 Fixes performance. rlm@3: *
1.0 2010-12-26 Created. rlm@3: */ rlm@3: public class ImageOutputStreamAdapter extends OutputStream { rlm@3: rlm@3: /** rlm@3: * The underlying output stream to be filtered. rlm@3: */ rlm@3: protected ImageOutputStream out; rlm@3: rlm@3: /** rlm@3: * Creates an output stream filter built on top of the specified rlm@3: * underlying output stream. rlm@3: * rlm@3: * @param out the underlying output stream to be assigned to rlm@3: * the field this.out for later use, or rlm@3: * null if this instance is to be rlm@3: * created without an underlying stream. rlm@3: */ rlm@3: public ImageOutputStreamAdapter(ImageOutputStream out) { rlm@3: this.out = out; rlm@3: } rlm@3: rlm@3: /** rlm@3: * Writes the specified byte to this output stream. rlm@3: *

rlm@3: * The write method of FilterOutputStream rlm@3: * calls the write method of its underlying output stream, rlm@3: * that is, it performs out.write(b). rlm@3: *

rlm@3: * Implements the abstract write method of OutputStream. rlm@3: * rlm@3: * @param b the byte. rlm@3: * @exception IOException if an I/O error occurs. rlm@3: */ rlm@3: @Override rlm@3: public void write(int b) throws IOException { rlm@3: out.write(b); rlm@3: } rlm@3: rlm@3: /** rlm@3: * Writes b.length bytes to this output stream. rlm@3: *

rlm@3: * The write method of FilterOutputStream rlm@3: * calls its write method of three arguments with the rlm@3: * arguments b, 0, and rlm@3: * b.length. rlm@3: *

rlm@3: * Note that this method does not call the one-argument rlm@3: * write method of its underlying stream with the single rlm@3: * argument b. rlm@3: * rlm@3: * @param b the data to be written. rlm@3: * @exception IOException if an I/O error occurs. rlm@3: * @see java.io.FilterOutputStream#write(byte[], int, int) rlm@3: */ rlm@3: @Override rlm@3: public void write(byte b[]) throws IOException { rlm@3: write(b, 0, b.length); rlm@3: } rlm@3: rlm@3: /** rlm@3: * Writes len bytes from the specified rlm@3: * byte array starting at offset off to rlm@3: * this output stream. rlm@3: *

rlm@3: * The write method of FilterOutputStream rlm@3: * calls the write method of one argument on each rlm@3: * byte to output. rlm@3: *

rlm@3: * Note that this method does not call the write method rlm@3: * of its underlying input stream with the same arguments. Subclasses rlm@3: * of FilterOutputStream should provide a more efficient rlm@3: * implementation of this method. rlm@3: * rlm@3: * @param b the data. rlm@3: * @param off the start offset in the data. rlm@3: * @param len the number of bytes to write. rlm@3: * @exception IOException if an I/O error occurs. rlm@3: * @see java.io.FilterOutputStream#write(int) rlm@3: */ rlm@3: @Override rlm@3: public void write(byte b[], int off, int len) throws IOException { rlm@3: out.write(b,off,len); rlm@3: } rlm@3: rlm@3: /** rlm@3: * Flushes this output stream and forces any buffered output bytes rlm@3: * to be written out to the stream. rlm@3: *

rlm@3: * The flush method of FilterOutputStream rlm@3: * calls the flush method of its underlying output stream. rlm@3: * rlm@3: * @exception IOException if an I/O error occurs. rlm@3: * @see java.io.FilterOutputStream#out rlm@3: */ rlm@3: @Override rlm@3: public void flush() throws IOException { rlm@3: out.flush(); rlm@3: } rlm@3: rlm@3: /** rlm@3: * Closes this output stream and releases any system resources rlm@3: * associated with the stream. rlm@3: *

rlm@3: * The close method of FilterOutputStream rlm@3: * calls its flush method, and then calls the rlm@3: * close method of its underlying output stream. rlm@3: * rlm@3: * @exception IOException if an I/O error occurs. rlm@3: * @see java.io.FilterOutputStream#flush() rlm@3: * @see java.io.FilterOutputStream#out rlm@3: */ rlm@3: @Override rlm@3: public void close() throws IOException { rlm@3: try { rlm@3: flush(); rlm@3: } finally { rlm@3: out.close(); rlm@3: } rlm@3: } rlm@3: }