Java ByteBuffer iterate
import java.nio.ByteBuffer; public class Main { public static void main(String[] args) { // Create a byte buffer of capacity 8 ByteBuffer bb = ByteBuffer.allocate(8); int limit = bb.limit(); System.out.println("Position = " + bb.position() + ", Limit = " + limit); System.out.print("Data: "); while (bb.hasRemaining()) { System.out.print(bb.get() + " "); }// w ww. j a v a2 s.co m // call flip() to reset the position to zero because bb.flip(); // Populate buffer elements from 50 to 57 int i = 50; while (bb.hasRemaining()) { bb.put((byte) i++); } // Call flip() again to reset the position to zero, // put() call incremented the position bb.flip(); // Print the buffer info System.out.println("After populating data:"); limit = bb.limit(); System.out.println("Position = " + bb.position() + ", Limit = " + limit); System.out.print("Data: "); while (bb.hasRemaining()) { System.out.print(bb.get() + " "); } } }