blob: 173e91724d49bb92e2e0916719ddfbd51ca04bbe [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1994-1999 Sun Microsystems, Inc. All Rights Reserved.
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
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
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 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26package 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 */
44public
45class 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 {
156 try {
157 flush();
158 } catch (IOException ignored) {
159 }
160 out.close();
161 }
162}