• Tech Travel Hub is your one stop, ultimate tour guide to all things tech, travel, visas and digital nomads
  • Home
  • Blog
    • Technology
      • General Tech
      • Blogging
      • Java
    • Lifestyle
      • General Lifestyle
    • Travel
    • Educational
    • Business
  • About
  • Contact
Menu
  • Home
  • Blog
    • Technology
      • General Tech
      • Blogging
      • Java
    • Lifestyle
      • General Lifestyle
    • Travel
    • Educational
    • Business
  • About
  • Contact
View Blog
April 13, 2023April 13, 2023

Class StringBufferInputStream in Java

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.

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.

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.

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.

Post navigation

Previous post
Next post

Umer Umer

Leave a Reply Cancel reply

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

Recent Posts

  • Simplified Concept on What Is Interface In Java: Detailed Facts In 5 Minutes
  • What is Integration Testing?
  • What is GEB-Groover Browser Functional Testing?
  • What is GEB- Detailed Facts in Just 5 Minutes
  • What is Extreme Programming(XP) Practice?

Categories

  • Adventure
  • Agile Methodology
  • All world Tour
  • Android Development
  • Ansible
  • App
  • App Development
  • Artificial Intelligence
  • AWS
  • Bitcoin
  • BlockChain
  • Blogging
  • Box Packing
  • Business
  • business
  • Business Communication
  • buying guide
  • C
  • car
  • Care
  • Certification
  • Certification
  • Chatbot
  • cinema
  • Cloud Tech
  • Content Marketing
  • Corporate
  • covid-19
  • crucible
  • Cryptocurrency
  • Database
  • DevOps
  • DevOps
  • Digital Marketing
  • Drawing
  • Dress
  • Educational
  • Employment
  • Entertainment
  • Exceptions In Java
  • Fashion
  • Festivals
  • Finance
  • Fitness
  • Food
  • Game
  • Gift ideas
  • Groovy
  • Health
  • Health
  • home
  • home improvement
  • HR
  • IllegalThreadStateException in Java
  • India Tour
  • Instagram
  • Ios
  • ISO
  • Java
  • Javascript
  • job
  • Kids Section
  • Kitchen
  • Law
  • Life Style
  • Lifestyle
  • Lifestyle Home Improvement
  • Main Stories
  • Management
  • Mattress
  • Motorable
  • Movie
  • nursing
  • Pendrive
  • Perspective
  • Pet
  • Popular
  • Programming
  • Python
  • Relationships
  • Selenium
  • SEO
  • Skin
  • SOAPUI
  • Social Media
  • Social Media Marketing
  • Software
  • software development
  • Software Development Model
  • Software Engineering
  • Software Testing
  • Sports
  • Startup
  • T Robot
  • TDD
  • Tech
  • Tech News
  • Tech News
  • Technology
  • Tool
  • Tour Guide
  • Tour Guide
  • Travel
  • UFT
  • Uncategorized
  • USB Cable
  • VPN
  • Web Development
  • Wordpress
  • Yoga
  • Youtube

Tags

Instagram Instagram Marketing Marketing SEO Thoptv App

Categories

  • Tech
  • Travel
  • Visas
  • Digital Nomad
  • Tech
  • Travel
  • Visas
  • Digital Nomad

Publishing

If you would like to publish on our site, please email admin@techtravelhub.com

Quick Links

  • Home
  • Blog
    • Technology
      • General Tech
      • Blogging
      • Java
    • Lifestyle
      • General Lifestyle
    • Travel
    • Educational
    • Business
  • About
  • Contact
  • Home
  • Blog
    • Technology
      • General Tech
      • Blogging
      • Java
    • Lifestyle
      • General Lifestyle
    • Travel
    • Educational
    • Business
  • About
  • Contact

Contact

Tech Travel Hub LLC

Address:
8 The Green STE A
Dover
Delaware, 19901

Phone: +1 302 956 9948