Open In App

Java.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 :



Methods:




// 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

.


Article Tags :