Path: blob/master/src/java.base/share/classes/java/io/BufferedOutputStream.java
41152 views
/*1* Copyright (c) 1994, 2019, 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 1.035*/36public class BufferedOutputStream extends FilterOutputStream {37/**38* The internal buffer where data is stored.39*/40protected byte buf[];4142/**43* The number of valid bytes in the buffer. This value is always44* in the range {@code 0} through {@code buf.length}; elements45* {@code buf[0]} through {@code buf[count-1]} contain valid46* byte data.47*/48protected int count;4950/**51* Creates a new buffered output stream to write data to the52* specified underlying output stream.53*54* @param out the underlying output stream.55*/56public BufferedOutputStream(OutputStream out) {57this(out, 8192);58}5960/**61* Creates a new buffered output stream to write data to the62* specified underlying output stream with the specified buffer63* size.64*65* @param out the underlying output stream.66* @param size the buffer size.67* @throws IllegalArgumentException if size <= 0.68*/69public BufferedOutputStream(OutputStream out, int size) {70super(out);71if (size <= 0) {72throw new IllegalArgumentException("Buffer size <= 0");73}74buf = new byte[size];75}7677/** Flush the internal buffer */78private void flushBuffer() throws IOException {79if (count > 0) {80out.write(buf, 0, count);81count = 0;82}83}8485/**86* Writes the specified byte to this buffered output stream.87*88* @param b the byte to be written.89* @throws IOException if an I/O error occurs.90*/91@Override92public synchronized void write(int b) throws IOException {93if (count >= buf.length) {94flushBuffer();95}96buf[count++] = (byte)b;97}9899/**100* Writes {@code len} bytes from the specified byte array101* starting at offset {@code off} 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}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* @throws IOException if an I/O error occurs.114*/115@Override116public synchronized void write(byte b[], int off, int len) throws IOException {117if (len >= buf.length) {118/* If the request length exceeds the size of the output buffer,119flush the output buffer and then write the data directly.120In this way buffered streams will cascade harmlessly. */121flushBuffer();122out.write(b, off, len);123return;124}125if (len > buf.length - count) {126flushBuffer();127}128System.arraycopy(b, off, buf, count, len);129count += len;130}131132/**133* Flushes this buffered output stream. This forces any buffered134* output bytes to be written out to the underlying output stream.135*136* @throws IOException if an I/O error occurs.137* @see java.io.FilterOutputStream#out138*/139@Override140public synchronized void flush() throws IOException {141flushBuffer();142out.flush();143}144}145146147