BufferedInputStream & BufferedOutputStream in Java

BufferedInputStream

The BufferedInputStream provides the functionality to another input stream for faster reading of data. The BufferedInputStream class has the ability to buffer the input and to support the mark and reset methods. It extends the functionality of FileInputStream.

BufferedInputStream Constructors

BufferedInputStream (InputStream in)
creates the BuffersInputStream and save its arguments.

BufferedInputStream (InputStream in, int size)
Creates the BuffersInputStream with the specified buffer size and saves its arguments.

BufferedInputStream Methods

MethodDescription
public int available () throws IOExceptionReturns an estimate of the number of bytes that can be read from the input stream.
public void mark (int readlimit)Checks the general contract of the mark method of input stream.
public void reset () throws IOExceptionChecks the general contract of the reset method of input stream.
public boolean markSupported ()Tests if this input stream supports the input and marked methods.
public void close () throws IOExceptionClose the input stream and release the system resources.
public int read () throws IOExceptionRead the next byte of data from input stream.
public int read (byte[] b, int off, int len) throws IOExceptionRead the data from specified byte array, starting at the given offset.
public long skip (long n) throws IOExceptionDiscard or skip the n bytes of the data from input stream.

Example: Illustrating the use of BufferedInputStream

// BufferedInputStreamDemo.java
import java.io.*;
public class BufferedInputStreamDemo
{
    public static void main(String args[]) throws Exception
    {
       String str = "Application of BufferedInputStream";
       byte b[] = str.getBytes();
       System.out.println ("Length of String: "+b.length);
       System.out.print ("String is: ");
       boolean marked = false;
       ByteArrayInputStream ba = new ByteArrayInputStream(b);
       BufferedInputStream bi = new BufferedInputStream(ba);
       int c;
       while((c = bi.read()) != -1)
       {
           switch(c)  
           {
              case 'm':
              if(!marked)
              {
                 marked = true;
                 bi.mark(32);
                 System.out.print("M");
              }
              else
                System.out.print((char)c);
              break;
              case';':
              bi.reset();
              break;
              default:
             System.out.print((char)c);
           }
       }
   }
}

    
Output:
Length of String: 34
String is: Application of BufferedInputStreaM

BufferedOutputStream


The BufferedInputStream class uses a buffer to store the data. This stream provides the better performance on OutputStream. It extends the FileOutputStream class.

BufferedOutputStream Constructors

BufferedOutputStream(OutputStream out)
This constructor creates a new buffer with default size.

BufferedOutputStream(OutputStream out, int size)
It is used to create new buffered output stream with specified size.

BufferedOutputStream Methods

MethodDescription
public void write(int b) throws IOExceptionWrite specified byte of data to this buffered output stream.
public void write(byte b[], int offset, int len)Write the specified byte array of data from starting at offset.
public void flush() throws IOExceptionFlush the output stream.