Java IO: ByteArrayInputStream

 

 

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 = input.read();
while(data != -1) {
  //do something with data

  data = input.read();
}
input.close();    

  

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.

 

posted @ 2015-08-17 04:16  hephec  阅读(140)  评论(0编辑  收藏  举报