Class ByteArrayOutputStream in Java

digitization, transformation, binary

Class ByteArrayOutputStream in Java:

ByteArrayOutputStream class implements an output stream where the data is written or stored into a Byte Array. The buffer automatically grows as data are written into it.

A ByteArrayOutputStream is a stream whose data is written to an internal byte array. None of the methods of this class throws an IOException because the data is written to an array instead of an actual I/O device.

Ads code goes here

The data for a ByteArrayOutputStream can be sent to another OutputStream using the
writeTo() method. A copy of the array can be obtained using the toCharArray() method.

The structure of Class ByteArrayOutputStream is given as below:


public class java.io.ByteArrayOutputStream extends java.io.OutputStream{
//member elements
protected byte buf[];//the buffer where the data is stored
protected int count;//the number of valid bytes in the buffer
//constructors:
public ByteArrayOutputStream();//creates a new ByteArrayOutputStream to write data.
public ByteArrayOutputStream(int size);//creates a new ByteArrayOutputStream to write data.The buffer capacity is initially 32 bytes.Its size increases if necessary 
//Methods:
public synchronized void reset();
public int size();
public synchronized byte[] toByteArray();
public String toString();
public String toString(String enc) throws UnsupportedEncodingException;
public String toString(int hiBytes);
public synchronized void write(byte b[],int offSet,int length);
public synchronized void write(int b);
public synchronized void writeTo(OutputStream out) throws IOException;
}
 

The details of the class structure are given as follows:

protected byte[] buf;

protected byte[] buf is the buffer that holds data for this stream.

protected int count;

protected int count is a placeholder that marks the end of the data in the buffer.

public ByteArrayOutputStream();

public ByteArrayOutputStream() constructor creates a ByteArrayOutputStream with an internal buffer that has a default size of 32 bytes. The buffer grows automatically as data is written to the stream.

public ByteArrayOutputStream(int size);

public ByteArrayOutputStream(int size) constructor creates a ByteArrayOutputStream with an internal buffer that has a size of size bytes. The buffer grows automatically as data is written to the stream.

READ  Class InterruptedIOException in Java

Parameter
size -The initial buffer size.

public synchronized void reset();

public synchronized void reset() method discards the current contents of the buffer and resets the position of the stream to zero. Subsequent data is written starting at the beginning of the array.

public int size( );

public int size( ) method returns the number of bytes currently stored in this object’s internal buffer. It is a count of the number of bytes that have been written to the stream.

public synchronized byte[] toByteArray();

public synchronized byte[] toByteArray() method copies the data in the internal array and returns a reference to the copy. The returned array is as long as the data that has been written to the stream, i.e., the same as size().

This method returns a copy of the data that has been written to this ByteArrayOutputStream.

public String toString();

public String toString() method returns a reference to a String object that contains a copy of the bytes currently stored in this object’s internal buffer. The bytes are assumed to represent characters in the encoding that is customary for the native platform, so the bytes are converted to Unicode characters based on that assumption.

This method returns a copy of the data that has been written to this ByteArrayOutputStream.

public String toString(int hibyte);

public String toString(int hibyte) method provides a way to convert from bytes to characters. In java 1.1, it is deprecated and replaced with toString(String).

This method returns A copy of the data that has been written to this ByteArrayOutputStream, where each character in the string has a high byte of hibyte and a low byte taken from the corresponding byte in the array.

READ  Learn Methods in Java in a Quick Manner

Parameter
hibyte – A value to use as the high byte of each character.

public String toString(String enc);

public String toString(String enc) method returns a Java String created from the byte array of this stream. The conversion is performed according to the encoding scheme enc.

This method returns a copy of the data that has been written to this ByteArrayOutputStream, converted from bytes to characters via the named encoding scheme enc.

Parameter
enc – The encoding scheme to use.

public synchronized void write(int b);

public synchronized void write(int b) method writes the low-order 8 bits of the given value into the internal array. If the array is full, a larger array is allocated.

Parameter
b -The value to write.

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

public synchronized void write(byte[] b, int off, int len) method copies len bytes to this object’s internal array from b, starting oset elements from the beginning of the supplied array b. If the internal array is full, a larger array is allocated.

Parameter
b – The array to copy from.
off -Offset into the byte array.
len -Number of bytes to write.

public synchronized void writeTo(OutputStream out);

public synchronized void writeTo(OutputStream out) method writes the contents of this object’s internal buffer to the given OutputStream. All the data that has been written to this ByteArrayOutputStream is written to out.

Parameter
out -The destination stream.

Apart from these ByteArrayOutputStream 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)
READ  Learn DataTypes in Java Quick and Easy Way

ByteArrayOutputStream class also has inherited methods from class- OutputStream. They are as follows:

  • flush()
  • write(byte[])
  • close()

Share and Enjoy !

Leave a Comment

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