Package java.io

Class BufferedOutputStream

All Implemented Interfaces:
Closeable, Flushable, AutoCloseable

public class BufferedOutputStream
extends FilterOutputStream
Wraps an existing OutputStream and buffers the output. Expensive interaction with the underlying input stream is minimized, since most (smaller) requests can be satisfied by accessing the buffer alone. The drawback is that some extra space is required to hold the buffer and that copying takes place when flushing that buffer, but this is usually outweighed by the performance benefits.

A typical application pattern for the class looks like this:

 BufferedOutputStream buf = new BufferedOutputStream(new FileOutputStream("file.java"));
 
See Also:
BufferedInputStream
  • Field Summary

    Fields
    Modifier and Type Field Description
    protected byte[] buf
    The buffer containing the bytes to be written to the target stream.
    protected int count
    The total number of bytes inside the byte array buf.

    Fields inherited from class java.io.FilterOutputStream

    out
  • Constructor Summary

    Constructors
    Constructor Description
    BufferedOutputStream​(OutputStream out)
    Constructs a new BufferedOutputStream, providing out with a buffer of 8192 bytes.
    BufferedOutputStream​(OutputStream out, int size)
    Constructs a new BufferedOutputStream, providing out with size bytes of buffer.
  • Method Summary

    Modifier and Type Method Description
    void close()
    Closes this stream.
    void flush()
    Flushes this stream to ensure all pending data is written out to the target stream.
    void write​(byte[] buffer, int offset, int length)
    Writes count bytes from the byte array buffer starting at offset to this stream.
    void write​(int oneByte)
    Writes one byte to this stream.

    Methods inherited from class java.io.OutputStream

    write

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • buf

      protected byte[] buf
      The buffer containing the bytes to be written to the target stream.
    • count

      protected int count
      The total number of bytes inside the byte array buf.
  • Constructor Details

    • BufferedOutputStream

      public BufferedOutputStream​(OutputStream out)
      Constructs a new BufferedOutputStream, providing out with a buffer of 8192 bytes.
      Parameters:
      out - the OutputStream the buffer writes to.
    • BufferedOutputStream

      public BufferedOutputStream​(OutputStream out, int size)
      Constructs a new BufferedOutputStream, providing out with size bytes of buffer.
      Parameters:
      out - the OutputStream the buffer writes to.
      size - the size of buffer in bytes.
      Throws:
      IllegalArgumentException - if size <= 0.
  • Method Details

    • flush

      public void flush() throws IOException
      Flushes this stream to ensure all pending data is written out to the target stream. In addition, the target stream is flushed.
      Specified by:
      flush in interface Flushable
      Overrides:
      flush in class FilterOutputStream
      Throws:
      IOException - if an error occurs attempting to flush this stream.
    • write

      public void write​(byte[] buffer, int offset, int length) throws IOException
      Writes count bytes from the byte array buffer starting at offset to this stream. If there is room in the buffer to hold the bytes, they are copied in. If not, the buffered bytes plus the bytes in buffer are written to the target stream, the target is flushed, and the buffer is cleared.
      Overrides:
      write in class FilterOutputStream
      Parameters:
      buffer - the buffer to be written.
      offset - the start position in buffer from where to get bytes.
      length - the number of bytes from buffer to write to this stream.
      Throws:
      IndexOutOfBoundsException - if offset < 0 or length < 0, or if offset + length is greater than the size of buffer.
      IOException - if an error occurs attempting to write to this stream.
      NullPointerException - if buffer is null.
      ArrayIndexOutOfBoundsException - If offset or count is outside of bounds.
    • close

      public void close() throws IOException
      Description copied from class: FilterOutputStream
      Closes this stream. This implementation closes the target stream.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class FilterOutputStream
      Throws:
      IOException - if an error occurs attempting to close this stream.
    • write

      public void write​(int oneByte) throws IOException
      Writes one byte to this stream. Only the low order byte of the integer oneByte is written. If there is room in the buffer, the byte is copied into the buffer and the count incremented. Otherwise, the buffer plus oneByte are written to the target stream, the target is flushed, and the buffer is reset.
      Overrides:
      write in class FilterOutputStream
      Parameters:
      oneByte - the byte to be written.
      Throws:
      IOException - if an error occurs attempting to write to this stream.