Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

compare ByteBuffer contents?

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

like image 953
Jason S Avatar asked Sep 22 '10 13:09

Jason S


People also ask

How do I get data from ByteBuffer?

In order to get the byte array from ByteBuffer just call the ByteBuffer. array() method. This method will return the backed array. Now you can call the String constructor which accepts a byte array and character encoding to create String.

What is the byte order of ByteBuffer?

By default, the order of a ByteBuffer object is BIG_ENDIAN. If a byte order is passed as a parameter to the order method, it modifies the byte order of the buffer and returns the buffer itself. The new byte order may be either LITTLE_ENDIAN or BIG_ENDIAN.

What does ByteBuffer compact do?

ByteBuffer is used to compact a buffer. Compacting a buffer means: Copying the values between the position and limit of a buffer to its beginning. The position of the buffer is set equal to the number of values copied.

What does ByteBuffer wrap do?

wrap. Wraps a byte array into a buffer. The new buffer will be backed by the given byte array; that is, modifications to the buffer will cause the array to be modified and vice versa. The new buffer's capacity will be array.


1 Answers

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.

like image 165
Colin Hebert Avatar answered Oct 16 '22 01:10

Colin Hebert