rt/emul/compact/src/main/java/java/io/BufferedOutputStream.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 26 Feb 2013 16:54:16 +0100
changeset 772 d382dacfd73f
parent 682 emul/compact/src/main/java/java/io/BufferedOutputStream.java@5d25a1df3540
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@682
     1
/*
jaroslav@682
     2
 * Copyright (c) 1994, 2003, Oracle and/or its affiliates. All rights reserved.
jaroslav@682
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jaroslav@682
     4
 *
jaroslav@682
     5
 * This code is free software; you can redistribute it and/or modify it
jaroslav@682
     6
 * under the terms of the GNU General Public License version 2 only, as
jaroslav@682
     7
 * published by the Free Software Foundation.  Oracle designates this
jaroslav@682
     8
 * particular file as subject to the "Classpath" exception as provided
jaroslav@682
     9
 * by Oracle in the LICENSE file that accompanied this code.
jaroslav@682
    10
 *
jaroslav@682
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
jaroslav@682
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jaroslav@682
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jaroslav@682
    14
 * version 2 for more details (a copy is included in the LICENSE file that
jaroslav@682
    15
 * accompanied this code).
jaroslav@682
    16
 *
jaroslav@682
    17
 * You should have received a copy of the GNU General Public License version
jaroslav@682
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
jaroslav@682
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jaroslav@682
    20
 *
jaroslav@682
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jaroslav@682
    22
 * or visit www.oracle.com if you need additional information or have any
jaroslav@682
    23
 * questions.
jaroslav@682
    24
 */
jaroslav@682
    25
jaroslav@682
    26
package java.io;
jaroslav@682
    27
jaroslav@682
    28
/**
jaroslav@682
    29
 * The class implements a buffered output stream. By setting up such
jaroslav@682
    30
 * an output stream, an application can write bytes to the underlying
jaroslav@682
    31
 * output stream without necessarily causing a call to the underlying
jaroslav@682
    32
 * system for each byte written.
jaroslav@682
    33
 *
jaroslav@682
    34
 * @author  Arthur van Hoff
jaroslav@682
    35
 * @since   JDK1.0
jaroslav@682
    36
 */
jaroslav@682
    37
public
jaroslav@682
    38
class BufferedOutputStream extends FilterOutputStream {
jaroslav@682
    39
    /**
jaroslav@682
    40
     * The internal buffer where data is stored.
jaroslav@682
    41
     */
jaroslav@682
    42
    protected byte buf[];
jaroslav@682
    43
jaroslav@682
    44
    /**
jaroslav@682
    45
     * The number of valid bytes in the buffer. This value is always
jaroslav@682
    46
     * in the range <tt>0</tt> through <tt>buf.length</tt>; elements
jaroslav@682
    47
     * <tt>buf[0]</tt> through <tt>buf[count-1]</tt> contain valid
jaroslav@682
    48
     * byte data.
jaroslav@682
    49
     */
jaroslav@682
    50
    protected int count;
jaroslav@682
    51
jaroslav@682
    52
    /**
jaroslav@682
    53
     * Creates a new buffered output stream to write data to the
jaroslav@682
    54
     * specified underlying output stream.
jaroslav@682
    55
     *
jaroslav@682
    56
     * @param   out   the underlying output stream.
jaroslav@682
    57
     */
jaroslav@682
    58
    public BufferedOutputStream(OutputStream out) {
jaroslav@682
    59
        this(out, 8192);
jaroslav@682
    60
    }
jaroslav@682
    61
jaroslav@682
    62
    /**
jaroslav@682
    63
     * Creates a new buffered output stream to write data to the
jaroslav@682
    64
     * specified underlying output stream with the specified buffer
jaroslav@682
    65
     * size.
jaroslav@682
    66
     *
jaroslav@682
    67
     * @param   out    the underlying output stream.
jaroslav@682
    68
     * @param   size   the buffer size.
jaroslav@682
    69
     * @exception IllegalArgumentException if size &lt;= 0.
jaroslav@682
    70
     */
jaroslav@682
    71
    public BufferedOutputStream(OutputStream out, int size) {
jaroslav@682
    72
        super(out);
jaroslav@682
    73
        if (size <= 0) {
jaroslav@682
    74
            throw new IllegalArgumentException("Buffer size <= 0");
jaroslav@682
    75
        }
jaroslav@682
    76
        buf = new byte[size];
jaroslav@682
    77
    }
jaroslav@682
    78
jaroslav@682
    79
    /** Flush the internal buffer */
jaroslav@682
    80
    private void flushBuffer() throws IOException {
jaroslav@682
    81
        if (count > 0) {
jaroslav@682
    82
            out.write(buf, 0, count);
jaroslav@682
    83
            count = 0;
jaroslav@682
    84
        }
jaroslav@682
    85
    }
jaroslav@682
    86
jaroslav@682
    87
    /**
jaroslav@682
    88
     * Writes the specified byte to this buffered output stream.
jaroslav@682
    89
     *
jaroslav@682
    90
     * @param      b   the byte to be written.
jaroslav@682
    91
     * @exception  IOException  if an I/O error occurs.
jaroslav@682
    92
     */
jaroslav@682
    93
    public synchronized void write(int b) throws IOException {
jaroslav@682
    94
        if (count >= buf.length) {
jaroslav@682
    95
            flushBuffer();
jaroslav@682
    96
        }
jaroslav@682
    97
        buf[count++] = (byte)b;
jaroslav@682
    98
    }
jaroslav@682
    99
jaroslav@682
   100
    /**
jaroslav@682
   101
     * Writes <code>len</code> bytes from the specified byte array
jaroslav@682
   102
     * starting at offset <code>off</code> to this buffered output stream.
jaroslav@682
   103
     *
jaroslav@682
   104
     * <p> Ordinarily this method stores bytes from the given array into this
jaroslav@682
   105
     * stream's buffer, flushing the buffer to the underlying output stream as
jaroslav@682
   106
     * needed.  If the requested length is at least as large as this stream's
jaroslav@682
   107
     * buffer, however, then this method will flush the buffer and write the
jaroslav@682
   108
     * bytes directly to the underlying output stream.  Thus redundant
jaroslav@682
   109
     * <code>BufferedOutputStream</code>s will not copy data unnecessarily.
jaroslav@682
   110
     *
jaroslav@682
   111
     * @param      b     the data.
jaroslav@682
   112
     * @param      off   the start offset in the data.
jaroslav@682
   113
     * @param      len   the number of bytes to write.
jaroslav@682
   114
     * @exception  IOException  if an I/O error occurs.
jaroslav@682
   115
     */
jaroslav@682
   116
    public synchronized void write(byte b[], int off, int len) throws IOException {
jaroslav@682
   117
        if (len >= buf.length) {
jaroslav@682
   118
            /* If the request length exceeds the size of the output buffer,
jaroslav@682
   119
               flush the output buffer and then write the data directly.
jaroslav@682
   120
               In this way buffered streams will cascade harmlessly. */
jaroslav@682
   121
            flushBuffer();
jaroslav@682
   122
            out.write(b, off, len);
jaroslav@682
   123
            return;
jaroslav@682
   124
        }
jaroslav@682
   125
        if (len > buf.length - count) {
jaroslav@682
   126
            flushBuffer();
jaroslav@682
   127
        }
jaroslav@682
   128
        System.arraycopy(b, off, buf, count, len);
jaroslav@682
   129
        count += len;
jaroslav@682
   130
    }
jaroslav@682
   131
jaroslav@682
   132
    /**
jaroslav@682
   133
     * Flushes this buffered output stream. This forces any buffered
jaroslav@682
   134
     * output bytes to be written out to the underlying output stream.
jaroslav@682
   135
     *
jaroslav@682
   136
     * @exception  IOException  if an I/O error occurs.
jaroslav@682
   137
     * @see        java.io.FilterOutputStream#out
jaroslav@682
   138
     */
jaroslav@682
   139
    public synchronized void flush() throws IOException {
jaroslav@682
   140
        flushBuffer();
jaroslav@682
   141
        out.flush();
jaroslav@682
   142
    }
jaroslav@682
   143
}