Top Description Fields Constructors Methods
java.io

public Class BufferedOutputStream

extends FilterOutputStream
Class Inheritance
Known Direct Subclasses
sun.net.TelnetOutputStream, java.lang.ProcessImpl.ProcessPipeOutputStream
Imports
java.util.Arrays, jdk.internal.misc.InternalLock, .VM

The class implements a buffered output stream. By setting up such an output stream, an application can write bytes to the underlying output stream without necessarily causing a call to the underlying system for each byte written.
Author
Arthur van Hoff
Since
1.0

Field Summary

Modifier and TypeField and Description
protected byte[]
buf

The internal buffer where data is stored.

protected int
count

The number of valid bytes in the buffer.

private static final int
private static final int
private final InternalLock
private final int
maxBufSize

Max size of the internal buffer.

Inherited from java.io.FilterOutputStream:
out

Constructor Summary

AccessConstructor and Description
private
BufferedOutputStream(OutputStream out, int initialSize, int maxSize)

Creates a new buffered output stream.

public
BufferedOutputStream(OutputStream
the underlying output stream.
out
)

Creates a new buffered output stream to write data to the specified underlying output stream.

public
BufferedOutputStream(OutputStream
the underlying output stream.
out
,
int
the buffer size.
size
)

Creates a new buffered output stream to write data to the specified underlying output stream with the specified buffer size.

Method Summary

Modifier and TypeMethod and Description
public void
flush()

Overrides java.io.FilterOutputStream.flush.

Implements java.io.Flushable.flush.

Flushes this buffered output stream.

private void
flushBuffer()

Flush the internal buffer

private void
growIfNeeded(int len)

Grow buf to fit an additional len bytes if needed.

private void
private void
implWrite(int b)

private void
implWrite(byte[] b, int off, int len)

private static int
initialBufferSize()

Returns the buffer size to use when no output buffer size specified.

public void
write(int
the byte to be written.
b
)

Overrides java.io.FilterOutputStream.write.

Writes the specified byte to this buffered output stream.

public void
write(byte[]
the data.
b
,
int
the start offset in the data.
off
,
int
the number of bytes to write.
len
)

Overrides java.io.FilterOutputStream.write.

Writes len bytes from the specified byte array starting at offset off to this buffered output stream.

Inherited from java.io.FilterOutputStream:
closewrite

Field Detail

bufback to summary
protected byte[] buf

The internal buffer where data is stored.

countback to summary
protected int count

The number of valid bytes in the buffer. This value is always in the range 0 through buf.length; elements buf[0] through buf[count-1] contain valid byte data.

DEFAULT_INITIAL_BUFFER_SIZEback to summary
private static final int DEFAULT_INITIAL_BUFFER_SIZE
DEFAULT_MAX_BUFFER_SIZEback to summary
private static final int DEFAULT_MAX_BUFFER_SIZE
lockback to summary
private final InternalLock lock
maxBufSizeback to summary
private final int maxBufSize

Max size of the internal buffer.

Constructor Detail

BufferedOutputStreamback to summary
private BufferedOutputStream(OutputStream out, int initialSize, int maxSize)

Creates a new buffered output stream.

BufferedOutputStreamback to summary
public BufferedOutputStream(OutputStream out)

Creates a new buffered output stream to write data to the specified underlying output stream.

Parameters
out:OutputStream

the underlying output stream.

BufferedOutputStreamback to summary
public BufferedOutputStream(OutputStream out, int size)

Creates a new buffered output stream to write data to the specified underlying output stream with the specified buffer size.

Parameters
out:OutputStream

the underlying output stream.

size:int

the buffer size.

Exceptions
IllegalArgumentException:
if size <= 0.

Method Detail

flushback to summary
public void flush() throws IOException

Overrides java.io.FilterOutputStream.flush.

Implements java.io.Flushable.flush.

Flushes this buffered output stream. This forces any buffered output bytes to be written out to the underlying output stream.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs.
See Also
java.io.FilterOutputStream#out
flushBufferback to summary
private void flushBuffer() throws IOException

Flush the internal buffer

growIfNeededback to summary
private void growIfNeeded(int len)

Grow buf to fit an additional len bytes if needed. If possible, it grows by len+1 to avoid flushing when len bytes are added. A no-op if the buffer is not resizable. This method should only be called while holding the lock.

implFlushback to summary
private void implFlush() throws IOException
implWriteback to summary
private void implWrite(int b) throws IOException
implWriteback to summary
private void implWrite(byte[] b, int off, int len) throws IOException
initialBufferSizeback to summary
private static int initialBufferSize()

Returns the buffer size to use when no output buffer size specified.

writeback to summary
public void write(int b) throws IOException

Overrides java.io.FilterOutputStream.write.

Writes the specified byte to this buffered output stream.

Parameters
b:int

the byte to be written.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs.
writeback to summary
public void write(byte[] b, int off, int len) throws IOException

Overrides java.io.FilterOutputStream.write.

Writes len bytes from the specified byte array starting at offset off to this buffered output stream.

Ordinarily this method stores bytes from the given array into this stream's buffer, flushing the buffer to the underlying output stream as needed. If the requested length is at least as large as this stream's buffer, however, then this method will flush the buffer and write the bytes directly to the underlying output stream. Thus redundant BufferedOutputStreams will not copy data unnecessarily.

Parameters
b:byte[]

the data.

off:int

the start offset in the data.

len:int

the number of bytes to write.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs.
IndexOutOfBoundsException:
If off is negative, len is negative, or len is greater than b.length - off