Open In App

Java.io.StringBufferInputStream Class in Java

Improve
Improve
Like Article
Like
Save
Share
Report

io.StringBufferInputStream class in Java

java.io.StringBufferInputStream class helps in creating an Input Stream where, one can read bytes from the string. If we use this class we can only read lower 8 bits of each character present in the string.
But if we use ByteArrayInputStream, there is no restriction of reading only lower 8 bits of each character present in the string.
This class has been deprecated by Oracle and should not be used any more.

Declaration:

public class StringBufferInputStream
   extends InputStream

Constructor :

  • StringBufferInputStream(String str) : Creates a string input stream to read data from the specified string.

Methods:

  • read() : java.io.StringBufferInputStream.read() reads bytes of data from the Input Stream and returns -1 if end of the Stream is reached.
    Syntax :

    public int read()
    Parameters : 
    -----------
    Return  :
    Returns read character as an integer ranging from range 0 to 65535.
    -1 : when end of file is reached.
  • read(byte[] buffer, int offset, int maxlen) : java.io.StringBufferInputStream.read(byte[] buffer, int offset, int maxlen)) reads bytes of data from the buffer starting at offset position up to maxlen and returns -1 if end of the Stream is reached.
    Syntax :

    public int read(byte[] buffer, int offset, int maxlen))
    Parameters : 
    buffer : destination buffer to be read into  
    offset : starting position from where to store characters
    maxlen : maximum no. of characters to be read
    Return  :
    Returns all the characters read
    -1 : when end of file is reached.
  • reset() : java.io.StringBufferInputStream.reset() resets the Input Stream and starts reading from the first character of ‘buffer’ present in the Input Stream.
    Syntax :

    public void reset()
    Parameters : 
    -----------
    Return  :
    void
    
  • skip(long b) : java.io.StringBufferInputStream.skip(long b) skips ‘b’ bytes. Few bytes are skipped if end of file is reached.
    Syntax :

    public long skip(long b)
    Parameters : 
    b : no. of bytes to be skipped
    Return  :
    no. of bytes skipped
    
  • available() : java.io.StringBufferInputStream.available() tells the total no. of bytes available for reading.
    Syntax :

    public int available()
    Parameters : 
    ----------------
    Return  :
    total no. of bytes that can be read
    




// Java program illustrating the working of StringBufferInputStream class methods
// read(), skip(), available(), reset()
// read(char[] char_array, int offset, int maxlen)
  
import java.io.*;
public class NewClass
{
    public static void main(String[] args) throws IOException
    {
  
        String str1 = "Hello Geeks";
        String str2 = "GeeksForGeeks";
        StringBufferInputStream Geek_buffer1 = new StringBufferInputStream(str1);
        StringBufferInputStream Geek_buffer2 = new StringBufferInputStream(str2);
  
        // USe of available() : to count total bytes to be read
        System.out.println("Use of available() 1 : "+ Geek_buffer1.available());
  
        int a = 0;
        System.out.print("Use of read() method : ");
          
        // Use of read() method : reading each byte one by one
        while((a = Geek_buffer1.read()) != -1)
        {
            // Converting byte to char
            char c1 = (char)a;
            System.out.println(c1);
  
            // Use of skip() method
            long char_no = Geek_buffer1.skip(1);
            System.out.println("Characters Skipped : "+ (c1+1));
  
        }
        System.out.println("");
  
        // USe of available() : to count total bytes to be read
        System.out.println("Use of available() 2 : "+ Geek_buffer2.available());
  
          
        byte[] buffer = new byte[15];
      
        // Use of read(char[] char_array, int offset, int maxlen):
        // reading a part of array
        Geek_buffer2.read(buffer, 1, 2);
        int b = 0;
  
        System.out.print("read(char[] char_array, int offset, int maxlen): ");
        while((b = Geek_buffer2.read()) != -1)
        {
            char c2 = (char)b;
            System.out.print(c2);
        }
        System.out.println("");
  
        // Use of reset() : to reset str1 for reading again
        Geek_buffer1.reset();
        int i = 0;
        System.out.print("\nUse of read() method again after reset() : ");
          
        // Use of read() method : reading each character one by one
        while((i = Geek_buffer1.read()) != -1)
        {
            char c3 = (char)i;
            System.out.print(c3);
        }
    }
}


Output :

Use of available() 1 : 11
Use of read() method : H
Characters Skipped : 73
l
Characters Skipped : 109
o
Characters Skipped : 112
G
Characters Skipped : 72
e
Characters Skipped : 102
s
Characters Skipped : 116

Use of available() 2 : 13
Use of read(char[] char_array, int offset, int maxlen) method : eksForGeeks

Use of read() method again after reset() : Hello Geeks

.



Last Updated : 26 Jan, 2017
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads