GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection
  • Introduction3 min read
  • Variables2 min read
  • Data Types7 min read
  • Operators7 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — Java.io.BufferedInputStream class in Java - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection

Java.io.BufferedInputStream class in Java

Last Updated : 23 Jul, 2025
A BufferedInputStream adds functionality to another input stream-namely, the ability to buffer the input and to support the mark and reset methods. When the BufferedInputStream is created, an internal buffer array is created. As bytes from the stream are read or skipped, the internal buffer is refilled as necessary from the contained input stream, many bytes at a time. Constructor and 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.
Methods:
  • int available() : Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream.
    Syntax:public int available()
                  throws IOException
    Returns:
    an estimate of the number of bytes that can be 
    read (or skipped over) from this input stream without blocking.
    Throws:
    IOException
    
  • void close() : Closes this input stream and releases any system resources associated with the stream.
    Syntax:public void close()
               throws IOException
    Overrides:
    close in class FilterInputStream
    Throws:
    IOException 
    
  • void mark(int readlimit) : Marks the current position in this input stream.
    Syntax:public void mark(int readlimit)
    Overrides:
    mark in class FilterInputStream
    Parameters:
    readlimit - the maximum limit of bytes that can be read 
    before the mark position becomes invalid.
    
  • boolean markSupported() : Tests if this input stream supports the mark and reset methods.
    Syntax:public boolean markSupported()
    Overrides:
    markSupported in class FilterInputStream
    Returns:
    a boolean indicating if this stream type supports the mark and reset methods.
    
  • int read() : Reads the next byte of data from the input stream.
    Syntax:public int read()
             throws IOException
    Returns:
    the next byte of data, or -1 if the end of the stream is reached.
    Throws:
    IOException 
    
  • 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.
    Syntax:public int read(byte[] b,
           int off,
           int len)
             throws IOException
    Parameters:
    b - destination buffer.
    off - offset at which to start storing bytes.
    len - maximum number of bytes to read.
    Returns:
    the number of bytes read, or -1 if the end of the stream has been reached.
    Throws:
    IOException 
    
  • void reset() : Repositions this stream to the position at the time the mark method was last called on this input stream.
    Syntax:public void reset()
               throws IOException
    Overrides:
    reset in class FilterInputStream
    Throws:
    IOException
    
  • long skip(long n) :Skips over and discards n bytes of data from this input stream
    Syntax:public long skip(long n)
              throws IOException
    Parameters:
    n - the number of bytes to be skipped.
    Returns:
    the actual number of bytes skipped.
    Throws:
    IOException
Program: Java
// Java program to demonstrate working of BufferedInputStream
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;

// Java program to demonstrate BufferedInputStream methods
class BufferedInputStreamDemo
{
    public static void main(String args[]) throws IOException
    {
        // attach the file to FileInputStream
        FileInputStream fin = new FileInputStream("file1.txt");

        BufferedInputStream bin = new BufferedInputStream(fin);

        // illustrating available method
        System.out.println("Number of remaining bytes:" +
                                            bin.available());

        // illustrating markSupported() and mark() method
        boolean b=bin.markSupported();
        if (b)
            bin.mark(bin.available());

        // illustrating skip method
        /*Original File content:
        * This is my first line
        * This is my second line*/
        bin.skip(4);
        System.out.println("FileContents :");

        // read characters from FileInputStream and
        // write them
        int ch;
        while ((ch=bin.read()) != -1)
            System.out.print((char)ch);

        // illustrating reset() method
        bin.reset();
        while ((ch=bin.read()) != -1)
            System.out.print((char)ch);

        // close the file
        fin.close();
    }
}
Output:
Number of remaining bytes:47
FileContents :
 is my first line
This is my second line
This is my first line
This is my second line
Next Article: Java.io.BufferedOutputStream class in Java
Comment

Explore

    Basics
    • Introduction3 min read
    • Variables2 min read
    • Data Types7 min read
    • Operators7 min read
    • Decision Making5 min read
    • Methods7 min read
    • Arrays7 min read
    • Strings5 min read
    OOP & Interface
    • Introduction6 min read
    • Constructors6 min read
    • Encapsulation3 min read
    • Inheritance8 min read
    • Polymorphism5 min read
    • Abstraction5 min read
    • Interfaces8 min read
    Collections
    • Collection class9 min read
    • Collection Interface5 min read
    • List Interface8 min read
    • Set Interface5 min read
    • Queue Interface5 min read
    • Map Interface in Java6 min read
    • Iterator4 min read
    Advance Java Concepts
    • Exception handling6 min read
    • Java Multithreading Tutorial3 min read
    • Synchronization in Java9 min read
    • File handling6 min read
    • Method References3 min read
    • Java 8 Stream Tutorial7 min read
    • Memory Management4 min read
    • Garbage Collection in Java6 min read
    • Networking6 min read
    • JDBC5 min read
    Practice Java
    • Interview Questions15+ min read
    • Programs6 min read
    • Practice Problems5 min read
    • Quiz1 min read
    • Project Ideas15+ min read
    Courses
    • Java Backend Course2 min read
    • Placement 360 Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.