ByteBuffer.capacity is the total capacity of the backing data structure, but ByteBuffer objects can represent just slices or windows into that structure.  Thus, it is generally more correct to do buf.remaining().