src/java.base/share/classes/java/io/BufferedOutputStream.java
author jboes
Tue, 24 Sep 2019 09:43:43 +0100
changeset 58288 48e480e56aad
parent 58242 94bb65cb37d3
child 58679 9c3209ff7550
permissions -rw-r--r--
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base Summary: Minor coding style update of javadoc tag in any file in java.base Reviewed-by: bchristi, lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
     2
 * Copyright (c) 1994, 2019, 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
 * The class implements a buffered output stream. By setting up such
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * an output stream, an application can write bytes to the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * output stream without necessarily causing a call to the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * system for each byte written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * @author  Arthur van Hoff
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 5506
diff changeset
    35
 * @since   1.0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 */
31421
d6ddd21f6df8 8042377: BufferedWriter and FilteredOutputStream.close throw IAE if flush and close throw equal exceptions
bpb
parents: 25859
diff changeset
    37
public class BufferedOutputStream extends FilterOutputStream {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
     * The internal buffer where data is stored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    protected byte buf[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
     * The number of valid bytes in the buffer. This value is always
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31421
diff changeset
    45
     * in the range {@code 0} through {@code buf.length}; elements
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31421
diff changeset
    46
     * {@code buf[0]} through {@code buf[count-1]} contain valid
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     * byte data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    protected int count;
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 a new buffered output stream to write data to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * specified 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.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    public BufferedOutputStream(OutputStream out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        this(out, 8192);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * Creates a new buffered output stream to write data to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * specified underlying output stream with the specified buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * @param   out    the underlying output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * @param   size   the buffer size.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
    68
     * @throws  IllegalArgumentException if size &lt;= 0.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    public BufferedOutputStream(OutputStream out, int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        super(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        if (size <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            throw new IllegalArgumentException("Buffer size <= 0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        buf = new byte[size];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /** Flush the internal buffer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private void flushBuffer() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        if (count > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            out.write(buf, 0, count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * Writes the specified byte to this buffered output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * @param      b   the byte to be written.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
    90
     * @throws     IOException  if an I/O error occurs.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     */
31421
d6ddd21f6df8 8042377: BufferedWriter and FilteredOutputStream.close throw IAE if flush and close throw equal exceptions
bpb
parents: 25859
diff changeset
    92
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    public synchronized void write(int b) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        if (count >= buf.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            flushBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        buf[count++] = (byte)b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   101
     * Writes {@code len} bytes from the specified byte array
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   102
     * starting at offset {@code off} to this buffered output stream.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * <p> Ordinarily this method stores bytes from the given array into this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * stream's buffer, flushing the buffer to the underlying output stream as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * needed.  If the requested length is at least as large as this stream's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * buffer, however, then this method will flush the buffer and write the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * bytes directly to the underlying output stream.  Thus redundant
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   109
     * {@code BufferedOutputStream}s will not copy data unnecessarily.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * @param      b     the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @param      off   the start offset in the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @param      len   the number of bytes to write.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   114
     * @throws     IOException  if an I/O error occurs.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
31421
d6ddd21f6df8 8042377: BufferedWriter and FilteredOutputStream.close throw IAE if flush and close throw equal exceptions
bpb
parents: 25859
diff changeset
   116
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    public synchronized void write(byte b[], int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        if (len >= buf.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            /* If the request length exceeds the size of the output buffer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
               flush the output buffer and then write the data directly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
               In this way buffered streams will cascade harmlessly. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            flushBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            out.write(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        if (len > buf.length - count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            flushBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        System.arraycopy(b, off, buf, count, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        count += len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * Flushes this buffered output stream. This forces any buffered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * output bytes to be written out to the underlying output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   137
     * @throws     IOException  if an I/O error occurs.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @see        java.io.FilterOutputStream#out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
31421
d6ddd21f6df8 8042377: BufferedWriter and FilteredOutputStream.close throw IAE if flush and close throw equal exceptions
bpb
parents: 25859
diff changeset
   140
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    public synchronized void flush() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        flushBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
}