001: /*
002: * @(#)FilterOutputStream.java 1.35 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027:
028: package java.io;
029:
030: /**
031: * This class is the superclass of all classes that filter output
032: * streams. These streams sit on top of an already existing output
033: * stream (the <i>underlying</i> output stream) which it uses as its
034: * basic sink of data, but possibly transforming the data along the
035: * way or providing additional functionality.
036: * <p>
037: * The class <code>FilterOutputStream</code> itself simply overrides
038: * all methods of <code>OutputStream</code> with versions that pass
039: * all requests to the underlying output stream. Subclasses of
040: * <code>FilterOutputStream</code> may further override some of these
041: * methods as well as provide additional methods and fields.
042: *
043: * @author Jonathan Payne
044: * @version 1.28, 02/02/00
045: * @since JDK1.0
046: */
047: public class FilterOutputStream extends OutputStream {
048: /**
049: * The underlying output stream to be filtered.
050: */
051: protected OutputStream out;
052:
053: /**
054: * Creates an output stream filter built on top of the specified
055: * underlying output stream.
056: *
057: * @param out the underlying output stream to be assigned to
058: * the field <tt>this.out</tt> for later use, or
059: * <code>null</code> if this instance is to be
060: * created without an underlying stream.
061: */
062: public FilterOutputStream(OutputStream out) {
063: this .out = out;
064: }
065:
066: /**
067: * Writes the specified <code>byte</code> to this output stream.
068: * <p>
069: * The <code>write</code> method of <code>FilterOutputStream</code>
070: * calls the <code>write</code> method of its underlying output stream,
071: * that is, it performs <tt>out.write(b)</tt>.
072: * <p>
073: * Implements the abstract <tt>write</tt> method of <tt>OutputStream</tt>.
074: *
075: * @param b the <code>byte</code>.
076: * @exception IOException if an I/O error occurs.
077: */
078: public void write(int b) throws IOException {
079: out.write(b);
080: }
081:
082: /**
083: * Writes <code>b.length</code> bytes to this output stream.
084: * <p>
085: * The <code>write</code> method of <code>FilterOutputStream</code>
086: * calls its <code>write</code> method of three arguments with the
087: * arguments <code>b</code>, <code>0</code>, and
088: * <code>b.length</code>.
089: * <p>
090: * Note that this method does not call the one-argument
091: * <code>write</code> method of its underlying stream with the single
092: * argument <code>b</code>.
093: *
094: * @param b the data to be written.
095: * @exception IOException if an I/O error occurs.
096: * @see java.io.FilterOutputStream#write(byte[], int, int)
097: */
098: public void write(byte b[]) throws IOException {
099: write(b, 0, b.length);
100: }
101:
102: /**
103: * Writes <code>len</code> bytes from the specified
104: * <code>byte</code> array starting at offset <code>off</code> to
105: * this output stream.
106: * <p>
107: * The <code>write</code> method of <code>FilterOutputStream</code>
108: * calls the <code>write</code> method of one argument on each
109: * <code>byte</code> to output.
110: * <p>
111: * Note that this method does not call the <code>write</code> method
112: * of its underlying input stream with the same arguments. Subclasses
113: * of <code>FilterOutputStream</code> should provide a more efficient
114: * implementation of this method.
115: *
116: * @param b the data.
117: * @param off the start offset in the data.
118: * @param len the number of bytes to write.
119: * @exception IOException if an I/O error occurs.
120: * @see java.io.FilterOutputStream#write(int)
121: */
122: public void write(byte b[], int off, int len) throws IOException {
123: if ((off | len | (b.length - (len + off)) | (off + len)) < 0)
124: throw new IndexOutOfBoundsException();
125:
126: for (int i = 0; i < len; i++) {
127: write(b[off + i]);
128: }
129: }
130:
131: /**
132: * Flushes this output stream and forces any buffered output bytes
133: * to be written out to the stream.
134: * <p>
135: * The <code>flush</code> method of <code>FilterOutputStream</code>
136: * calls the <code>flush</code> method of its underlying output stream.
137: *
138: * @exception IOException if an I/O error occurs.
139: * @see java.io.FilterOutputStream#out
140: */
141: public void flush() throws IOException {
142: out.flush();
143: }
144:
145: /**
146: * Closes this output stream and releases any system resources
147: * associated with the stream.
148: * <p>
149: * The <code>close</code> method of <code>FilterOutputStream</code>
150: * calls its <code>flush</code> method, and then calls the
151: * <code>close</code> method of its underlying output stream.
152: *
153: * @exception IOException if an I/O error occurs.
154: * @see java.io.FilterOutputStream#flush()
155: * @see java.io.FilterOutputStream#out
156: */
157: public void close() throws IOException {
158: try {
159: flush();
160: } catch (IOException ignored) {
161: }
162: out.close();
163: }
164: }
|