Java ByteBuffer allocation automatically zeroes the ByteBuffer
import java.nio.ByteBuffer; public class Main { private static final int BSIZE = 1024; public static void main(String[] args) { ByteBuffer bb = ByteBuffer.allocate(BSIZE); int i = 0;/* w ww . ja v a 2s . co m*/ while (i++ < bb.limit()) { if (bb.get() != 0) { System.out.println("nonzero"); } } System.out.println("i = " + i); } }