Introduction
InputStream and OutputStream are abstract classes used to read or write data. A ByteArray is an array of bytes. The ByteArrayInputStream reads this Byte array as an input stream, and ByteArrayOutputStream writes data into this Byte array. Both contain an internal buffer to store data and an internal counter to keep track of the next byte to be read or written. Let’s learn about these classes in detail.
Also see, Swap Function in Java
ByteArrayInputStream
The ByteArrayInputStream class is used to read bytes of data from a stream. Closing this stream after use has no effect as its methods can be called even after closing it without producing any IOException.
This class is a part of the java.io package and extends the InputStream class.
Declaration:
public class ByteArrayInputStream extends InputStreamConstructors
-
ByteArrayInputStream(byte[ ] b)
-
It creates ByteArrayInputStream to use b as its buffer array. It reads the entire data from this array.
-
It creates ByteArrayInputStream to use b as its buffer array. It reads the entire data from this array.
ByteArrayInputStream ba_in = new ByteArrayInputStream(byte[] b);
-
ByteArrayInputStream(byte [ ] b, int offset, int length)
-
It creates ByteArrayInputStream to use b as its buffer array. It reads a specific number of bytes from a particular starting point in the array.
-
It creates ByteArrayInputStream to use b as its buffer array. It reads a specific number of bytes from a particular starting point in the array.
ByteArrayInputStream ba_in = new ByteArrayInputStream(byte[] b, int offset, int length);Methods
Method |
Description |
| int available() | It returns the estimated number of bytes that can be read or skipped over from this input stream without blocking calls from another method that requests this InputStream. |
| void close() | It closes the InputStream and releases all the resources associated with it. |
| void mark (int readLimit) | It marks a point in the input stream. |
| Boolean markSupported() | It checks if the input stream supports the mark and reset functions. |
| int read() | It reads the next byte of data from the Input stream. |
| int read (byte b[ ], int off, int len) | It reads a given number of bytes from the Input stream and stores it in the given byte array. |
| void reset() | It resets the position of the stream to the initial one mentioned by the most recent mark function. |
| long skip(long n) | It skips over and discards n bytes of data from the Input stream. |
Example
import java.io.ByteArrayInputStream;
public class Main {
public static void main(String[] args) {
byte[] arr = {'a','b','c'};
try {
// Create input stream
ByteArrayInputStream input = new ByteArrayInputStream(arr);
System.out.println(input.markSupported());
System.out.println(input.available());
for(int i= 0; i < arr.length; i++) {
// Read the bytes
int data = input.read();
System.out.print(data + " ");
}
input.close();
}
catch(Exception e) {
System.out.println(e);
}
}
}
Output:
true
3
97 98 99
Try it on online java compiler.




