0% found this document useful (0 votes)
5 views7 pages

Java Byte Array Output Input Stream Class

The Java Byte Array Output Stream class allows writing data to a byte array that can be forwarded to multiple streams, with automatic buffer growth. It includes constructors for creating streams with default or specified capacities, and methods for writing data, converting to strings, and managing the stream. The Java Byte Array Input Stream class reads data from a byte array, providing methods to access and manipulate the input stream.

Uploaded by

annelashalini
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views7 pages

Java Byte Array Output Input Stream Class

The Java Byte Array Output Stream class allows writing data to a byte array that can be forwarded to multiple streams, with automatic buffer growth. It includes constructors for creating streams with default or specified capacities, and methods for writing data, converting to strings, and managing the stream. The Java Byte Array Input Stream class reads data from a byte array, providing methods to access and manipulate the input stream.

Uploaded by

annelashalini
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Byte Array Output Stream Class

 Java Byte Array Output Stream class is used to write common data into multiple files.
 In this stream, the data is written into a byte array which can be written to multiple streams later.
 The Byte Array Output Stream holds a copy of data and forwards it to multiple streams.
 The buffer of Byte Array Output Stream automatically grows according to data.

Java Byte Array Output Stream class declaration


Let's see the declaration for [Link]. Byte Array Output Stream class:

public class Byte Array Output Stream extends Output Stream

Java ByteArrayOutputStream class constructors

Constructor Description

ByteArrayOutputStream() Creates a new byte array output stream with the initial capacity of 32 bytes,
though its size increases if necessary.

ByteArrayOutputStream(int size) Creates a new byte array output stream, with a buffer capacity of the specified size, in bytes.
Java ByteArrayOutputStream class methods

Method Description

int size() It is used to returns the current size of a buffer.

byte[] toByteArray() It is used to create a newly allocated byte array.

String toString() It is used for converting the content into a string decoding bytes using
a platform default character set.

String toString(String charsetName) It is used for converting the content into a string decoding bytes using a specified charsetName.

void write(int b) It is used for writing the byte specified to the byte array output stream.

void write(byte[] b, int off, int len It is used for writing len bytes from specified byte array starting from
the offset off to the byte array output stream.

void writeTo(OutputStream out) It is used for writing the complete content of a byte array
output stream to the specified output stream.

void reset() It is used to reset the count field of a byte array output stream to zero value.

void close() It is used to close the ByteArrayOutputStream.


import [Link];

class Main {
public static void main(String[] args) {

String data = "This is a line of text inside the string.";

try {
// Creates an output stream
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] array = [Link]();

// Writes data to the output stream


[Link](array);

// Retrieves data from the output stream in string format


String streamData = [Link]();
[Link]("Output stream: " + streamData);

[Link]();
}

catch(Exception e) {
[Link]();
}
}
}

Output

Output stream: This is a line of text inside the string.


Java ByteArrayInputStream Class
The ByteArrayInputStream is composed of two words: ByteArray and InputStream. As the name suggests, it can be used to
read byte array as input stream.

Java ByteArrayInputStream class contains an internal buffer which is used to read byte array as stream. In this stream, the
data is read from a byte array.

The buffer of ByteArrayInputStream automatically grows according to data.

Java ByteArrayInputStream class declaration


Let's see the declaration for [Link] class:

public class ByteArrayInputStream extends InputStream

Java ByteArrayInputStream class constructors

Constructor Description

ByteArrayInputStream(byte[] ary) Creates a new byte array input stream which uses ary as its buffer array.

ByteArrayInputStream(byte[] ary, int offset, int len) Creates a new byte array input stream which uses ary as its
buffer array that can read up to specified len bytes of data from an array.
Java ByteArrayInputStream class methods

Methods Description

int available() It is used to return the number of remaining bytes that can be read from the input stream.

int read() It is used to read the next byte of data from the input stream.

int read(byte[] ary, int off, int len) It is used to read up to len bytes of data from an array of bytes in the input stream.

boolean markSupported() It is used to test the input stream for mark and reset method.

long skip(long x) It is used to skip the x bytes of input from the input stream.

void mark(int readAheadLimit) It is used to set the current marked position in the stream.

void reset() It is used to reset the buffer of a byte array.

void close() It is used for closing a ByteArrayInputStream.


Example: ByteArrayInputStream to read data
import [Link];

public class Main {


public static void main(String[] args) {

// Creates an array of byte


byte[] array = {1, 2, 3, 4};

try {
ByteArrayInputStream input = new ByteArrayInputStream(array);

[Link]("The bytes read from the input stream: ");

for(int i= 0; i < [Link]; i++) {

// Reads the bytes


int data = [Link]();
[Link](data + ", ");
}
[Link]();
}

catch(Exception e) {
[Link]();
}
}
}
Run Code
Output

The bytes read from the input stream: 1, 2, 3, 4,

You might also like