author | ohair |
Wed, 06 Apr 2011 22:06:11 -0700 | |
changeset 9035 | 1255eb81cc2f |
parent 8540 | ed028ce13912 |
child 10347 | 1c9efe1ec7d3 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
9035
1255eb81cc2f
7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents:
8540
diff
changeset
|
2 |
* Copyright (c) 1994, 2011, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.io; |
|
27 |
||
28 |
/** |
|
29 |
* This class is the superclass of all classes that filter output |
|
30 |
* streams. These streams sit on top of an already existing output |
|
31 |
* stream (the <i>underlying</i> output stream) which it uses as its |
|
32 |
* basic sink of data, but possibly transforming the data along the |
|
33 |
* way or providing additional functionality. |
|
34 |
* <p> |
|
35 |
* The class <code>FilterOutputStream</code> itself simply overrides |
|
36 |
* all methods of <code>OutputStream</code> with versions that pass |
|
37 |
* all requests to the underlying output stream. Subclasses of |
|
38 |
* <code>FilterOutputStream</code> may further override some of these |
|
39 |
* methods as well as provide additional methods and fields. |
|
40 |
* |
|
41 |
* @author Jonathan Payne |
|
42 |
* @since JDK1.0 |
|
43 |
*/ |
|
44 |
public |
|
45 |
class FilterOutputStream extends OutputStream { |
|
46 |
/** |
|
47 |
* The underlying output stream to be filtered. |
|
48 |
*/ |
|
49 |
protected OutputStream out; |
|
50 |
||
51 |
/** |
|
52 |
* Creates an output stream filter built on top of the specified |
|
53 |
* underlying output stream. |
|
54 |
* |
|
55 |
* @param out the underlying output stream to be assigned to |
|
56 |
* the field <tt>this.out</tt> for later use, or |
|
57 |
* <code>null</code> if this instance is to be |
|
58 |
* created without an underlying stream. |
|
59 |
*/ |
|
60 |
public FilterOutputStream(OutputStream out) { |
|
61 |
this.out = out; |
|
62 |
} |
|
63 |
||
64 |
/** |
|
65 |
* Writes the specified <code>byte</code> to this output stream. |
|
66 |
* <p> |
|
67 |
* The <code>write</code> method of <code>FilterOutputStream</code> |
|
68 |
* calls the <code>write</code> method of its underlying output stream, |
|
69 |
* that is, it performs <tt>out.write(b)</tt>. |
|
70 |
* <p> |
|
71 |
* Implements the abstract <tt>write</tt> method of <tt>OutputStream</tt>. |
|
72 |
* |
|
73 |
* @param b the <code>byte</code>. |
|
74 |
* @exception IOException if an I/O error occurs. |
|
75 |
*/ |
|
76 |
public void write(int b) throws IOException { |
|
77 |
out.write(b); |
|
78 |
} |
|
79 |
||
80 |
/** |
|
81 |
* Writes <code>b.length</code> bytes to this output stream. |
|
82 |
* <p> |
|
83 |
* The <code>write</code> method of <code>FilterOutputStream</code> |
|
84 |
* calls its <code>write</code> method of three arguments with the |
|
85 |
* arguments <code>b</code>, <code>0</code>, and |
|
86 |
* <code>b.length</code>. |
|
87 |
* <p> |
|
88 |
* Note that this method does not call the one-argument |
|
89 |
* <code>write</code> method of its underlying stream with the single |
|
90 |
* argument <code>b</code>. |
|
91 |
* |
|
92 |
* @param b the data to be written. |
|
93 |
* @exception IOException if an I/O error occurs. |
|
94 |
* @see java.io.FilterOutputStream#write(byte[], int, int) |
|
95 |
*/ |
|
96 |
public void write(byte b[]) throws IOException { |
|
97 |
write(b, 0, b.length); |
|
98 |
} |
|
99 |
||
100 |
/** |
|
101 |
* Writes <code>len</code> bytes from the specified |
|
102 |
* <code>byte</code> array starting at offset <code>off</code> to |
|
103 |
* this output stream. |
|
104 |
* <p> |
|
105 |
* The <code>write</code> method of <code>FilterOutputStream</code> |
|
106 |
* calls the <code>write</code> method of one argument on each |
|
107 |
* <code>byte</code> to output. |
|
108 |
* <p> |
|
109 |
* Note that this method does not call the <code>write</code> method |
|
110 |
* of its underlying input stream with the same arguments. Subclasses |
|
111 |
* of <code>FilterOutputStream</code> should provide a more efficient |
|
112 |
* implementation of this method. |
|
113 |
* |
|
114 |
* @param b the data. |
|
115 |
* @param off the start offset in the data. |
|
116 |
* @param len the number of bytes to write. |
|
117 |
* @exception IOException if an I/O error occurs. |
|
118 |
* @see java.io.FilterOutputStream#write(int) |
|
119 |
*/ |
|
120 |
public void write(byte b[], int off, int len) throws IOException { |
|
121 |
if ((off | len | (b.length - (len + off)) | (off + len)) < 0) |
|
122 |
throw new IndexOutOfBoundsException(); |
|
123 |
||
124 |
for (int i = 0 ; i < len ; i++) { |
|
125 |
write(b[off + i]); |
|
126 |
} |
|
127 |
} |
|
128 |
||
129 |
/** |
|
130 |
* Flushes this output stream and forces any buffered output bytes |
|
131 |
* to be written out to the stream. |
|
132 |
* <p> |
|
133 |
* The <code>flush</code> method of <code>FilterOutputStream</code> |
|
134 |
* calls the <code>flush</code> method of its underlying output stream. |
|
135 |
* |
|
136 |
* @exception IOException if an I/O error occurs. |
|
137 |
* @see java.io.FilterOutputStream#out |
|
138 |
*/ |
|
139 |
public void flush() throws IOException { |
|
140 |
out.flush(); |
|
141 |
} |
|
142 |
||
143 |
/** |
|
144 |
* Closes this output stream and releases any system resources |
|
145 |
* associated with the stream. |
|
146 |
* <p> |
|
147 |
* The <code>close</code> method of <code>FilterOutputStream</code> |
|
148 |
* calls its <code>flush</code> method, and then calls the |
|
149 |
* <code>close</code> method of its underlying output stream. |
|
150 |
* |
|
151 |
* @exception IOException if an I/O error occurs. |
|
152 |
* @see java.io.FilterOutputStream#flush() |
|
153 |
* @see java.io.FilterOutputStream#out |
|
154 |
*/ |
|
155 |
public void close() throws IOException { |
|
8540
ed028ce13912
7021327: Changes for 7020888 included changes to other files in error
alanb
parents:
8539
diff
changeset
|
156 |
try { |
ed028ce13912
7021327: Changes for 7020888 included changes to other files in error
alanb
parents:
8539
diff
changeset
|
157 |
flush(); |
ed028ce13912
7021327: Changes for 7020888 included changes to other files in error
alanb
parents:
8539
diff
changeset
|
158 |
} catch (IOException ignored) { |
2 | 159 |
} |
8540
ed028ce13912
7021327: Changes for 7020888 included changes to other files in error
alanb
parents:
8539
diff
changeset
|
160 |
out.close(); |
2 | 161 |
} |
162 |
} |