Topics Index

Input/Output: Exploring > Byte Streams >
Siva Nookala - 20 Feb 2017
BufferedInputStream helps in increasing the performance while reading less number of bytes from a resource. Unlike BufferedInputStream if we use any other InputStream each time when we perform read operation we generate a system call to the operating system which in turn executes many other system instructions. BufferedInputStream internally has a buffer to store the bytes and it stores all the bytes in to it so we no need to generate frequent system calls to read from the resource and also unlike other InputStreams BufferedInputStream reads many bytes at a time.

public class BufferedInputStream
extends FilterInputStream
Constructors :
Constructor Description
BufferedInputStream(InputStream in) Creates a BufferedInputStream and saves its argument, the input stream in, for later use.
BufferedInputStream(InputStream in, int size) Creates a BufferedInputStream with the specified buffer size, and saves its argument, the input stream in, for later use.
Important Methods :
Method Description
int available() Returns an estimate of the number of bytes that can be read (or skipped over) from the input stream without blocking by the next invocation of a method for this input stream.
int read() Reads the next byte of data from the input stream.
boolean markSupported() Tests if the input stream supports the mark and reset methods.
void mark(int readlimit) Marks the position. reamlimit is the number of bytes to be remembered after marked position.
void reset() Repositions the stream to the position at the time the mark method was last called on this input stream.
void close() This method closes the input stream and releases any system resources associated with the stream.
class BufferedInputStreamDemo
    public static void main(String arg[])
        File f1 = new File("E:JavaPrograms\\FolderOne\\fileOne.txt");
        //LINE A
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f1));
        System.out.println("Available Bytes : " + bis.available()); //LINE B
        System.out.println("MarkSuppoted : " + bis.markSupported()); //LINE C
        // reading bytes from BufferedInputStream
        // marking position
        bis.mark(2); // LINE D
        // reading bytes from BufferedInputStream
        System.out.println("reset method invoked.");
        bis.reset(); // LINE E
        int c = 0;
        while((c = != -1)
            System.out.print((char) c);

Available Bytes : 12
MarkSuppoted : true
reset method invoked.


At LINE A we created a BufferedInputStream object.
At LINE B we checking for the available bytes. Available bytes will be zero if the file is empty.
At LINE C we are checking whether the file is markSupported.
At LINE D we are marking and the parameter 2 represents two positions to be remembered after mark postion.
At LINE E we are invoking reset method which in turns resets the bytes to the mark position.

  • Read more than two bytes before invoking reset method. The reset method will throw IOException. If the IOException is not thrown the method resets the bytes to the mark position. Remember the IOException is not guaranteed.
  • Comment LINE D and invoke reset method to see an IOException.
Rest Of Methods :
Method Description
int read(byte[] b, int off, int len) Reads bytes from this byte-input stream into the specified byte array, starting at the given offset.
long skip(long n) Skips over and discards n bytes of data from this input stream.

Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App