From a bytes array:
You can create Bytes
objects by wrapping a native byte array:
Note the underlying array is not copied - any change to the byte array will change the Bytes object’s behavior.
You can also wrap with an offset and a size to select a portion of the array:
From a hex string:
You can create Bytes
objects from a hexadecimal-encoded string with the fromHexString
method:
The "0x"
prefix is optional.
However, this requires an even-sized string. For example, this succeeds:
This fails:
You can circumvent this with the fromHexStringLenient
method:
From a base64-encoded string:
You can create Bytes
objects from a base64-encoded string with the fromBase64String
method:
From primitive types
We also have convenience methods to create Bytes
objects from primitive types.
Bytes.of() takes a variadic argument of bytes:
More wrapping
Use Bytes.wrapByteBuf(buffer)
to wrap a Netty ByteBuf
object as a Bytes
object.
You can apply an offset and size parameter:
Use Bytes.wrapByteBuffer(buffer)
to wrap a ByteBuffer
object as a Bytes
object.
You can apply an offset and size parameter:
Use Bytes.wrapBuffer(buffer)
to wrap a Vert.x Buffer
object as a Bytes
object.
You can apply an offset and size parameter:
Random
You can create random bytes objects of a given length with the Bytes.random() method:
Create a Bytes object with our own Random implementation: