By default, bytes objects are not mutable. You can use MutableBytes objects instead.

Creating MutableBytes objects

The methods described in the tutorial “Creating Bytes” all work in the same way for MutableBytes.

You can call the method mutableCopy() on any Bytes object to get a copy of the Bytes object as mutable.

Finally, you can create fresh objects with the create() method.

Fill, Clear

Fill a MutableBytes with the same byte the fill method:

MutableBytes bytes = MutableBytes.create(2);
bytes.fill((byte) 34);
assertEquals(Bytes.fromHexString("0x2222"), bytes);

You can clear the contents with the clear method:

MutableBytes bytes = MutableBytes.fromHexString("0xdeadbeef");
bytes.clear();

Setting values

You can set values with different arguments: