# Class BufferedOutputStream

java.lang.Object
java.io.OutputStream
com.codename1.io.BufferedOutputStream

All Implemented Interfaces:
  `Closeable, Flushable, AutoCloseable`

Direct Known Subclasses:
  `TarOutputStream`

public class BufferedOutputStream extends OutputStream
Based on the buffered output stream from the JDK with some minor tweaks to allow external classes to monitor stream status and progress.

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `protected byte[]`
  `buf`
  The internal buffer where data is stored.
  `protected int`
  `count`
  The number of valid bytes in the buffer.

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `BufferedOutputStream(OutputStream out)`
  Creates a new buffered output stream to write data to the specified underlying output stream.
  `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.
  `BufferedOutputStream(OutputStream out, int size, String name)`
  Creates a new buffered output stream to write data to the specified underlying output stream with the specified buffer size.
  `BufferedOutputStream(OutputStream out, String name)`
  Creates a new buffered output stream to write data to the specified underlying output stream.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `close()`
  Closes this output stream and releases any system resources associated with the stream.
  `void`
  `flush()`
  Flushes this buffered output stream.
  `void`
  `flushBuffer()`
  Flush the internal buffer
  `Object`
  `getConnection()`
  If applicable this member represents the connection object for the stream
  `static int`
  `getDefaultBufferSize()`
  The default size for a stream buffer
  `long`
  `getLastActivityTime()`
  Returns the time of the last activity
  `String`
  `getName()`
  Indicates the name of the stream for debugging purposes
  `int`
  `getTotalBytesWritten()`
  Returns the total amount of bytes written to this stream so far
  `void`
  `setConnection(Object connection)`
  If applicable this member represents the connection object for the stream
  `static void`
  `setDefaultBufferSize(int aDefaultBufferSize)`
  The default size for a stream buffer
  `void`
  `setProgressListener(IOProgressListener progressListener)`
  Sets the callback for IO updates from a buffered stream
  `void`
  `write(byte[] b)`
  Writes `b.length` bytes to this output stream.
  `void`
  `write(byte[] b, int off, int len)`
  Writes `len` bytes from the specified byte array starting at offset `off` to this buffered output stream.
  `void`
  `write(int b)`
  Writes the specified byte to this buffered output stream.

  ### Methods inherited from class Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Field Details

  - ### buf

    protected byte[] buf
    The internal buffer where data is stored.

  - ### count

    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.

- ## Constructor Details

  - ### BufferedOutputStream

    public BufferedOutputStream(OutputStream out)

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

    ###### Parameters

    - `out`: the underlying output stream.

  - ### BufferedOutputStream

    public BufferedOutputStream(OutputStream out, String name)

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

    ###### Parameters

    - `out`: the underlying output stream.

    - `name`: the name of the stream used for debugging/logging purposes

  - ### BufferedOutputStream

    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`: the underlying output stream.

    - `size`: the buffer size.

    ###### Throws

    - `IllegalArgumentException`: if size <= 0.

  - ### BufferedOutputStream

    public BufferedOutputStream(OutputStream out, int size, String name)

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

    ###### Parameters

    - `out`: the underlying output stream.

    - `size`: the buffer size.

    - `name`: the name of the stream used for debugging/logging purposes

    ###### Throws

    - `IllegalArgumentException`: if size <= 0.

- ## Method Details

  - ### getDefaultBufferSize

    public static int getDefaultBufferSize()

    The default size for a stream buffer

    ###### Returns

    the defaultBufferSize

  - ### setDefaultBufferSize

    public static void setDefaultBufferSize(int aDefaultBufferSize)

    The default size for a stream buffer

    ###### Parameters

    - `aDefaultBufferSize`: the defaultBufferSize to set

  - ### getName

    public String getName()

    Indicates the name of the stream for debugging purposes

    ###### Returns

    the name of the stream

  - ### flushBuffer

    public void flushBuffer() throws IOException
    Flush the internal buffer
    Throws:
      `IOException`

  - ### write

    public void write(int b) throws IOException

    Writes the specified byte to this buffered output stream.

    ###### Parameters

    - `b`: the byte to be written.

    ###### Throws

    - `IOException`: if an I/O error occurs.
    Specified by:
      `write` in class `OutputStream`
    Throws:
      `IOException`

  - ### write

    public void write(byte[] b, int off, int len) throws IOException

    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 `BufferedOutputStream`s will not copy data unnecessarily.

    ###### Parameters

    - `b`: the data.

    - `off`: the start offset in the data.

    - `len`: the number of bytes to write.

    ###### Throws

    - `IOException`: if an I/O error occurs.
    Overrides:
      `write` in class `OutputStream`
    Throws:
      `IOException`

  - ### flush

    public void flush() throws IOException

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

    ###### Throws

    - `IOException`: if an I/O error occurs.
    Specified by:
      `flush` in interface `Flushable`
    Overrides:
      `flush` in class `OutputStream`
    Throws:
      `IOException`

  - ### getLastActivityTime

    public long getLastActivityTime()

    Returns the time of the last activity

    ###### Returns

    time of the last activity on this stream

  - ### getTotalBytesWritten

    public int getTotalBytesWritten()

    Returns the total amount of bytes written to this stream so far

    ###### Returns

    the total amount of bytes written to this stream so far

  - ### setProgressListener

    public void setProgressListener(IOProgressListener progressListener)

    Sets the callback for IO updates from a buffered stream

    ###### Parameters

    - `progressListener`: the progressListener to set

  - ### write

    public void write(byte[] b) throws IOException

    Writes `b.length` bytes to this output stream.

    The `write` method of `FilterOutputStream` calls its `write` method of three arguments with the arguments `b`, `0`, and `b.length`.

    Note that this method does not call the one-argument `write` method of its underlying stream with the single argument `b`.

    ###### Parameters

    - `b`: the data to be written.

    ###### Throws

    - `IOException`: if an I/O error occurs.
    Overrides:
      `write` in class `OutputStream`
    Throws:
      `IOException`

  - ### close

    public void close() throws IOException

    Closes this output stream and releases any system resources associated with the stream.

    The `close` method of `FilterOutputStream` calls its `flush` method, and then calls the `close` method of its underlying output stream.

    ###### Throws

    - `IOException`: if an I/O error occurs.
    Specified by:
      `close` in interface `AutoCloseable`
    Specified by:
      `close` in interface `Closeable`
    Overrides:
      `close` in class `OutputStream`
    Throws:
      `IOException`

  - ### getConnection

    public Object getConnection()

    If applicable this member represents the connection object for the stream

    ###### Returns

    the connection

  - ### setConnection

    public void setConnection(Object connection)

    If applicable this member represents the connection object for the stream

    ###### Parameters

    - `connection`: the connection to set