compare ByteBuffer contents?

Jason S picture Jason S · Sep 22, 2010 · Viewed 7.8k times · Source

What's the easiest way in Java to compare the contents of two ByteBuffers to check for equality?

Answer

Colin Hebert picture Colin Hebert · Sep 22, 2010

You could check the equals() method too.

Tells whether or not this buffer is equal to another object.

Two byte buffers are equal if, and only if,

  1. They have the same element type,
  2. They have the same number of remaining elements, and
  3. The two sequences of remaining elements, considered independently of their starting positions, are pointwise equal.

A byte buffer is not equal to any other type of object.