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