Java Create a ByteArrayInputStream

Java Create a ByteArrayInputStream

To create a ByteArrayInputStream in Java, you can use the following code:

byte[] data = {1, 2, 3, 4, 5};
ByteArrayInputStream in = new ByteArrayInputStream(data);
Source‮w:‬ww.lautturi.com

This code creates a new ByteArrayInputStream called in that reads from an array of bytes called data. The ByteArrayInputStream wraps the array of bytes and provides methods to read from it.

You can then use the read method of the ByteArrayInputStream to read data from the array, like this:

int b = in.read(); // reads the next byte from the array and stores it in the variable b

It is important to remember to close the ByteArrayInputStream when you are finished reading from it, to release any resources that it is using. You can do this using the close method, like this:

in.close();
Created Time:2017-11-03 00:14:38  Author:lautturi