digitization, transformation, binary

Class BufferedOutputStream in Java

Class BufferedOutputStream in Java

BufferedOutputStream 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 to call to the underlying system for each byte written. The data is written into a buffer and then written to the underlying system. If the buffer reaches its capacity, the buffer output stream is closed or the buffer stream is explicitly flushed.

A BufferedOutputStream object provides a more efficient way to write just a few bytes at a time to an OutputStream. BufferedOutputStream objects use a buffer to store output for an associated OutputStream.

In other words, a large number of bytes are stored in an internal buffer and only written when the buffer fills up or is explicitly flushed. A BufferedOutputStream is more efficient than a regular OutputStream because the data is written to memory, rather than a disk or a network.

Minimizing the number of write operations to a disk or the network minimizes the cumulative overhead for these operations.

We should wrap a BufferedOutputStream around any OutputStream whose write() operations may be time-consuming or costly, such as a FileOutputStream.

The structure of the class BufferedOutputStream is given as

public class java.io.BufferedOutputStream extends java.io.FilterOutputStream{
//member elements
protected byte buf[];//A buffer array of bytes where data is stored
protected int count;//the number of valid bytes in the buffer
//Constructors
public BufferedOutputStrea(OutputStream out);//creates a new buffered output stream to write data 
//to the specified underlying output stream with default 512 byte buffer size.
public BufferedOutputStrea(OutputStream out,int size);//creates a new buffered output stream to write data 
//to the specified underlying output stream with the specified buffer size.
//Methods:
public synchronized void flush()throws IOException;
public synchronized void write(byte b[],int offSet,int length)throws IOException;
public synchronized void write(int b)throws IOException;
}
 

The details of the class structure are given as follows:

protected byte[] buf;

protected byte[] buf is the buffer that stores the data for the output stream.

protected int count;

protected int count is the current position in the buffer.

public BufferedOutputStream(OutputStream out);

public BufferedOutputStream(OutputStream out) constructor creates a BufferedOutputStream that acts on the specified OutputStream, using a buffer with the default size of 512 bytes.

Parameter
out – The output stream to buffer.

public BufferedOutputStream(OutputStream out, int size);

public BufferedOutputStream(OutputStream out, int size) constructor creates a BufferedOutputStream that acts on the specified OutputStream, using a buffer that is size bytes long.

Parameter
out – The output stream to buffer.
size – The size of buffer to use.

public synchronized void flush();

public synchronized void flush() method writes the contents of the buffer to the underlying output stream. It is called automatically when the buffer fills up. You can also call it before the buffer is full. This is known as “flushing” the buffer. This method blocks until the underlying write() is complete.

public synchronized void write(int b);

public synchronized void write(int b) method places a byte containing the low-order eight bits of the given integer into the buffer. If the buffer is full, it is flushed, and the value b is placed in the newly empty buffer. If the buffer is flushed, this method blocks until flush() returns; otherwise, this method returns immediately.

Parameter
b – The value to write.

public synchronized void write(byte[] b, int off, int len);

public synchronized void write(byte[] b, int off, int len) method copies len bytes from b, starting at off, into the buffer. If there is enough space left in the buffer for the new data, it is copied into the buffer and the method returns immediately. Otherwise, the buffer is flushed, and the new data is written directly to the underlying stream. This is subtly different from the
behavior of write(int), which places new data in the buffer after a flush().

Parameter
b – An array of bytes to write to the stream.
off – An offset into the byte array.
len – The number of bytes to write.

Apart from these BufferedOutputStream class also has inherited methods from class- Object. They are as follows:

  • clone()
  • finalize()
  • hashCode()
  • notifyAll()
  • wait()
  • wait(long, int)
  • equals(Object)
  • getClass()
  • notify()
  • toString()
  • wait(long)

BufferedOutputStream class also has inherited methods from FilterOutputStream. They are as follows:

  • write(byte[])
  • close()

Share and Enjoy !

Leave a Comment

Your email address will not be published. Required fields are marked *