rlm@10: /*
rlm@10: * @(#)ImageOutputStreamAdapter.java 1.1 2011-01-07
rlm@10: *
rlm@10: * Copyright © 2010 Werner Randelshofer, Immensee, Switzerland.
rlm@10: * All rights reserved.
rlm@10: *
rlm@10: * You may not use, copy or modify this file, except in compliance with the
rlm@10: * license agreement you entered into with Werner Randelshofer.
rlm@10: * For details see accompanying license terms.
rlm@10: */
rlm@10: package ca.randelshofer;
rlm@10:
rlm@10: import java.io.IOException;
rlm@10: import java.io.OutputStream;
rlm@10:
rlm@10: import javax.imageio.stream.ImageOutputStream;
rlm@10:
rlm@10: /**
rlm@10: * Adapts an {@code ImageOutputStream} for classes requiring an
rlm@10: * {@code OutputStream}.
rlm@10: *
rlm@10: * @author Werner Randelshofer
rlm@10: * @version 1.1 2011-01-07 Fixes performance.
rlm@10: *
1.0 2010-12-26 Created.
rlm@10: */
rlm@10: public class ImageOutputStreamAdapter extends OutputStream {
rlm@10:
rlm@10: /**
rlm@10: * The underlying output stream to be filtered.
rlm@10: */
rlm@10: protected ImageOutputStream out;
rlm@10:
rlm@10: /**
rlm@10: * Creates an output stream filter built on top of the specified
rlm@10: * underlying output stream.
rlm@10: *
rlm@10: * @param out the underlying output stream to be assigned to
rlm@10: * the field this.out for later use, or
rlm@10: * null
if this instance is to be
rlm@10: * created without an underlying stream.
rlm@10: */
rlm@10: public ImageOutputStreamAdapter(ImageOutputStream out) {
rlm@10: this.out = out;
rlm@10: }
rlm@10:
rlm@10: /**
rlm@10: * Writes the specified byte
to this output stream.
rlm@10: *
rlm@10: * The write
method of FilterOutputStream
rlm@10: * calls the write
method of its underlying output stream,
rlm@10: * that is, it performs out.write(b).
rlm@10: *
rlm@10: * Implements the abstract write method of OutputStream.
rlm@10: *
rlm@10: * @param b the byte
.
rlm@10: * @exception IOException if an I/O error occurs.
rlm@10: */
rlm@10: @Override
rlm@10: public void write(int b) throws IOException {
rlm@10: out.write(b);
rlm@10: }
rlm@10:
rlm@10: /**
rlm@10: * Writes b.length
bytes to this output stream.
rlm@10: *
rlm@10: * The write
method of FilterOutputStream
rlm@10: * calls its write
method of three arguments with the
rlm@10: * arguments b
, 0
, and
rlm@10: * b.length
.
rlm@10: *
rlm@10: * Note that this method does not call the one-argument
rlm@10: * write
method of its underlying stream with the single
rlm@10: * argument b
.
rlm@10: *
rlm@10: * @param b the data to be written.
rlm@10: * @exception IOException if an I/O error occurs.
rlm@10: * @see java.io.FilterOutputStream#write(byte[], int, int)
rlm@10: */
rlm@10: @Override
rlm@10: public void write(byte b[]) throws IOException {
rlm@10: write(b, 0, b.length);
rlm@10: }
rlm@10:
rlm@10: /**
rlm@10: * Writes len
bytes from the specified
rlm@10: * byte
array starting at offset off
to
rlm@10: * this output stream.
rlm@10: *
rlm@10: * The write
method of FilterOutputStream
rlm@10: * calls the write
method of one argument on each
rlm@10: * byte
to output.
rlm@10: *
rlm@10: * Note that this method does not call the write
method
rlm@10: * of its underlying input stream with the same arguments. Subclasses
rlm@10: * of FilterOutputStream
should provide a more efficient
rlm@10: * implementation of this method.
rlm@10: *
rlm@10: * @param b the data.
rlm@10: * @param off the start offset in the data.
rlm@10: * @param len the number of bytes to write.
rlm@10: * @exception IOException if an I/O error occurs.
rlm@10: * @see java.io.FilterOutputStream#write(int)
rlm@10: */
rlm@10: @Override
rlm@10: public void write(byte b[], int off, int len) throws IOException {
rlm@10: out.write(b,off,len);
rlm@10: }
rlm@10:
rlm@10: /**
rlm@10: * Flushes this output stream and forces any buffered output bytes
rlm@10: * to be written out to the stream.
rlm@10: *
rlm@10: * The flush
method of FilterOutputStream
rlm@10: * calls the flush
method of its underlying output stream.
rlm@10: *
rlm@10: * @exception IOException if an I/O error occurs.
rlm@10: * @see java.io.FilterOutputStream#out
rlm@10: */
rlm@10: @Override
rlm@10: public void flush() throws IOException {
rlm@10: out.flush();
rlm@10: }
rlm@10:
rlm@10: /**
rlm@10: * Closes this output stream and releases any system resources
rlm@10: * associated with the stream.
rlm@10: *
rlm@10: * The close
method of FilterOutputStream
rlm@10: * calls its flush
method, and then calls the
rlm@10: * close
method of its underlying output stream.
rlm@10: *
rlm@10: * @exception IOException if an I/O error occurs.
rlm@10: * @see java.io.FilterOutputStream#flush()
rlm@10: * @see java.io.FilterOutputStream#out
rlm@10: */
rlm@10: @Override
rlm@10: public void close() throws IOException {
rlm@10: try {
rlm@10: flush();
rlm@10: } finally {
rlm@10: out.close();
rlm@10: }
rlm@10: }
rlm@10: }