A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/java/java-stringbufferinputstream-class/ below:

Java StringBufferInputStream Class - GeeksforGeeks

Java StringBufferInputStream Class

Last Updated : 21 Apr, 2025

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();
        }
    }
}
Java StringBufferStream Methods

The image below demonstrates the methods of PipedWriter class.

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()

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)

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

Syntax:

public void reset()

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

Syntax:

public long skip(long n)

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

Syntax:

public int available()


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


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4