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