Java ByteBuffer set limit to current position
import java.nio.ByteBuffer; public class Main { public static void main(String[] args) { ByteBuffer bb = ByteBuffer.allocate(8); for (int i = 50; i < 58; i++) { bb.put((byte) i); }//from w ww . j av a2s .co m bb.limit(bb.position()); bb.position(0); int limit = bb.limit(); for (int i = 0; i < limit; i++) { byte b = bb.get(); System.out.println(b); } } }