Java BufferedInputStream Class with Examples

Another input stream gains functionality with the Java.io.BufferedInputStream class, which supports the mark and reset methods in addition to buffering input.

Explanation

When an input stream is wrapped (bufferized) into a buffered stream, it becomes a BufferedInputStream in Java, which is a concrete subclass of FilterInputStream and improves the speed and efficiency of read operations on the stream.

Note

  • When bytes in the stream were read or skipped, the internal buffer automatically filled up, many bytes at a time, from the contained input stream.
  • An internal buffer array is created during the creation of a BufferedInputStream.

Constructor

  • BufferedInputStream(InputStream in)
    Makes a BufferedInputStream and stores the input stream as its argument for later usage.
  • BufferedInputStream(InputStream in, int size)
    Saves its argument, the input stream in, for later usage and creates a BufferedInputStream with the given buffer size.

Method

            Method               Description
int available() The number of bytes from the input stream that can be used without being blocked is returned by this method
void close() Closes the current input stream and frees up any related system resources.
void mark(int readlimit) Indicates where you are right now in this input stream.
boolean markSupported() Checks whether the mark and reset methods are supported by this input stream
int read()  Opens the input stream and reads the subsequent byte of data
void reset()  This stream back to its original position when this input stream’s mark method was last called.
long skip(long n)  This input stream’s n bytes of data are ignored and discarded.

int available()

Syntax:

public int available()
              throws IOException

void close()

Syntax:

public void close()
           throws IOException

void mark(int readlimit)

Syntax:

public void mark(int readlimit)

boolean markSupported()
.
Syntax:

public boolean markSupported()

int read()

Syntax:

public int read()
         throws IOException

void reset()

Syntax:

public void reset()
           throws IOException

long skip(long n) .

Syntax:

public long skip(long n)
          throws IOException

Declaration:

public class BufferedInputStream extends

Example

import java.io.BufferedInputStream;
import java.io.FileInputStream;
public class TechVidvan{
public static void main(String[] args)
{
 try
 {
   FileInputStream fi= new FileInputStream("D:\\myfiles.txt");
  
   BufferedInputStream b= new BufferedInputStream(fi);
 
   System.out.println("Available bytes at  beginning: " + b.available());  
 
   b.read();
   b.read();
   b.read();
 
   System.out.println("Available bytes at  end: " + b.available());
    b.close();  
}
catch(Exception e)
 {
   System.out.println(e);
 }
 }
}

Output
Available bytes at beginning: 25
Available bytes at end: 22

Example

import java.io.BufferedInputStream;
import java.io.FileInputStream;

public class TechVidvan{
public static void main(String[] args)
{
 try
 {
   FileInputStream f= new FileInputStream("D:\\myfile.txt");
  
   BufferedInputStream bis = new BufferedInputStream(f);
    bis.skip(5);

   System.out.println("Input stream after skipping first 5 bytes:");

   int i = 0;
   while ((i = bis.read()) != -1)
 {
    System.out.print((char) i);
   }
    bis.close();  
}
catch(Exception e)
 {
   System.out.println(e);
 }
 }
}

myfile.txt
TechVidvan

Output
Input stream after skipping first 5 bytes:
idvan

Example

import java.io.*;  
public class TechVidvan{    
 public static void main(String args[])
{    
  try
{    
    FileInputStream fin=new FileInputStream("D:\\testout.txt");    
    BufferedInputStream bin=new BufferedInputStream(fin);    
    int i;    
    while((i=bin.read())!=-1)
{    
     System.out.print((char)i);    
    }    
    bin.close();    
    fin.close();    
  }
catch(Exception e)

{
System.out.println(e);
}    
 }    
}

testout.txt
I am Learning java programming course in TechVidvan.
Output
I am Learning java programming course in TechVidvan.

Conclusion

The superclass of Input operations, the input stream is utilized to read user input from a file, keyboard, etc. It is employed to depict a byte-stream input. It is descended from Reader, an abstract class. In order to reduce the amount of Input operations and facilitate quicker and more efficient reading, it keeps an internal character buffer full.