What's the easiest way in Java to compare the contents of two ByteBuffers to check for equality?
Asked
Active
Viewed 9,718 times
12
-
One can also use an [API introduced in JDK/11](https://stackoverflow.com/a/51811651/1746118), to additionally find out the relative index of mismatch while comparing two `ByteBuffer`s. – Naman Aug 12 '18 at 18:27
2 Answers
16
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,
- They have the same element type,
- They have the same number of remaining elements, and
- 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.

Colin Hebert
- 91,525
- 15
- 160
- 151
-
With a reputation of 17.7 k we can probably assume he has considered the `.equals` method. I suspect the OP wants to compare content only. – aioobe Sep 22 '10 at 14:39
-
Still, he explicitly asks for "compare the contents of two ByteBuffers". – aioobe Sep 22 '10 at 14:44
-
3Of course I'm human, I forgot to check the javadoc first before asking. Duh. :-) And I asked to check for equality, so this is the best method. – Jason S Sep 22 '10 at 15:11
-
4Be careful with this. The key phrase in the definition is "remaining elements". So if the two ByteBuffers are in write mode, with their contents having been set via calls to various put() methods, then there are no "remaining elements" and equals will always return true. To get equals() to work you need to have a rewind() call after the last put(). – RenniePet May 30 '15 at 08:57
4
Alternatively, with JDK/11, there is another way of comparing a ByteBuffer
with another. The API which primarily focusses on finding the mismatch between the two can be used as -
int mismatchBetweenTwoBuffers = byteBuffer1.mismatch(byteBuffer2);
if(mismatchBetweenTwoBuffers == -1) {
System.out.println("The buffers are equal!");
} else {
System.out.println("The buffers are mismatched at - " + mismatchBetweenTwoBuffers);
}
Its documentation reads as:-
/**
* Finds and returns the relative index of the first mismatch between this
* buffer and a given buffer. The index is relative to the
* {@link #position() position} of each buffer and will be in the range of
* 0 (inclusive) up to the smaller of the {@link #remaining() remaining}
* elements in each buffer (exclusive).
*
* <p> If the two buffers share a common prefix then the returned index is
* the length of the common prefix and it follows that there is a mismatch
* between the two buffers at that index within the respective buffers.
* If one buffer is a proper prefix of the other then the returned index is
* the smaller of the remaining elements in each buffer, and it follows that
* the index is only valid for the buffer with the larger number of
* remaining elements.
* Otherwise, there is no mismatch.
*
* @return The relative index of the first mismatch between this and the
* given buffer, otherwise -1 if no mismatch.
*
* @since 11
*/
public int mismatch(ByteBuffer that)

Naman
- 27,789
- 26
- 218
- 353