Java.io - SPLessons

Java.io BufferedInputStream

Chapter 2

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java.io BufferedInputStream

Java.io BufferedInputStream

shape Introduction

The buffered input stream is the class used to reading the bytes into internal buffer and then to the directory or file. The Java.io BufferedInputStream performance is little bit faster compared to other readers like reading the files into drives and in many more applications.

shape Description

The buffered output stream is similar to that buffered writer with tiny difference that it writes stream of bytes like data related to images, where as the buffered reads the characters like in the text form. The main feature of this class is to read block of characters or data at a time instead of reading data character by character like input stream.

shape Conceptual
figure

Class Declaration

shape Declaration

Java.io BufferedInputStream class is declared below:
Public class BufferedInputStream extends FilterInputStream

shape Fields

Java.io/BufferedInputStream class fields are:

  • Protected InputStream In : The field represents to be filter the input stream.
  • Protected int Count : The field represents to the index to count input stream bytes after the last byte.
  • Protected byte [] bu : The field represents to the internal buffer where actually the data  stored.

Class Constructors

shape Table

Constructor Description
BufferedInputStream (InputStream in) The function of this constructor is to create the buffered input stream and reads into the internal buffer.
BufferedInputStream (InputStream in, int n) The function of this constructor is to create the buffered input stream with represented size of buffer.

Class Methods

shape Table

Method Description
void close () The function of this method is to close the present stream and associated to input stream systems resources are released.
Long skip (long m) The function of this method is to skip the text or characters in this stream of ‘ m ‘ bytes.
Boolean markSupported () The function of this method is to check the represented mark method is supported by this stream or not
int read(byte[] b, int off, int n) By using this method it reads the data up to ‘n’ bytes from the input stream into an array of bytes.

Inherited Methods

shape Description

From the following classes, methods are inherited the buffered input stream class.

  • Java.io.Object
  • Java.io.FileInputStream

shape Examples

Usage of void close() method.

package com.cp.io;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
public class BufferedInputStream
{
   public BufferedInputStream(InputStream inS) {
		// TODO Auto-generated constructor stub
	}
public static void main(String[] args) throws Exception 
   {
      InputStream inS = null;
      BufferedInputStream bis = null;
      try{
         // for reading purpose open input stream test.txt
         inS = new FileInputStream("c:/test.txt");
         // To convert input stream iinto buffered input stream
         bis = new BufferedInputStream(inS);      
         // invoke available
         int byteNum = bis.available();            
         // Printing the number of available bytes
         System.out.println(byteNum);           
         // releases all the system resources associated with this stream
         bis.close();           
         // It throws io exception on available() invocation
         byteNum = bis.available();
         System.out.println(byteNum);        
      } catch (IOException e) 
      {             
         // if exception occurred.
         System.out.println("Error: 'bis' is closed");
      }finally
      { 
         // releases all the system resources associated with this stream
         if(inS!=null)
            inS.close();
      }
   }
private void close() {
	// TODO Auto-generated method stub	
}
private int available() {
	// TODO Auto-generated method stub
	return 0;
}
}

Output
The result will be as follows.

Error: 'bis' is closed

Summary

shape Key Points

  • The Java.io BufferedInputStream is the class used to reading the bytes into internal buffer and then to the directory or file.
  • The Java.io BufferedInputStream is little bit faster compared to other readers like reading the files into drives and in many more applications.
  • The main feature of this class is to read block of characters or data at a time instead of reading data character by character like input stream.