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