Java ByteArrayInputStream Class
In Java, the ByteArrayInputStream
class is part of the java.io
package and provides an input stream that reads data from a byte array in memory. Unlike file-based streams such as FileInputStream
, ByteArrayInputStream
operates entirely within memory, reading data directly from a byte array.
It is especially useful when you need to work with data that is already available in a byte array, and you want to treat it as an input stream for operations like reading or processing byte data.
The ByteArrayInputStream
class extends InputStream
and allows you to read data from a byte array. It provides a way to treat a byte array as an input stream. This is useful in scenarios where you need to work with byte data that is already available in memory (such as data read from a file or received over a network) without needing to write it to a file or another external source.
Some key features of the ByteArrayInputStream
class include:
The ByteArrayInputStream
class inherits methods from the InputStream
class but provides additional functionality specific to byte arrays. Here are the essential methods:
int read()
:
Reads the next byte of data from the byte array. Returns the byte as an integer value, or -1
if the end of the byte array is reached.
int read(byte[] b)
:
Reads bytes from the byte array into the provided byte array b
. It reads up to b.length
bytes and returns the number of bytes actually read, or -1
if the end of the byte array is reached.
int read(byte[] b, int off, int len)
:
Reads a specific number of bytes from the byte array into the provided byte array b
. The reading starts from the specified offset off
, and up to len
bytes are read.
long skip(long n)
:
Skips over n
bytes of data in the byte array. It returns the actual number of bytes skipped, which can be less than n
if the end of the byte array is reached.
int available()
:
Returns the number of bytes that can be read from the byte array without blocking. This is useful to check how much data remains in the byte array.
void mark(int readlimit)
:
Marks the current position in the byte array as a reference point. This allows the reset()
method to return to this marked position.
void reset()
:
Resets the stream to the most recent marked position. If no position was marked, an IOException
is thrown.
void close()
:
Although this method is inherited from InputStream
, closing a ByteArrayInputStream
does not release any system resources, as no external resources are being used.
The ByteArrayInputStream
can be used to read data from a byte array as if it were a stream. Below is an example of how to read the content of a byte array.
import java.io.*;
public class ByteArrayInputStreamExample {
public static void main(String[] args) {
byte[] data = "Hello, ByteArrayInputStream!".getBytes(); // Convert string to byte array
try (ByteArrayInputStream bais = new ByteArrayInputStream(data)) {
int byteData;
while ((byteData = bais.read()) != -1) {
System.out.print((char) byteData); // Convert each byte to a character and print
}
} catch (IOException e) {
System.out.println("An error occurred while reading the byte array.");
e.printStackTrace();
}
}
}
Explanation:
data
is created from a string.ByteArrayInputStream
is used to read the bytes from the array.(char) byteData
.Output:
Hello, ByteArrayInputStream!
You can also use ByteArrayInputStream
to read multiple bytes at once into a larger byte array.
import java.io.*;
public class ByteArrayInputStreamArrayExample {
public static void main(String[] args) {
byte[] data = "This is an example of reading multiple bytes.".getBytes(); // Convert string to byte array
byte[] buffer = new byte[10]; // Buffer to hold byte data
try (ByteArrayInputStream bais = new ByteArrayInputStream(data)) {
int bytesRead;
while ((bytesRead = bais.read(buffer)) != -1) {
System.out.print(new String(buffer, 0, bytesRead)); // Print the bytes read as a string
}
} catch (IOException e) {
System.out.println("An error occurred while reading the byte array.");
e.printStackTrace();
}
}
}
Explanation:
read(byte[] b)
method reads bytes into the buffer in chunks and prints them until the end of the byte array is reached.Output:
This is an example of reading multiple bytes.
The skip(long n)
method allows you to skip a specified number of bytes in the byte array.
import java.io.*;
public class ByteArrayInputStreamSkipExample {
public static void main(String[] args) {
byte[] data = "Skip over this portion of data.".getBytes();
try (ByteArrayInputStream bais = new ByteArrayInputStream(data)) {
bais.skip(5); // Skip the first 5 bytes
int byteData;
while ((byteData = bais.read()) != -1) {
System.out.print((char) byteData); // Print the remaining data
}
} catch (IOException e) {
System.out.println("An error occurred while reading the byte array.");
e.printStackTrace();
}
}
}
Explanation:
skip(5)
method skips the first 5 bytes.Output:
ver this portion of data.
ByteArrayInputStream
is ideal for working with in-memory byte data, such as data stored in arrays or received over the network.