view src/ca/randelshofer/ImageOutputStreamAdapter.java @ 15:be5ac56826be

created part of the advanced documentation
author Robert McIntyre <rlm@mit.edu>
date Fri, 28 Oct 2011 06:52:46 -0700
parents 4c5fc53778c1
children 302d5e9ad120
line wrap: on
line source
1 /*
2 * @(#)ImageOutputStreamAdapter.java 1.1 2011-01-07
3 *
4 * Copyright © 2010 Werner Randelshofer, Immensee, Switzerland.
5 * All rights reserved.
6 *
7 * You may not use, copy or modify this file, except in compliance with the
8 * license agreement you entered into with Werner Randelshofer.
9 * For details see accompanying license terms.
10 */
11 package ca.randelshofer;
13 import java.io.IOException;
14 import java.io.OutputStream;
16 import javax.imageio.stream.ImageOutputStream;
18 /**
19 * Adapts an {@code ImageOutputStream} for classes requiring an
20 * {@code OutputStream}.
21 *
22 * @author Werner Randelshofer
23 * @version 1.1 2011-01-07 Fixes performance.
24 * <br>1.0 2010-12-26 Created.
25 */
26 public class ImageOutputStreamAdapter extends OutputStream {
28 /**
29 * The underlying output stream to be filtered.
30 */
31 protected ImageOutputStream out;
33 /**
34 * Creates an output stream filter built on top of the specified
35 * underlying output stream.
36 *
37 * @param out the underlying output stream to be assigned to
38 * the field <tt>this.out</tt> for later use, or
39 * <code>null</code> if this instance is to be
40 * created without an underlying stream.
41 */
42 public ImageOutputStreamAdapter(ImageOutputStream out) {
43 this.out = out;
44 }
46 /**
47 * Writes the specified <code>byte</code> to this output stream.
48 * <p>
49 * The <code>write</code> method of <code>FilterOutputStream</code>
50 * calls the <code>write</code> method of its underlying output stream,
51 * that is, it performs <tt>out.write(b)</tt>.
52 * <p>
53 * Implements the abstract <tt>write</tt> method of <tt>OutputStream</tt>.
54 *
55 * @param b the <code>byte</code>.
56 * @exception IOException if an I/O error occurs.
57 */
58 @Override
59 public void write(int b) throws IOException {
60 out.write(b);
61 }
63 /**
64 * Writes <code>b.length</code> bytes to this output stream.
65 * <p>
66 * The <code>write</code> method of <code>FilterOutputStream</code>
67 * calls its <code>write</code> method of three arguments with the
68 * arguments <code>b</code>, <code>0</code>, and
69 * <code>b.length</code>.
70 * <p>
71 * Note that this method does not call the one-argument
72 * <code>write</code> method of its underlying stream with the single
73 * argument <code>b</code>.
74 *
75 * @param b the data to be written.
76 * @exception IOException if an I/O error occurs.
77 * @see java.io.FilterOutputStream#write(byte[], int, int)
78 */
79 @Override
80 public void write(byte b[]) throws IOException {
81 write(b, 0, b.length);
82 }
84 /**
85 * Writes <code>len</code> bytes from the specified
86 * <code>byte</code> array starting at offset <code>off</code> to
87 * this output stream.
88 * <p>
89 * The <code>write</code> method of <code>FilterOutputStream</code>
90 * calls the <code>write</code> method of one argument on each
91 * <code>byte</code> to output.
92 * <p>
93 * Note that this method does not call the <code>write</code> method
94 * of its underlying input stream with the same arguments. Subclasses
95 * of <code>FilterOutputStream</code> should provide a more efficient
96 * implementation of this method.
97 *
98 * @param b the data.
99 * @param off the start offset in the data.
100 * @param len the number of bytes to write.
101 * @exception IOException if an I/O error occurs.
102 * @see java.io.FilterOutputStream#write(int)
103 */
104 @Override
105 public void write(byte b[], int off, int len) throws IOException {
106 out.write(b,off,len);
107 }
109 /**
110 * Flushes this output stream and forces any buffered output bytes
111 * to be written out to the stream.
112 * <p>
113 * The <code>flush</code> method of <code>FilterOutputStream</code>
114 * calls the <code>flush</code> method of its underlying output stream.
115 *
116 * @exception IOException if an I/O error occurs.
117 * @see java.io.FilterOutputStream#out
118 */
119 @Override
120 public void flush() throws IOException {
121 out.flush();
122 }
124 /**
125 * Closes this output stream and releases any system resources
126 * associated with the stream.
127 * <p>
128 * The <code>close</code> method of <code>FilterOutputStream</code>
129 * calls its <code>flush</code> method, and then calls the
130 * <code>close</code> method of its underlying output stream.
131 *
132 * @exception IOException if an I/O error occurs.
133 * @see java.io.FilterOutputStream#flush()
134 * @see java.io.FilterOutputStream#out
135 */
136 @Override
137 public void close() throws IOException {
138 try {
139 flush();
140 } finally {
141 out.close();
142 }
143 }
144 }