Skip to content
geeksforgeeks
  • 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
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • 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.OutputStreamWriter Class
Next article icon

Java StringBufferInputStream Class

Last Updated : 21 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The StringBufferInoutStream class in Java allows us to create an input stream from a string, so that we can read bytes from it. It only works with the lower 8 bits of each character, It can not handle the full range of character values. Instead of using this class, the best approach is to use ByteArrayInputStream, which does not have this limitation, it can read the full range of character values.

Note: The StringBufferInputStream has been deprecated by Oracle.

Declaration of StringBufferInputStream Class

The Declaration of the StringBufferInputStream class is below:

public class StringBufferInputStream extends InputStream

Constructors in StringBufferInputStream

This class consists of one constructor, with the help of which we can create an object of this class.

1. StringBufferInputStream(String str): This constructor is used to create a string input stream to read data from a specified string.

Syntax:

StringBufferInputStream(String str)

Example:

Java
// Demonstrating the working  // of StringBufferInputStream(String str) import java.io.*;  public class Geeks{     public static void main(String[] args) {                  // Creating a String to be used with StringBufferInputStream         String str = "Hello, World!";          // Creating StringBufferInputStream from the String         StringBufferInputStream is = new StringBufferInputStream(str);          // Reading bytes from the StringBufferInputStream         int data;         try {             while ((data = is.read()) != -1) {                 // Print each byte as a character                 System.out.print((char) data);             }             is.close();         } catch (IOException e) {             e.printStackTrace();         }     } } 

Output
Hello, World!


Java StringBufferStream Methods

The image below demonstrates the methods of PipedWriter class.

io.StringBufferInputStream class in Java


Now, we are going to discuss about each method one by one in detail:

1. read(): This method is used to reads a byte of data from the input stream

Syntax:

public int read()

  • Parameter: This method does not take any parameter.
  • Return Type: This method returns the read character as an integer otherwise returns -1, when the end of the stream is reached.


2. read(byte[] buffer, int offset, int maxlen): This method is used to read a specific number of characters from a buffer.

Syntax:

public int read(byte[] buffer, int offset, int maxlen)

  • Parameter: This method takes three parameters, which are listed below:
    • buffer: It is an array to store characters
    • offset: It is the starting position from where to store character
    • maxlen: It is the maximum number of characters to read.
  • Return Type: This method returns the maximum number of character to read otherwise returns -1 when the end of the stream is reached.


3. reset(): This method is used to reset the stream and because of this the reading starts from the beginning.

Syntax:

public void reset()

  • Parameter: This method does not take any parameter.
  • Return Type: This method does not return anything.


4. skip(long n): This method is used to skip and ignore some characters in the input stream.

Syntax:

public long skip(long n)

  • Parameter: This method takes a single paramter n, which is the number of bytes to skip
  • Return Type: This method returns the actuall number of bytes skipped.


5. available(): This method tells how many characters are left to read in the input stream

Syntax:

public int available()

  • Parameter: This method does not take any parameter
  • Return Type: This method returns the number of characters that can be read from the input stream.


Java Program Illustrating the Working of StringBufferInputStream Class Methods

Now, in the below example we willl see the working of all the methods.

Example:

Java
// Demonstrating the working of  // read(), read(byte[] buffer, int offset, int maxlen), // reset(), skip(long n), available() import java.io.*;  public class Geeks {     public static void main(String[] args) throws IOException {                  String s1 = "Hello Geeks";         String s2 = "GeeksForGeeks";          StringBufferInputStream b1 = new StringBufferInputStream(s1);         StringBufferInputStream b2 = new StringBufferInputStream(s2);          // available()         System.out.println("Use of available() 1: " + b1.available());          int a;         System.out.println("Use of read() method:");         while ((a = b1.read()) != -1) {             char c = (char) a;             System.out.println(c);              // skip()             b1.skip(1);         }          System.out.println("Use of available() 2: " + b2.available());          byte[] byteBuffer = new byte[15];         b2.read(byteBuffer, 1, 2);          int b;         System.out.print("Remaining characters after partial read: ");         while ((b = b2.read()) != -1) {             System.out.print((char) b);         }          // reset()         b1.reset();         System.out.print("\nAfter reset(): ");         int i;         while ((i = b1.read()) != -1) {             System.out.print((char) i);         }     } } 

Output
Use of available() 1: 11 Use of read() method: H l o G e s Use of available() 2: 13 Remaining characters after partial read: eksForGeeks After reset(): Hello Geeks


Next Article
Java.io.OutputStreamWriter Class

M

Mohit Gupta
Improve
Article Tags :
  • Java
  • Java-I/O
Practice Tags :
  • Java

Similar Reads

  • StringBuffer Class in Java
    The StringBuffer class in Java represents a sequence of characters that can be modified, which means we can change the content of the StringBuffer without creating a new object every time. It represents a mutable sequence of characters. Features of StringBuffer ClassThe key features of StringBuffer
    11 min read
  • Java.io.LineNumberInputStream Class in Java
    java.io.LineNumberInputStream class is simply an extension of input stream providing a extra facility to keep the record of current line number. Line is a sequence of bytes ending with : '\r' i.e. a carriage return character or a newline character : '\n', or a linefeed character following the carria
    11 min read
  • Java.io.PipedInputStream class in Java
    Pipes in IO provides a link between two threads running in JVM at the same time. So, Pipes are used both as source or destination. PipedInputStream is also piped with PipedOutputStream. So, data can be written using PipedOutputStream and can be written using PipedInputStream.But, using both threads
    5 min read
  • Java.io.OutputStreamWriter Class
    In Java, OutputStreamWriter class connects character streams to byte streams. It encodes Characters into bytes using a specified charset. Declaration of Java OutputStreamWriter Class public class OutputStreamWriter extends WriterConstructors of OutputStreamWriter Class in JavaConstructors in OutputS
    5 min read
  • Java.io.PushbackInputStream class in Java
    Pushback is used on an input stream to allow a byte to be read and then returned (i.e, "pushed back") to the stream. The PushbackInputStream class implements this idea. It provides a mechanism "peek" at what is coming from an input stream without disrupting it. It extends FilterInputStream.Fields: p
    7 min read
  • Java FileInputStream Class
    FileInputStream class in Java is useful for reading data from a file in the form of a Java sequence of bytes. FileInputStream is meant for reading streams of raw bytes such as image data. For reading streams of characters, consider using FileReader. Example: FileInputStream class to read data from f
    4 min read
  • Java.io.OutputStream class in Java
    This abstract class is the superclass of all classes representing an output stream of bytes. An output stream accepts output bytes and sends them to some sink. Applications that need to define a subclass of OutputStream must always provide at least a method that writes one byte of output. Constructo
    2 min read
  • Java.io.StringWriter class in Java
    Java StringWriter class creates a string from the characters of the String Buffer stream. Methods of the StringWriter class in Java can also be called after closing the Stream as this will raise no IO Exception. Declaration in Java StringWriter Classpublic class StringWriter extends WriterConstructo
    6 min read
  • StringBuffer insert() in Java
    The StringBuffer.insert() method in Java allows us to insert a string representation of a given data type at a specified position in a StringBuffer. This method is useful when we need to modify a string at specific positions without creating a new string each time by making it more efficient than co
    4 min read
  • StringBuffer vs StringBuilder in Java
    Java provides three classes to represent the sequence of characters i.e. String, StringBuffer, and StringBuilder. A string is immutable in Java whereas, both StringBuffer and StringBuilder are mutable. This means they allow modifications to the character sequence without creating new objects. The ma
    4 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