public interface ByteBlock
An ByteBlock operates in two sequential modes: write
mode and read mode. In write mode, data is appended
through typed write methods and the buffer elastically expands to accommodate
any amount of data. Calling flip() transitions the buffer to read
mode, after which the written data can be consumed through the corresponding
typed read methods.
Unlike ByteBuffer, an ByteBlock does not require the
caller to pre-calculate or declare a fixed capacity. The buffer grows as
needed, making it suitable for serialization tasks where the final size is
not known in advance.
| Modifier and Type | Method and Description |
|---|---|
byte[] |
array()
Return the backing byte array.
|
ByteBlock |
flip()
Transition from write mode to read mode.
|
boolean |
hasRemaining()
Return
true if there are bytes remaining to be read. |
long |
position()
Return the current position — the number of bytes written so far
(in write mode) or the number of bytes read so far (in read mode).
|
ByteBlock |
read(byte[] dest)
Read bytes into the provided array, filling it completely.
|
ByteBlock |
read(byte[] dest,
int offset,
int length)
Read
length bytes into dest starting at offset. |
boolean |
readBoolean()
Read a boolean written by
writeBoolean(boolean). |
int |
readByte()
Read a single byte.
|
char |
readChar()
Read a 2-byte char in big-endian order.
|
double |
readDouble()
Read an 8-byte double in IEEE 754 format, big-endian order.
|
float |
readFloat()
Read a 4-byte float in IEEE 754 format, big-endian order.
|
int |
readInt()
Read a 4-byte integer in big-endian order.
|
long |
readLong()
Read an 8-byte long in big-endian order.
|
short |
readShort()
Read a 2-byte short in big-endian order.
|
String |
readUtf8()
Read a UTF-8 encoded string written by
writeUtf8(String). |
long |
remaining()
Return the number of bytes remaining to be read (in read mode) or the
number of bytes written so far (in write mode).
|
ByteBlock |
write(byte[] bytes)
Write a raw byte array.
|
ByteBlock |
write(byte[] bytes,
int offset,
int length)
Write
length bytes from bytes starting at offset. |
ByteBlock |
writeBoolean(boolean value)
Write a boolean as a single byte (
1 for true, 0
for false). |
ByteBlock |
writeByte(int value)
Write a single byte.
|
ByteBlock |
writeChar(char value)
Write a 2-byte char in big-endian order.
|
ByteBlock |
writeDouble(double value)
Write an 8-byte double in IEEE 754 format, big-endian order.
|
ByteBlock |
writeFloat(float value)
Write a 4-byte float in IEEE 754 format, big-endian order.
|
ByteBlock |
writeInt(int value)
Write a 4-byte integer in big-endian order.
|
ByteBlock |
writeLong(long value)
Write an 8-byte long in big-endian order.
|
ByteBlock |
writeShort(short value)
Write a 2-byte short in big-endian order.
|
ByteBlock |
writeUtf8(String value)
Write a UTF-8 encoded string with a length prefix.
|
ByteBlock writeByte(int value)
value - the byte value (only the lowest 8 bits are used)ByteBlockByteBlock writeShort(short value)
value - the short valueByteBlockByteBlock writeChar(char value)
value - the char valueByteBlockByteBlock writeInt(int value)
value - the int valueByteBlockByteBlock writeFloat(float value)
value - the float valueByteBlockByteBlock writeLong(long value)
value - the long valueByteBlockByteBlock writeDouble(double value)
value - the double valueByteBlockByteBlock writeBoolean(boolean value)
1 for true, 0
for false).value - the boolean valueByteBlockByteBlock writeUtf8(@Nullable String value)
A null string is supported and will be read back as null
by readUtf8().
value - the string to write, or nullByteBlockByteBlock write(byte[] bytes)
bytes - the bytes to writeByteBlockByteBlock write(byte[] bytes, int offset, int length)
length bytes from bytes starting at offset.bytes - the source byte arrayoffset - the start offset in the sourcelength - the number of bytes to writeByteBlockint readByte()
short readShort()
char readChar()
int readInt()
float readFloat()
long readLong()
double readDouble()
boolean readBoolean()
writeBoolean(boolean).@Nullable String readUtf8()
writeUtf8(String).null if null was writtenByteBlock read(byte[] dest)
dest - the destination byte arrayByteBlockByteBlock read(byte[] dest, int offset, int length)
length bytes into dest starting at offset.dest - the destination byte arrayoffset - the start offset in the destinationlength - the number of bytes to readByteBlockByteBlock flip()
remaining() reflects the total
number of bytes that were written.ByteBlockboolean hasRemaining()
true if there are bytes remaining to be read.true if bytes remainlong remaining()
long position()
byte[] array()