001 /*
002 * Copyright 1994-1999 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package java.io;
027
028 /**
029 * This class is the superclass of all classes that filter output
030 * streams. These streams sit on top of an already existing output
031 * stream (the <i>underlying</i> output stream) which it uses as its
032 * basic sink of data, but possibly transforming the data along the
033 * way or providing additional functionality.
034 * <p>
035 * The class <code>FilterOutputStream</code> itself simply overrides
036 * all methods of <code>OutputStream</code> with versions that pass
037 * all requests to the underlying output stream. Subclasses of
038 * <code>FilterOutputStream</code> may further override some of these
039 * methods as well as provide additional methods and fields.
040 *
041 * @author Jonathan Payne
042 * @version 1.38, 05/05/07
043 * @since JDK1.0
044 */
045 public class FilterOutputStream extends OutputStream {
046 /**
047 * The underlying output stream to be filtered.
048 */
049 protected OutputStream out;
050
051 /**
052 * Creates an output stream filter built on top of the specified
053 * underlying output stream.
054 *
055 * @param out the underlying output stream to be assigned to
056 * the field <tt>this.out</tt> for later use, or
057 * <code>null</code> if this instance is to be
058 * created without an underlying stream.
059 */
060 public FilterOutputStream(OutputStream out) {
061 this .out = out;
062 }
063
064 /**
065 * Writes the specified <code>byte</code> to this output stream.
066 * <p>
067 * The <code>write</code> method of <code>FilterOutputStream</code>
068 * calls the <code>write</code> method of its underlying output stream,
069 * that is, it performs <tt>out.write(b)</tt>.
070 * <p>
071 * Implements the abstract <tt>write</tt> method of <tt>OutputStream</tt>.
072 *
073 * @param b the <code>byte</code>.
074 * @exception IOException if an I/O error occurs.
075 */
076 public void write(int b) throws IOException {
077 out.write(b);
078 }
079
080 /**
081 * Writes <code>b.length</code> bytes to this output stream.
082 * <p>
083 * The <code>write</code> method of <code>FilterOutputStream</code>
084 * calls its <code>write</code> method of three arguments with the
085 * arguments <code>b</code>, <code>0</code>, and
086 * <code>b.length</code>.
087 * <p>
088 * Note that this method does not call the one-argument
089 * <code>write</code> method of its underlying stream with the single
090 * argument <code>b</code>.
091 *
092 * @param b the data to be written.
093 * @exception IOException if an I/O error occurs.
094 * @see java.io.FilterOutputStream#write(byte[], int, int)
095 */
096 public void write(byte b[]) throws IOException {
097 write(b, 0, b.length);
098 }
099
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 }
|