Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/io/BufferedOutputStream.java
38829 views
/*1* Copyright (c) 1994, 2003, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.io;2627/**28* The class implements a buffered output stream. By setting up such29* an output stream, an application can write bytes to the underlying30* output stream without necessarily causing a call to the underlying31* system for each byte written.32*33* @author Arthur van Hoff34* @since JDK1.035*/36public37class BufferedOutputStream extends FilterOutputStream {38/**39* The internal buffer where data is stored.40*/41protected byte buf[];4243/**44* The number of valid bytes in the buffer. This value is always45* in the range <tt>0</tt> through <tt>buf.length</tt>; elements46* <tt>buf[0]</tt> through <tt>buf[count-1]</tt> contain valid47* byte data.48*/49protected int count;5051/**52* Creates a new buffered output stream to write data to the53* specified underlying output stream.54*55* @param out the underlying output stream.56*/57public BufferedOutputStream(OutputStream out) {58this(out, 8192);59}6061/**62* Creates a new buffered output stream to write data to the63* specified underlying output stream with the specified buffer64* size.65*66* @param out the underlying output stream.67* @param size the buffer size.68* @exception IllegalArgumentException if size <= 0.69*/70public BufferedOutputStream(OutputStream out, int size) {71super(out);72if (size <= 0) {73throw new IllegalArgumentException("Buffer size <= 0");74}75buf = new byte[size];76}7778/** Flush the internal buffer */79private void flushBuffer() throws IOException {80if (count > 0) {81out.write(buf, 0, count);82count = 0;83}84}8586/**87* Writes the specified byte to this buffered output stream.88*89* @param b the byte to be written.90* @exception IOException if an I/O error occurs.91*/92public synchronized void write(int b) throws IOException {93if (count >= buf.length) {94flushBuffer();95}96buf[count++] = (byte)b;97}9899/**100* Writes <code>len</code> bytes from the specified byte array101* starting at offset <code>off</code> to this buffered output stream.102*103* <p> Ordinarily this method stores bytes from the given array into this104* stream's buffer, flushing the buffer to the underlying output stream as105* needed. If the requested length is at least as large as this stream's106* buffer, however, then this method will flush the buffer and write the107* bytes directly to the underlying output stream. Thus redundant108* <code>BufferedOutputStream</code>s will not copy data unnecessarily.109*110* @param b the data.111* @param off the start offset in the data.112* @param len the number of bytes to write.113* @exception IOException if an I/O error occurs.114*/115public synchronized void write(byte b[], int off, int len) throws IOException {116if (len >= buf.length) {117/* If the request length exceeds the size of the output buffer,118flush the output buffer and then write the data directly.119In this way buffered streams will cascade harmlessly. */120flushBuffer();121out.write(b, off, len);122return;123}124if (len > buf.length - count) {125flushBuffer();126}127System.arraycopy(b, off, buf, count, len);128count += len;129}130131/**132* Flushes this buffered output stream. This forces any buffered133* output bytes to be written out to the underlying output stream.134*135* @exception IOException if an I/O error occurs.136* @see java.io.FilterOutputStream#out137*/138public synchronized void flush() throws IOException {139flushBuffer();140out.flush();141}142}143144145