Tech and Media Labs
This site uses cookies to improve the user experience.

Java IO: ByteArrayInputStream

Jakob Jenkov
Last update: 2014-06-23

The ByteArrayInputStream class of the Java IO API allows you to read data from byte arrays as streams. Here is a simple example:

byte[] bytes = ... //get byte array from somewhere.

InputStream input = new ByteArrayInputStream(bytes);

int data =;
while(data != -1) {
  //do something with data

  data =;

ByteArrayInputStream's can be handy if your data is stored in an array, but you have a component that can only process it as an InputStream. The ByteArrayInputStream can thus wrap the byte array, and turn it into a stream.

Note: The proper exception handling has been skipped here for the sake of clarity. To learn more about correct exception handling, go to Java IO Exception Handling.

Jakob Jenkov

Copyright  Jenkov Aps
Close TOC