Programming Tips - Java: read from an array of bytes like I read from a file?

Date: 2012feb9 Language: Java Keywords: interate Q. Java: read from an array of bytes like I read from a file? A. Use ByteBuffer. For example:
byte [] data = { 00, 11, 22, 33, 44 }; ByteBuffer bb = ByteBuffer.wrap(data); // Create the ByteBuffer byte b; for (;;) { try { // Call get() until EOF b = bb.get(); } catch(Exception) { // EOF break; } }
OK maybe that's not the best example. But you get the idea. If you just want to look thru a byte array do this:
for (byte b : data) { }