diff src/ca/randelshofer/ImageOutputStreamAdapter.java @ 10:4c5fc53778c1

moved randelshofer stuff to rightfull place, enabled XuggleVideoRecorder
author Robert McIntyre <rlm@mit.edu>
date Wed, 26 Oct 2011 09:38:27 -0700
parents
children 302d5e9ad120
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/ca/randelshofer/ImageOutputStreamAdapter.java	Wed Oct 26 09:38:27 2011 -0700
     1.3 @@ -0,0 +1,144 @@
     1.4 +/*
     1.5 + * @(#)ImageOutputStreamAdapter.java  1.1  2011-01-07
     1.6 + *
     1.7 + * Copyright © 2010 Werner Randelshofer, Immensee, Switzerland.
     1.8 + * All rights reserved.
     1.9 + *
    1.10 + * You may not use, copy or modify this file, except in compliance with the
    1.11 + * license agreement you entered into with Werner Randelshofer.
    1.12 + * For details see accompanying license terms.
    1.13 + */
    1.14 +package ca.randelshofer;
    1.15 +
    1.16 +import java.io.IOException;
    1.17 +import java.io.OutputStream;
    1.18 +
    1.19 +import javax.imageio.stream.ImageOutputStream;
    1.20 +
    1.21 +/**
    1.22 + * Adapts an {@code ImageOutputStream} for classes requiring an
    1.23 + * {@code OutputStream}.
    1.24 + *
    1.25 + * @author Werner Randelshofer
    1.26 + * @version 1.1 2011-01-07 Fixes performance.
    1.27 + * <br>1.0 2010-12-26 Created.
    1.28 + */
    1.29 +public class ImageOutputStreamAdapter extends OutputStream {
    1.30 +
    1.31 +    /**
    1.32 +     * The underlying output stream to be filtered.
    1.33 +     */
    1.34 +    protected ImageOutputStream out;
    1.35 +
    1.36 +    /**
    1.37 +     * Creates an output stream filter built on top of the specified
    1.38 +     * underlying output stream.
    1.39 +     *
    1.40 +     * @param   out   the underlying output stream to be assigned to
    1.41 +     *                the field <tt>this.out</tt> for later use, or
    1.42 +     *                <code>null</code> if this instance is to be
    1.43 +     *                created without an underlying stream.
    1.44 +     */
    1.45 +    public ImageOutputStreamAdapter(ImageOutputStream out) {
    1.46 +        this.out = out;
    1.47 +    }
    1.48 +
    1.49 +    /**
    1.50 +     * Writes the specified <code>byte</code> to this output stream.
    1.51 +     * <p>
    1.52 +     * The <code>write</code> method of <code>FilterOutputStream</code>
    1.53 +     * calls the <code>write</code> method of its underlying output stream,
    1.54 +     * that is, it performs <tt>out.write(b)</tt>.
    1.55 +     * <p>
    1.56 +     * Implements the abstract <tt>write</tt> method of <tt>OutputStream</tt>.
    1.57 +     *
    1.58 +     * @param      b   the <code>byte</code>.
    1.59 +     * @exception  IOException  if an I/O error occurs.
    1.60 +     */
    1.61 +    @Override
    1.62 +    public void write(int b) throws IOException {
    1.63 +        out.write(b);
    1.64 +    }
    1.65 +
    1.66 +    /**
    1.67 +     * Writes <code>b.length</code> bytes to this output stream.
    1.68 +     * <p>
    1.69 +     * The <code>write</code> method of <code>FilterOutputStream</code>
    1.70 +     * calls its <code>write</code> method of three arguments with the
    1.71 +     * arguments <code>b</code>, <code>0</code>, and
    1.72 +     * <code>b.length</code>.
    1.73 +     * <p>
    1.74 +     * Note that this method does not call the one-argument
    1.75 +     * <code>write</code> method of its underlying stream with the single
    1.76 +     * argument <code>b</code>.
    1.77 +     *
    1.78 +     * @param      b   the data to be written.
    1.79 +     * @exception  IOException  if an I/O error occurs.
    1.80 +     * @see        java.io.FilterOutputStream#write(byte[], int, int)
    1.81 +     */
    1.82 +    @Override
    1.83 +    public void write(byte b[]) throws IOException {
    1.84 +        write(b, 0, b.length);
    1.85 +    }
    1.86 +
    1.87 +    /**
    1.88 +     * Writes <code>len</code> bytes from the specified
    1.89 +     * <code>byte</code> array starting at offset <code>off</code> to
    1.90 +     * this output stream.
    1.91 +     * <p>
    1.92 +     * The <code>write</code> method of <code>FilterOutputStream</code>
    1.93 +     * calls the <code>write</code> method of one argument on each
    1.94 +     * <code>byte</code> to output.
    1.95 +     * <p>
    1.96 +     * Note that this method does not call the <code>write</code> method
    1.97 +     * of its underlying input stream with the same arguments. Subclasses
    1.98 +     * of <code>FilterOutputStream</code> should provide a more efficient
    1.99 +     * implementation of this method.
   1.100 +     *
   1.101 +     * @param      b     the data.
   1.102 +     * @param      off   the start offset in the data.
   1.103 +     * @param      len   the number of bytes to write.
   1.104 +     * @exception  IOException  if an I/O error occurs.
   1.105 +     * @see        java.io.FilterOutputStream#write(int)
   1.106 +     */
   1.107 +    @Override
   1.108 +    public void write(byte b[], int off, int len) throws IOException {
   1.109 +        out.write(b,off,len);
   1.110 +    }
   1.111 +
   1.112 +    /**
   1.113 +     * Flushes this output stream and forces any buffered output bytes
   1.114 +     * to be written out to the stream.
   1.115 +     * <p>
   1.116 +     * The <code>flush</code> method of <code>FilterOutputStream</code>
   1.117 +     * calls the <code>flush</code> method of its underlying output stream.
   1.118 +     *
   1.119 +     * @exception  IOException  if an I/O error occurs.
   1.120 +     * @see        java.io.FilterOutputStream#out
   1.121 +     */
   1.122 +    @Override
   1.123 +    public void flush() throws IOException {
   1.124 +        out.flush();
   1.125 +    }
   1.126 +
   1.127 +    /**
   1.128 +     * Closes this output stream and releases any system resources
   1.129 +     * associated with the stream.
   1.130 +     * <p>
   1.131 +     * The <code>close</code> method of <code>FilterOutputStream</code>
   1.132 +     * calls its <code>flush</code> method, and then calls the
   1.133 +     * <code>close</code> method of its underlying output stream.
   1.134 +     *
   1.135 +     * @exception  IOException  if an I/O error occurs.
   1.136 +     * @see        java.io.FilterOutputStream#flush()
   1.137 +     * @see        java.io.FilterOutputStream#out
   1.138 +     */
   1.139 +    @Override
   1.140 +    public void close() throws IOException {
   1.141 +        try {
   1.142 +            flush();
   1.143 +        } finally {
   1.144 +            out.close();
   1.145 +        }
   1.146 +    }
   1.147 +}