Concatenating and wrapping

You can concatenate or wrap bytes objects.

When concatenating, the underlying objects are copied into a new bytes object.

When wrapping, you are creating a view made of the underlying bytes objects. If their values change, the wrapped view changes as well.

Of course, wrapping is preferrable to avoid copying bytes in memory.

Copying and slicing

In the same spirit as above, you can copy or slice bytes objects. When you slice a bytes object, you create a view of the original bytes object, and the slice will change if the underlying bytes object changes. If you copy instead, you create a new copy of the bytes.

// slice from the second byte:
bytes.slice(2);
// slice from the second byte to the fifth byte:
bytes.slice(2, 5);

Shifting bytes

You can shift right and left the bits of a bytes object by a given distance.

This is equivalent to the <<< or >>> operators in Java.

xor, or, and

You can apply boolean operations to Bytes objects.

Those methods take as argument the value to compare this value with, and return a new Bytes object that is the result of the boolean operation.

If the argument and the value are different lengths, then the shorter will be zero-padded to the left.

Bytes value = Bytes.fromHexString("0x01000001").xor(Bytes.fromHexString("0x01000000"));
assertEquals(Bytes.fromHexString("0x00000001"), value);

not

The not method returns a bit-wise NOT of the bytes.

Bytes value = Bytes.fromHexString("0x01000001").not();
assertEquals(Bytes.fromHexString("0xfefffffe"), value);

commonPrefix

The commonPrefix method returns the common bytes both the value and the argument start with.