rt/emul/compact/src/main/java/java/io/FilterOutputStream.java
changeset 772 d382dacfd73f
parent 601 5198affdb915
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/rt/emul/compact/src/main/java/java/io/FilterOutputStream.java	Tue Feb 26 16:54:16 2013 +0100
     1.3 @@ -0,0 +1,162 @@
     1.4 +/*
     1.5 + * Copyright (c) 1994, 2011, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package java.io;
    1.30 +
    1.31 +/**
    1.32 + * This class is the superclass of all classes that filter output
    1.33 + * streams. These streams sit on top of an already existing output
    1.34 + * stream (the <i>underlying</i> output stream) which it uses as its
    1.35 + * basic sink of data, but possibly transforming the data along the
    1.36 + * way or providing additional functionality.
    1.37 + * <p>
    1.38 + * The class <code>FilterOutputStream</code> itself simply overrides
    1.39 + * all methods of <code>OutputStream</code> with versions that pass
    1.40 + * all requests to the underlying output stream. Subclasses of
    1.41 + * <code>FilterOutputStream</code> may further override some of these
    1.42 + * methods as well as provide additional methods and fields.
    1.43 + *
    1.44 + * @author  Jonathan Payne
    1.45 + * @since   JDK1.0
    1.46 + */
    1.47 +public
    1.48 +class FilterOutputStream extends OutputStream {
    1.49 +    /**
    1.50 +     * The underlying output stream to be filtered.
    1.51 +     */
    1.52 +    protected OutputStream out;
    1.53 +
    1.54 +    /**
    1.55 +     * Creates an output stream filter built on top of the specified
    1.56 +     * underlying output stream.
    1.57 +     *
    1.58 +     * @param   out   the underlying output stream to be assigned to
    1.59 +     *                the field <tt>this.out</tt> for later use, or
    1.60 +     *                <code>null</code> if this instance is to be
    1.61 +     *                created without an underlying stream.
    1.62 +     */
    1.63 +    public FilterOutputStream(OutputStream out) {
    1.64 +        this.out = out;
    1.65 +    }
    1.66 +
    1.67 +    /**
    1.68 +     * Writes the specified <code>byte</code> to this output stream.
    1.69 +     * <p>
    1.70 +     * The <code>write</code> method of <code>FilterOutputStream</code>
    1.71 +     * calls the <code>write</code> method of its underlying output stream,
    1.72 +     * that is, it performs <tt>out.write(b)</tt>.
    1.73 +     * <p>
    1.74 +     * Implements the abstract <tt>write</tt> method of <tt>OutputStream</tt>.
    1.75 +     *
    1.76 +     * @param      b   the <code>byte</code>.
    1.77 +     * @exception  IOException  if an I/O error occurs.
    1.78 +     */
    1.79 +    public void write(int b) throws IOException {
    1.80 +        out.write(b);
    1.81 +    }
    1.82 +
    1.83 +    /**
    1.84 +     * Writes <code>b.length</code> bytes to this output stream.
    1.85 +     * <p>
    1.86 +     * The <code>write</code> method of <code>FilterOutputStream</code>
    1.87 +     * calls its <code>write</code> method of three arguments with the
    1.88 +     * arguments <code>b</code>, <code>0</code>, and
    1.89 +     * <code>b.length</code>.
    1.90 +     * <p>
    1.91 +     * Note that this method does not call the one-argument
    1.92 +     * <code>write</code> method of its underlying stream with the single
    1.93 +     * argument <code>b</code>.
    1.94 +     *
    1.95 +     * @param      b   the data to be written.
    1.96 +     * @exception  IOException  if an I/O error occurs.
    1.97 +     * @see        java.io.FilterOutputStream#write(byte[], int, int)
    1.98 +     */
    1.99 +    public void write(byte b[]) throws IOException {
   1.100 +        write(b, 0, b.length);
   1.101 +    }
   1.102 +
   1.103 +    /**
   1.104 +     * Writes <code>len</code> bytes from the specified
   1.105 +     * <code>byte</code> array starting at offset <code>off</code> to
   1.106 +     * this output stream.
   1.107 +     * <p>
   1.108 +     * The <code>write</code> method of <code>FilterOutputStream</code>
   1.109 +     * calls the <code>write</code> method of one argument on each
   1.110 +     * <code>byte</code> to output.
   1.111 +     * <p>
   1.112 +     * Note that this method does not call the <code>write</code> method
   1.113 +     * of its underlying input stream with the same arguments. Subclasses
   1.114 +     * of <code>FilterOutputStream</code> should provide a more efficient
   1.115 +     * implementation of this method.
   1.116 +     *
   1.117 +     * @param      b     the data.
   1.118 +     * @param      off   the start offset in the data.
   1.119 +     * @param      len   the number of bytes to write.
   1.120 +     * @exception  IOException  if an I/O error occurs.
   1.121 +     * @see        java.io.FilterOutputStream#write(int)
   1.122 +     */
   1.123 +    public void write(byte b[], int off, int len) throws IOException {
   1.124 +        if ((off | len | (b.length - (len + off)) | (off + len)) < 0)
   1.125 +            throw new IndexOutOfBoundsException();
   1.126 +
   1.127 +        for (int i = 0 ; i < len ; i++) {
   1.128 +            write(b[off + i]);
   1.129 +        }
   1.130 +    }
   1.131 +
   1.132 +    /**
   1.133 +     * Flushes this output stream and forces any buffered output bytes
   1.134 +     * to be written out to the stream.
   1.135 +     * <p>
   1.136 +     * The <code>flush</code> method of <code>FilterOutputStream</code>
   1.137 +     * calls the <code>flush</code> method of its underlying output stream.
   1.138 +     *
   1.139 +     * @exception  IOException  if an I/O error occurs.
   1.140 +     * @see        java.io.FilterOutputStream#out
   1.141 +     */
   1.142 +    public void flush() throws IOException {
   1.143 +        out.flush();
   1.144 +    }
   1.145 +
   1.146 +    /**
   1.147 +     * Closes this output stream and releases any system resources
   1.148 +     * associated with the stream.
   1.149 +     * <p>
   1.150 +     * The <code>close</code> method of <code>FilterOutputStream</code>
   1.151 +     * calls its <code>flush</code> method, and then calls the
   1.152 +     * <code>close</code> method of its underlying output stream.
   1.153 +     *
   1.154 +     * @exception  IOException  if an I/O error occurs.
   1.155 +     * @see        java.io.FilterOutputStream#flush()
   1.156 +     * @see        java.io.FilterOutputStream#out
   1.157 +     */
   1.158 +    public void close() throws IOException {
   1.159 +        try {
   1.160 +          flush();
   1.161 +        } catch (IOException ignored) {
   1.162 +        }
   1.163 +        out.close();
   1.164 +    }
   1.165 +}