All About Class BufferedInputStream in Java

0
21
Spread the love
  • 2
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    2
    Shares

BufferedInputStream in Java
BufferedInputStream in Java

BufferedInputStream in Java:

BufferedInputStream(is a FilterInputStream) class implements a buffered input Stream.By setting up such an input stream an application can read bytes from a stream without necessarily causing a call to the underlying System (disk,network or other slow sources)for each byte read.It is more efficient way to read just a few bytes at at a time. The data is read by blocks into buffer.We can use this class to prevent a physical read every time we want more data. Hence a large chunk of bytes are read from the underlying InputStream. The data is stayed in a buffer.The constructor of BufferedInputStream takes InputStream and an optional buffer size to process the data.BufferedInputStream does not provide an interface but a requirement that a buffer be used.We need to attach this an interface object.All reading is done directly from the internal buffer hence the network or disk or file needs to accessed only when required to fill the buffer.

The structure of class BufferedInputStream is given below:


public class java.io.BufferedInputStream extends
             jva.io.FilterInputStream{
//Member Elements
protected byte buf[];//A buffer array of bytes where data is stored
protected int count;//The index is one greater than the index of the
// last valid byte in the buffer.It is a placeholder that marks the 
//end of the valid data in the buffer.
protected int markLimit;//The maximum read ahead allowed after a call
// to the mark method before subsequent calls to the reset method fails.
protected int markPos;//The value of the pos filed at the time of the 
//last mark method was called.
//The value of this field is -1 if there is no current mark.
protected int pos//the current position in the buffer. This is the index 
//of the next character to be read from the buffer array.
//Constructors
public BufferedInputStream(InputStream in)
//creates a new BufferedInputStream to read data from the specified 
//InputStream in with a default
// 2048 byte buffer size
public BufferedInputStream(InputStream in,int size)
//creates a new BufferedInputStream to read data from the specified 
//InputStream with the specified byte of buffer size
//Methods:
public synchronized int available() throws IOException;
//This method returns the number of bytes that can be read without 
//any wait or blocking.This value id calculated the sum of the number
// of bytes remaining in the buffer and the number returned as the 
//result of calling available() method on InputStream object.
public synchronized void mark(int readLimit);
//this method remembers the current position of the BufferedInputStream.
//readLimit is the number of bytes that can be read before the saved
//position. A call to reset() returns to the saved position and reread 
//the portion of the buffer.
public boolean markSupported();
//this method returns true if the class support mark() and reset() method.
public synchronized int read()throws IOException;
//This method returns the next byte from the buffer.Once the entire buffer
//is read, the buffer is filled again from the InputStream and next byte
//is returned.While filling up buffer operation takes place, the method is
//blocked until the buffer is filled.If it reaches to the EOF or end of file
//it returns -1.
public synchronized int read(byte buff[],int off,int len)throws IOException;
//This method copies the bytes from the internal buffer to the array-b,
//starting from index off till len length.Once the entire buffer
//is read, the buffer is filled again from the InputStream and next byte
//is returned.While filling up buffer operation takes place, the method is
//blocked until the buffer is filled.If it reaches to the EOF or end of file
//or an exception is encountered,it returns -1.
public synchronized void reset()throws IOException;
//This method sets the position of the BufferedInputStream to a previously 
//saved position by the call mark().The nxt call to read bytes will begin 
//from the saved postion.
public synchronized long skip(long l)throws IOException;
//This method skips n bytes of the input.If the new position of the stream 
//is still within the data contained in the buffer. The method returns immediately.
//incase skip() method of the underlying stream is called,next read() method
//forces the buffer to fill.
}
 

Apart from these this class also has the below inherited methods:
From Object class

  • clone()
  • getClass()
  • equals(Object obj);
  • finalize()
  • hashCode()
  • notify()
  • notifyAll()
  • wait()
  • wait(long timeput)
  • wait(long timeout,int nanos)
  • toString()
From FiltertInputStream class
  • close()
  • read(bute[] b)
Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  • 2
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    2
    Shares

LEAVE A REPLY

Please enter your comment!
Please enter your name here