By default, bytes objects are not mutable. You can use MutableBytes
objects instead.
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 a MutableBytes with the same byte the fill method:
{%highlight java%} MutableBytes bytes = MutableBytes.create(2); bytes.fill((byte) 34); assertEquals(Bytes.fromHexString(“0x2222”), bytes); {%endhighlight%}
You can clear the contents with the clear
method:
{%highlight java%} MutableBytes bytes = MutableBytes.fromHexString(“0xdeadbeef”); bytes.clear(); {%endhighlight%}
You can set values with different arguments:
set(int i, byte b)
method sets the value of a byte at index i
.setInt(int i, int value)
method sets the value of the next four bytes at index i
.setLong(int i, long value)
method sets the value of the next eight bytes at index i
.set(int offset, Bytes bytes)
method sets the value of the next bytes at index i
.