Class ByteArrayInputStream in Java

digitization, transformation, binary

ByteArrayInputStream in Java:

ByteArrayInputStream allows an application to create an input stream where the bytes read are supplied by the contents of a Byte Array.ByteArrayInputStream is a subclass of InputStream where the data comes from a specified array of bytes values.

The data may come from a file, a pipe or a socket. In other words, this class allows a buffer in the memory to be used as an InputStream. The constructor takes the buffer as an argument from which it extracts the bytes Application can also read bytes from a String by using a StringBufferInputStream.

Ads code goes here

We need to connect ByteArrayInputStream to a FilterInputStream object to provide a useful interface.

A ByteArrayInputStream is a stream whose data comes from a byte array. None of the methods of this class throw an IOException because the data comes from an array instead of an actual I/O device.

This class does not support the ability to mark a position in the stream. A call to reset(), however, does position the stream at the beginning of the byte array.

The position of the end of the stream depends on the constructor used. If the ByteArrayInputStream(byte[] buf) constructor is used, the end of the stream is the end of the byte array. If the ByteArrayInputStream(byte[] buf, int offset, int length) constructor is used, the end of the stream is reached at the index given by offset+length.

The structure of the class ByteArrayInputStream is given as:

public class java.io.ByteArrayInputStream extends java.io.InputStream{
//member elements:
protected byte buff[];//the buffer consists of array of bytes containing the data.
protect int count;//The index is greater than the last valid character in the input stream buffer.
protected int pos;//the index of the next character to read from the input stream buffer.
//Constructor
public ByteArrayInputStream(byte buf[]);
//creates a new byte array input stream which reads data from the specified byte array.
//The byte array is not copied
public ByteArrayInputStream(byte buf[],int offSet,int length);
//creates a new byte array input stream which reads data from the specified byte array upto
//length characters are to be read from the byte array starting at the indicated offSet.
//Methods:
public synchronized int available();
public synchronized int read();
public synchronized int read(byte b[],int offSet,int length);
public synchronized void reset();
public synchronized long skip(long l);
} 

The details of the class structure are given as follows:

protected byte[] buf;

protected byte[] buf is the buffer represented by this stream.

protected int count;

protected int count is a placeholder that marks the end of the data this ByteArrayInputStream represents.

protected int pos;

protected int pos is the current position in the buffer.

public ByteArrayInputStream(byte[] buf);

public ByteArrayInputStream(byte[] buf) constructor creates a ByteArrayInputStream object that uses the given array of bytes as its data source. The data is not copied, so changes made to the array affect the data the ByteArrayInputStream returns.

READ  IllegalAccessError in Java

Parameter
buf – The stream source.

public ByteArrayInputStream(byte[] buf, int offset, int length);

public ByteArrayInputStream(byte[] buf, int offset, int length) constructor creates a ByteArrayInputStream that uses, as its data source, length bytes in a given array of bytes, starting at offset bytes from the beginning of the array. The data is not copied, so changes made to the array affect the data the ByteArrayInputStream returns.

Parameter
buf – The stream source.
offset – An index into the buffer where the stream should begin.
length – The number of bytes to read.

public synchronized int available();

public synchronized int available() method returns the number of bytes remaining to be read in the byte array.
This method returns the number of bytes remaining to be read in the array.

public synchronized int read();

public synchronized int read() method returns the next byte in the array.

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

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

public synchronized int read(byte[] b, int off, int len) method copies up to len bytes from its internal byte array into the given array b, starting at index off.

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

Parameter

b – An array to read bytes into.
off – An offset into b.
len – The number of bytes to read.

public synchronized void reset();

public synchronized void reset() method resets the position of the input stream to the beginning of the byte array. If we specified an offset into the array, we might expect this method to reset the position to where we first started reading from the stream, but that is not the case.

public synchronized long skip(long n);

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

READ  Class Runtime in Java

This method returns the number of bytes skipped.

Parameter
n – The number of bytes to skip.
Apart from these ByteArrayInputStream 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)

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

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

Share and Enjoy !

Leave a Comment

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