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:
- The
set(int i, byte b)method sets the value of a byte at indexi. - The
setInt(int i, int value)method sets the value of the next four bytes at indexi. - The
setLong(int i, long value)method sets the value of the next eight bytes at indexi. - The
set(int offset, Bytes bytes)method sets the value of the next bytes at indexi.