jaroslav@601: /* jaroslav@601: * Copyright (c) 1994, 2011, Oracle and/or its affiliates. All rights reserved. jaroslav@601: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jaroslav@601: * jaroslav@601: * This code is free software; you can redistribute it and/or modify it jaroslav@601: * under the terms of the GNU General Public License version 2 only, as jaroslav@601: * published by the Free Software Foundation. Oracle designates this jaroslav@601: * particular file as subject to the "Classpath" exception as provided jaroslav@601: * by Oracle in the LICENSE file that accompanied this code. jaroslav@601: * jaroslav@601: * This code is distributed in the hope that it will be useful, but WITHOUT jaroslav@601: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jaroslav@601: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jaroslav@601: * version 2 for more details (a copy is included in the LICENSE file that jaroslav@601: * accompanied this code). jaroslav@601: * jaroslav@601: * You should have received a copy of the GNU General Public License version jaroslav@601: * 2 along with this work; if not, write to the Free Software Foundation, jaroslav@601: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jaroslav@601: * jaroslav@601: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jaroslav@601: * or visit www.oracle.com if you need additional information or have any jaroslav@601: * questions. jaroslav@601: */ jaroslav@601: jaroslav@601: package java.io; jaroslav@601: jaroslav@601: /** jaroslav@601: * This class is the superclass of all classes that filter output jaroslav@601: * streams. These streams sit on top of an already existing output jaroslav@601: * stream (the underlying output stream) which it uses as its jaroslav@601: * basic sink of data, but possibly transforming the data along the jaroslav@601: * way or providing additional functionality. jaroslav@601: *

jaroslav@601: * The class FilterOutputStream itself simply overrides jaroslav@601: * all methods of OutputStream with versions that pass jaroslav@601: * all requests to the underlying output stream. Subclasses of jaroslav@601: * FilterOutputStream may further override some of these jaroslav@601: * methods as well as provide additional methods and fields. jaroslav@601: * jaroslav@601: * @author Jonathan Payne jaroslav@601: * @since JDK1.0 jaroslav@601: */ jaroslav@601: public jaroslav@601: class FilterOutputStream extends OutputStream { jaroslav@601: /** jaroslav@601: * The underlying output stream to be filtered. jaroslav@601: */ jaroslav@601: protected OutputStream out; jaroslav@601: jaroslav@601: /** jaroslav@601: * Creates an output stream filter built on top of the specified jaroslav@601: * underlying output stream. jaroslav@601: * jaroslav@601: * @param out the underlying output stream to be assigned to jaroslav@601: * the field this.out for later use, or jaroslav@601: * null if this instance is to be jaroslav@601: * created without an underlying stream. jaroslav@601: */ jaroslav@601: public FilterOutputStream(OutputStream out) { jaroslav@601: this.out = out; jaroslav@601: } jaroslav@601: jaroslav@601: /** jaroslav@601: * Writes the specified byte to this output stream. jaroslav@601: *

jaroslav@601: * The write method of FilterOutputStream jaroslav@601: * calls the write method of its underlying output stream, jaroslav@601: * that is, it performs out.write(b). jaroslav@601: *

jaroslav@601: * Implements the abstract write method of OutputStream. jaroslav@601: * jaroslav@601: * @param b the byte. jaroslav@601: * @exception IOException if an I/O error occurs. jaroslav@601: */ jaroslav@601: public void write(int b) throws IOException { jaroslav@601: out.write(b); jaroslav@601: } jaroslav@601: jaroslav@601: /** jaroslav@601: * Writes b.length bytes to this output stream. jaroslav@601: *

jaroslav@601: * The write method of FilterOutputStream jaroslav@601: * calls its write method of three arguments with the jaroslav@601: * arguments b, 0, and jaroslav@601: * b.length. jaroslav@601: *

jaroslav@601: * Note that this method does not call the one-argument jaroslav@601: * write method of its underlying stream with the single jaroslav@601: * argument b. jaroslav@601: * jaroslav@601: * @param b the data to be written. jaroslav@601: * @exception IOException if an I/O error occurs. jaroslav@601: * @see java.io.FilterOutputStream#write(byte[], int, int) jaroslav@601: */ jaroslav@601: public void write(byte b[]) throws IOException { jaroslav@601: write(b, 0, b.length); jaroslav@601: } jaroslav@601: jaroslav@601: /** jaroslav@601: * Writes len bytes from the specified jaroslav@601: * byte array starting at offset off to jaroslav@601: * this output stream. jaroslav@601: *

jaroslav@601: * The write method of FilterOutputStream jaroslav@601: * calls the write method of one argument on each jaroslav@601: * byte to output. jaroslav@601: *

jaroslav@601: * Note that this method does not call the write method jaroslav@601: * of its underlying input stream with the same arguments. Subclasses jaroslav@601: * of FilterOutputStream should provide a more efficient jaroslav@601: * implementation of this method. jaroslav@601: * jaroslav@601: * @param b the data. jaroslav@601: * @param off the start offset in the data. jaroslav@601: * @param len the number of bytes to write. jaroslav@601: * @exception IOException if an I/O error occurs. jaroslav@601: * @see java.io.FilterOutputStream#write(int) jaroslav@601: */ jaroslav@601: public void write(byte b[], int off, int len) throws IOException { jaroslav@601: if ((off | len | (b.length - (len + off)) | (off + len)) < 0) jaroslav@601: throw new IndexOutOfBoundsException(); jaroslav@601: jaroslav@601: for (int i = 0 ; i < len ; i++) { jaroslav@601: write(b[off + i]); jaroslav@601: } jaroslav@601: } jaroslav@601: jaroslav@601: /** jaroslav@601: * Flushes this output stream and forces any buffered output bytes jaroslav@601: * to be written out to the stream. jaroslav@601: *

jaroslav@601: * The flush method of FilterOutputStream jaroslav@601: * calls the flush method of its underlying output stream. jaroslav@601: * jaroslav@601: * @exception IOException if an I/O error occurs. jaroslav@601: * @see java.io.FilterOutputStream#out jaroslav@601: */ jaroslav@601: public void flush() throws IOException { jaroslav@601: out.flush(); jaroslav@601: } jaroslav@601: jaroslav@601: /** jaroslav@601: * Closes this output stream and releases any system resources jaroslav@601: * associated with the stream. jaroslav@601: *

jaroslav@601: * The close method of FilterOutputStream jaroslav@601: * calls its flush method, and then calls the jaroslav@601: * close method of its underlying output stream. jaroslav@601: * jaroslav@601: * @exception IOException if an I/O error occurs. jaroslav@601: * @see java.io.FilterOutputStream#flush() jaroslav@601: * @see java.io.FilterOutputStream#out jaroslav@601: */ jaroslav@601: public void close() throws IOException { jaroslav@601: try { jaroslav@601: flush(); jaroslav@601: } catch (IOException ignored) { jaroslav@601: } jaroslav@601: out.close(); jaroslav@601: } jaroslav@601: }