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.
public class StringBufferInputStream extends InputStream
- StringBufferInputStream(String str) : Creates a string input stream to read data from the specified string.
- read() : java.io.StringBufferInputStream.read() reads bytes of data from the Input Stream and returns -1 if end of the Stream is reached.
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.
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.
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.
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.
public int available() Parameters : ---------------- Return : total no. of bytes that can be read
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
This article is contributed by Mohit Gupta 🙂. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.