Package org.symbol.sdk.utils
Class BufferView
java.lang.Object
org.symbol.sdk.utils.BufferView
- BufferView
- Buffer view that supports moving / shrinking the visible window over a backing array without copying. Thin wrapper over a little-endian
ByteBuffer; the visible window is[position, limit).
-
Constructor Summary
ConstructorsConstructorDescriptionBufferView(byte[] buffer) Creates buffer view around a buffer.BufferView(ByteBuffer buffer) Creates a buffer view over aByteBuffer's remaining bytes. -
Method Summary
Modifier and TypeMethodDescriptionbuffer()Returns aByteBufferover the visible window (this view is not advanced).intlength()Returns the length of the visible window.byte[]peekBytes(int size) Copies the firstsizebytes of the visible window into a fresh array, bounded to the window (asizelarger than the remaining bytes throws).longpeekInt(int size, boolean isSigned) Reads a little-endian integer ofsizebytes at the current position without advancing, sign- or zero-extended into along(an 8-byte unsignedu64 >= 2^63reads back negative — seeLong.toUnsignedString(long)).voidshiftRight(int size) Moves view right.voidshrink(int size) Shrinks view to specified size.snapshot()Returns an independent, zero-copy view over this view's current window (rebased so reads start at its first byte).window(int size) Returns a zero-copy view over the firstsizebytes of the visible window (sharing the backing array; this view is not advanced).
-
Constructor Details
-
BufferView
public BufferView(byte[] buffer) Creates buffer view around a buffer.- Parameters:
buffer- Initial buffer view.
-
BufferView
Creates a buffer view over aByteBuffer's remaining bytes.- Parameters:
buffer- Backing buffer; read from its current position, left unmodified.
-
-
Method Details
-
buffer
- BufferView.buffer
- Returns a
ByteBufferover the visible window (this view is not advanced).
- Returns:
- ByteBuffer over the current window.
-
peekBytes
public byte[] peekBytes(int size) - BufferView.peekBytes
- Copies the first
sizebytes of the visible window into a fresh array, bounded to the window (asizelarger than the remaining bytes throws). The current position is not advanced (the caller shiftRights by the field's size).
- Parameters:
size- Number of bytes to copy.- Returns:
- Copied bytes.
-
length
public int length()- BufferView.length
- Returns the length of the visible window.
- Returns:
- Length.
-
shiftRight
public void shiftRight(int size) - BufferView.shiftRight
- Moves view right.
- Parameters:
size- Amount of bytes to shift.
-
window
- BufferView.window
- Returns a zero-copy view over the first
sizebytes of the visible window (sharing the backing array; this view is not advanced). The window is rebased so reads start at its first byte; copy it out withpeekBytes(int).
- Parameters:
size- Length in bytes.- Returns:
- View limited to specified size.
-
snapshot
- BufferView.snapshot
- Returns an independent, zero-copy view over this view's current window (rebased so reads start at its first byte). Reads on the returned cursor do not advance this view — used by deserializers to walk a private cursor while the caller advances the original by the object's size().
- Returns:
- Independent view over the same window.
-
shrink
public void shrink(int size) - BufferView.shrink
- Shrinks view to specified size.
- Parameters:
size- New length in bytes.
-
peekInt
public long peekInt(int size, boolean isSigned) - BufferView.peekInt
- Reads a little-endian integer of
sizebytes at the current position without advancing, sign- or zero-extended into along(an 8-byte unsignedu64 >= 2^63reads back negative — seeLong.toUnsignedString(long)).
- Parameters:
size- Byte width (1, 2, 4 or 8).isSigned-trueif the value should be signed (ignored forsize == 8, which keeps the raw 64-bit pattern).- Returns:
- Decoded value.
-