Date: 2012feb9
Update: 2025sep10
Language: Java
Keywords: iterate
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 a ByteBuffer around the array
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) {
// Do something with b
}