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.
Creates the BuffersInputStream with the specified buffer size and saves its arguments.
Method | Description |
---|
public int available () throws IOException | Returns 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 IOException | Checks 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 IOException | Close the input stream and release the system resources. |
public int read () throws IOException | Read the next byte of data from input stream. |
public int read (byte[] b, int off, int len) throws IOException | Read the data from specified byte array, starting at the given offset. |
public long skip (long n) throws IOException | Discard 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);
}
}
}
}
The BufferedInputStream class uses a buffer to store the data. This stream provides the better performance on OutputStream. It extends the FileOutputStream class.
It is used to create new buffered output stream with specified size.
Method | Description |
---|
public void write(int b) throws IOException | Write 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 IOException | Flush the output stream. |