jdk/src/share/classes/java/io/FilterOutputStream.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 8540 ed028ce13912
child 10347 1c9efe1ec7d3
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 8540
diff changeset
     2
 * Copyright (c) 1994, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.io;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * This class is the superclass of all classes that filter output
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * streams. These streams sit on top of an already existing output
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * stream (the <i>underlying</i> output stream) which it uses as its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * basic sink of data, but possibly transforming the data along the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * way or providing additional functionality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * The class <code>FilterOutputStream</code> itself simply overrides
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * all methods of <code>OutputStream</code> with versions that pass
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * all requests to the underlying output stream. Subclasses of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <code>FilterOutputStream</code> may further override some of these
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * methods as well as provide additional methods and fields.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @author  Jonathan Payne
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @since   JDK1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
class FilterOutputStream extends OutputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     * The underlying output stream to be filtered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    protected OutputStream out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * Creates an output stream filter built on top of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * underlying output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * @param   out   the underlying output stream to be assigned to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     *                the field <tt>this.out</tt> for later use, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     *                <code>null</code> if this instance is to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     *                created without an underlying stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    public FilterOutputStream(OutputStream out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        this.out = out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * Writes the specified <code>byte</code> to this output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * The <code>write</code> method of <code>FilterOutputStream</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * calls the <code>write</code> method of its underlying output stream,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * that is, it performs <tt>out.write(b)</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * Implements the abstract <tt>write</tt> method of <tt>OutputStream</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * @param      b   the <code>byte</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    public void write(int b) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        out.write(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * Writes <code>b.length</code> bytes to this output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * The <code>write</code> method of <code>FilterOutputStream</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * calls its <code>write</code> method of three arguments with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * arguments <code>b</code>, <code>0</code>, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * <code>b.length</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * Note that this method does not call the one-argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * <code>write</code> method of its underlying stream with the single
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * argument <code>b</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * @param      b   the data to be written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * @see        java.io.FilterOutputStream#write(byte[], int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    public void write(byte b[]) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        write(b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * Writes <code>len</code> bytes from the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * <code>byte</code> array starting at offset <code>off</code> to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * this output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * The <code>write</code> method of <code>FilterOutputStream</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * calls the <code>write</code> method of one argument on each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * <code>byte</code> to output.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * Note that this method does not call the <code>write</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * of its underlying input stream with the same arguments. Subclasses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * of <code>FilterOutputStream</code> should provide a more efficient
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * implementation of this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @param      b     the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @param      off   the start offset in the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @param      len   the number of bytes to write.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @see        java.io.FilterOutputStream#write(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    public void write(byte b[], int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        if ((off | len | (b.length - (len + off)) | (off + len)) < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        for (int i = 0 ; i < len ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            write(b[off + i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * Flushes this output stream and forces any buffered output bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * to be written out to the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * The <code>flush</code> method of <code>FilterOutputStream</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * calls the <code>flush</code> method of its underlying output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @see        java.io.FilterOutputStream#out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    public void flush() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * Closes this output stream and releases any system resources
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * associated with the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * The <code>close</code> method of <code>FilterOutputStream</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * calls its <code>flush</code> method, and then calls the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * <code>close</code> method of its underlying output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * @see        java.io.FilterOutputStream#flush()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @see        java.io.FilterOutputStream#out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    public void close() throws IOException {
8540
ed028ce13912 7021327: Changes for 7020888 included changes to other files in error
alanb
parents: 8539
diff changeset
   156
        try {
ed028ce13912 7021327: Changes for 7020888 included changes to other files in error
alanb
parents: 8539
diff changeset
   157
          flush();
ed028ce13912 7021327: Changes for 7020888 included changes to other files in error
alanb
parents: 8539
diff changeset
   158
        } catch (IOException ignored) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        }
8540
ed028ce13912 7021327: Changes for 7020888 included changes to other files in error
alanb
parents: 8539
diff changeset
   160
        out.close();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
}