Class StringBufferInputStream in Java

digitization, transformation, binary

StringBufferInputStream in Java:

StringBufferInputStream class allows an application to create an input stream where bytes read are supplied by the contents of a String. The application can also read bytes from a byte array by using a ByteArrayInputStream. So this stream converts a String into an Input Stream.

StringBufferInputStream class is a subclass of InputStream in which input bytes come from the characters of a specified string object.

Ads code goes here

The StringBufferInputStream class represents a byte stream whose data source is a String. This class is similar to the ByteArrayInputStream class, which uses a byte array as its data source.

The String, however, uses a StringBuffer to implement StringBufferInoutStream.Only the low eight bits of each character in the string is used by this class. We need to connect StringBufferInputStream to a FilterInputStream object to provide a user interface.

StringBufferInputStream is deprecated now because it does not correctly convert characters
to bytes. The StringReader class should now be used to create a character stream from a String.

The structure of the class StringBufferInputStream is given as:

public class java.io.StringBufferInputStream extends java.io.InputStream{
protected String buffer;//The string from which bytes are read.
protected int count;//the number of valid characters in the input stream buffer
protect int pos;//the index of the next characters to read data from specified input stream buffer.
//constructor
public StringBufferInputStream(String s);
//creates a StringBufferInputStream object to read data from specified String.
//methods:
public synchronized int available(); // Overrides InputStream
public synchronized int read(); //Defines InputStream
public synchronized int read(byte b[],int offSet,int length);// Overrides InputStream
public synchronized void reset();// Overrides InputStream
public synchronized long skip(long n);// Overrides InputStream
} 

The details of the class structure are given as follows:

protected String buffer;

protected String buffer represents a buffer that stores the data for the input stream.

protected int count;

protected int count represents the size of the buffer, or in other words, the length of the string.

protected int pos;

protected int pos represents the current stream position.

public StringBufferInputStream(String s);

public StringBufferInputStream(String s) constructor creates a StringBufferInputStream that uses the given String as its data source. Note that the data is not copied, so changes made to the String affect the data that the StringBufferInputStream returns.

READ  Hire Java Builders With Working experience in Top 10 Java Frameworks

Parameter
s – The String to use.

public synchronized int available();

public synchronized int available() method returns the number of bytes that are left in the string. This is the length of the string, count, minus the current stream position, pos.

This method returns the number of bytes remaining in the string.

public synchronized int read();

public synchronized int read() method returns the next byte from the string. The method takes the next character from the string and returns the low eight bits of that character as a byte, which is not the correct way to convert characters into bytes. The method cannot block.

This method returns the next byte of data or -1 if the end of the string is encountered.

public synchronized int read(byte[] b, int off, int len);

public synchronized int read(byte[] b, int off, int len) method copies bytes from the internal buffer into the given array b, starting at index off and continuing for len bytes. The method takes each character from the string and returns the low eight bits of that character as a byte, which is not the correct way to convert characters into bytes.

This method returns the actual number of bytes read or -1 if the end of the string is encountered immediately.

Parameter
b – An array of bytes to be filled from the stream.
off – An offset into the byte array.
len – The number of bytes to read.

public synchronized void reset();

public synchronized void reset() method sets the position of the StringBufferInputStream back to the beginning of the internal buffer.

public synchronized long skip(long n);

public synchronized long skip(long n) method skips n bytes of the string. If you try to skip past the end of the string, the stream is positioned at the end of the string.

READ  Class OutputStream in Java

This method returns actual number of bytes skipped.
Parameter
n – The number of bytes to skip.

Apart from these StringBufferInputStream class also has inherited methods from class- Object. They are as follows:

  • clone()
  • finalize()
  • hashCode()
  • notifyAll()
  • wait()
  • wait(long, int)
  • equals(Object)
  • getClass()
  • notify()
  • toString()
  • wait(long)

StringBufferInputStream class also has inherited methods from InputStream. They are as follows:

  • mark(int)
  • read(byte[])
  • close()
  • markSupported()

Note: This class does not correctly convert the characters of a StringBuffer into bytes and is deprecated in Java 1. 1. Instead, we need to use StringReader to correctly convert characters into bytes, or use ByteArrayInputStream to read bytes from an array of bytes.

Share and Enjoy !

Leave a Comment

Your email address will not be published. Required fields are marked *