@NotThreadSafe public final class HeapByteBlock extends Object implements ByteBlock
ByteBlock that manages a byte[] directly and
grows by doubling when the capacity is exceeded.
Write operations encode typed values via bit-shifting, matching the
efficiency of java.nio.ByteBuffer's internal
encoding without any wrapper overhead. Read operations delegate to a
ByteBuffer that wraps the written bytes after flip() is
called.
| Modifier and Type | Method and Description |
|---|---|
static HeapByteBlock |
allocate()
Return a new
HeapByteBlock in write mode with a default initial
capacity. |
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
ByteBlock.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
ByteBlock.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).
|
static HeapByteBlock |
wrap(byte[] data)
Return a new
HeapByteBlock in read mode that wraps the provided
byte array. |
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.
|
public static HeapByteBlock allocate()
HeapByteBlock in write mode with a default initial
capacity.HeapByteBlockpublic static HeapByteBlock wrap(byte[] data)
HeapByteBlock in read mode that wraps the provided
byte array.data - the byte array to wrapHeapByteBlock ready for readingpublic ByteBlock writeByte(int value)
ByteBlockpublic ByteBlock writeShort(short value)
ByteBlockwriteShort in interface ByteBlockvalue - the short valueByteBlockpublic ByteBlock writeChar(char value)
ByteBlockpublic ByteBlock writeInt(int value)
ByteBlockpublic ByteBlock writeFloat(float value)
ByteBlockwriteFloat in interface ByteBlockvalue - the float valueByteBlockpublic ByteBlock writeLong(long value)
ByteBlockpublic ByteBlock writeDouble(double value)
ByteBlockwriteDouble in interface ByteBlockvalue - the double valueByteBlockpublic ByteBlock writeBoolean(boolean value)
ByteBlock1 for true, 0
for false).writeBoolean in interface ByteBlockvalue - the boolean valueByteBlockpublic ByteBlock writeUtf8(@Nullable String value)
ByteBlock
A null string is supported and will be read back as null
by ByteBlock.readUtf8().
public ByteBlock write(byte[] bytes)
ByteBlockpublic ByteBlock write(byte[] bytes, int offset, int length)
ByteBlocklength bytes from bytes starting at offset.public int readByte()
ByteBlockpublic short readShort()
ByteBlockpublic char readChar()
ByteBlockpublic int readInt()
ByteBlockpublic float readFloat()
ByteBlockpublic long readLong()
ByteBlockpublic double readDouble()
ByteBlockreadDouble in interface ByteBlockpublic boolean readBoolean()
ByteBlockByteBlock.writeBoolean(boolean).readBoolean in interface ByteBlock@Nullable public String readUtf8()
ByteBlockByteBlock.writeUtf8(String).public ByteBlock read(byte[] dest)
ByteBlockpublic ByteBlock read(byte[] dest, int offset, int length)
ByteBlocklength bytes into dest starting at offset.public ByteBlock flip()
ByteBlockByteBlock.remaining() reflects the total
number of bytes that were written.public boolean hasRemaining()
ByteBlocktrue if there are bytes remaining to be read.hasRemaining in interface ByteBlocktrue if bytes remainpublic long remaining()
ByteBlockpublic long position()
ByteBlockpublic byte[] array()
ByteBlock