Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Java.io.BufferedInputStream class in Java
Next article icon

Java.io.BufferedInputStream class in Java

Last Updated : 12 Sep, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report
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

Next Article
Java.io.BufferedInputStream class in Java

N

Nishant Sharma
Improve
Article Tags :
  • Java
  • Java-I/O
  • Java-BufferedInputStream
Practice Tags :
  • Java

Similar Reads

    Java.io.ByteArrayInputStream class in Java
    ByteArrayInputStream class of java.io package contains all the buffers, containing bytes to be read from the Input Stream. There is no IO exception in case of ByteArrayInputStream class methods. Methods of this class can be called even after closing the Stream, there is no effect of it on the class
    3 min read
    Java.io.ByteArrayOutputStream() Class in Java
    java.io.ByteArrayOutputStream class creates an Output Stream for writing data into byte array. The size of buffer grows automatically as data is written to it. There is no affect of closing the byteArrayOutputStream on the working of it's methods. They can be called even after closing the class. Thu
    4 min read
    Java BufferedOutputStream Class
    BufferedOutputStream class in Java is a part of the java.io package. It improves the efficiency of writing data to an output stream by buffering the data. This reduces the number of direct writes to the underlying output stream, making the process faster and more efficient.Example 1: The below Java
    2 min read
    Java.io.BufferedWriter class methods in Java
    Bufferreader class writes text to character-output stream, buffering characters.Thus, providing efficient writing of single array, character and strings. A buffer size needs to be specified, if not it takes Default value. An output is immediately set to the underlying character or byte stream by the
    5 min read
    Java.io.FilterInputStream Class in Java
    Filter Streams filters data as they read and write data in the Input Stream, filters it and pass it on to the underlying Streams. Filter Streams are FilterInputStream FilterOutput Stream FilterInputStream : Java.io.FilterInputStream class works almost like InputStream class in Java but what it does
    9 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences