Class ByteReader
- All Implemented Interfaces:
Proxy
GstByteReader
provides a byte reader that can read different integer and
floating point types from a memory buffer. It provides functions for reading
signed/unsigned, little/big endian integers of 8, 16, 24, 32 and 64 bits
and functions for reading little/big endian floating points numbers of
32 and 64 bits. It also provides functions to read NUL-terminated strings
in various character encodings.-
Constructor Summary
ConstructorDescriptionAllocate a new ByteReader.ByteReader
(byte[] data, int size, int byte_) Allocate a new ByteReader with the fields set to the provided values.ByteReader
(byte[] data, int size, int byte_, Arena arena) Allocate a new ByteReader with the fields set to the provided values.ByteReader
(Arena arena) Allocate a new ByteReader.ByteReader
(MemorySegment address) Create a ByteReader proxy instance for the provided memory address. -
Method Summary
Modifier and TypeMethodDescriptionboolean
Free-function: g_freeboolean
dupStringUtf32
(int[] str) Free-function: g_freeboolean
dupStringUtf8
(String[] str) Free-function: g_freevoid
free()
Frees aGstByteReader
instance, which was previously allocated by gst_byte_reader_new().boolean
Returns a constant pointer to the current data position if at leastsize
bytes are left and updates the current position.boolean
getFloat32Be
(Out<Float> val) Read a 32 bit big endian floating point value intoval
and update the current position.boolean
getFloat32Le
(Out<Float> val) Read a 32 bit little endian floating point value intoval
and update the current position.boolean
getFloat64Be
(Out<Double> val) Read a 64 bit big endian floating point value intoval
and update the current position.boolean
getFloat64Le
(Out<Double> val) Read a 64 bit little endian floating point value intoval
and update the current position.boolean
getInt16Be
(Out<Short> val) Read a signed 16 bit big endian integer intoval
and update the current position.boolean
getInt16Le
(Out<Short> val) Read a signed 16 bit little endian integer intoval
and update the current position.boolean
getInt24Be
(Out<Integer> val) Read a signed 24 bit big endian integer intoval
and update the current position.boolean
getInt24Le
(Out<Integer> val) Read a signed 24 bit little endian integer intoval
and update the current position.boolean
getInt32Be
(Out<Integer> val) Read a signed 32 bit big endian integer intoval
and update the current position.boolean
getInt32Le
(Out<Integer> val) Read a signed 32 bit little endian integer intoval
and update the current position.boolean
getInt64Be
(Out<Long> val) Read a signed 64 bit big endian integer intoval
and update the current position.boolean
getInt64Le
(Out<Long> val) Read a signed 64 bit little endian integer intoval
and update the current position.boolean
Read a signed 8 bit integer intoval
and update the current position.static MemoryLayout
The memory layout of the native struct.int
getPos()
Returns the current position of aGstByteReader
instance in bytes.int
Returns the remaining number of bytes of aGstByteReader
instance.int
getSize()
Returns the total number of bytes of aGstByteReader
instance.boolean
getStringUtf8
(String[] str) Returns a constant pointer to the current data position if there is a NUL-terminated string in the data (this could be just a NUL terminator), advancing the current position to the byte after the string.boolean
getSubReader
(ByteReader subReader, int size) Initializes aGstByteReader
sub-reader instance to containsize
bytes of data from the current position of this ByteReader.boolean
getUint16Be
(Out<Short> val) Read an unsigned 16 bit big endian integer intoval
and update the current position.boolean
getUint16Le
(Out<Short> val) Read an unsigned 16 bit little endian integer intoval
and update the current position.boolean
getUint24Be
(Out<Integer> val) Read an unsigned 24 bit big endian integer intoval
and update the current position.boolean
getUint24Le
(Out<Integer> val) Read an unsigned 24 bit little endian integer intoval
and update the current position.boolean
getUint32Be
(Out<Integer> val) Read an unsigned 32 bit big endian integer intoval
and update the current position.boolean
getUint32Le
(Out<Integer> val) Read an unsigned 32 bit little endian integer intoval
and update the current position.boolean
getUint64Be
(Out<Long> val) Read an unsigned 64 bit big endian integer intoval
and update the current position.boolean
getUint64Le
(Out<Long> val) Read an unsigned 64 bit little endian integer intoval
and update the current position.boolean
Read an unsigned 8 bit integer intoval
and update the current position.void
init
(byte[] data) Initializes aGstByteReader
instance to read fromdata
.int
maskedScanUint32
(int mask, int pattern, int offset, int size) Scan for patternpattern
with applied maskmask
in the byte reader data, starting from offsetoffset
relative to the current position.int
maskedScanUint32Peek
(int mask, int pattern, int offset, int size, Out<Integer> value) Scan for patternpattern
with applied maskmask
in the byte reader data, starting from offsetoffset
relative to the current position.static ByteReader
new_
(byte[] data) Create a newGstByteReader
instance, which will read fromdata
.boolean
Returns a constant pointer to the current data position if at leastsize
bytes are left and keeps the current position.boolean
peekFloat32Be
(Out<Float> val) Read a 32 bit big endian floating point value intoval
but keep the current position.boolean
peekFloat32Le
(Out<Float> val) Read a 32 bit little endian floating point value intoval
but keep the current position.boolean
peekFloat64Be
(Out<Double> val) Read a 64 bit big endian floating point value intoval
but keep the current position.boolean
peekFloat64Le
(Out<Double> val) Read a 64 bit little endian floating point value intoval
but keep the current position.boolean
peekInt16Be
(Out<Short> val) Read a signed 16 bit big endian integer intoval
but keep the current position.boolean
peekInt16Le
(Out<Short> val) Read a signed 16 bit little endian integer intoval
but keep the current position.boolean
peekInt24Be
(Out<Integer> val) Read a signed 24 bit big endian integer intoval
but keep the current position.boolean
peekInt24Le
(Out<Integer> val) Read a signed 24 bit little endian integer intoval
but keep the current position.boolean
peekInt32Be
(Out<Integer> val) Read a signed 32 bit big endian integer intoval
but keep the current position.boolean
peekInt32Le
(Out<Integer> val) Read a signed 32 bit little endian integer intoval
but keep the current position.boolean
peekInt64Be
(Out<Long> val) Read a signed 64 bit big endian integer intoval
but keep the current position.boolean
peekInt64Le
(Out<Long> val) Read a signed 64 bit little endian integer intoval
but keep the current position.boolean
Read a signed 8 bit integer intoval
but keep the current position.boolean
peekStringUtf8
(String[] str) Returns a constant pointer to the current data position if there is a NUL-terminated string in the data (this could be just a NUL terminator).boolean
peekSubReader
(ByteReader subReader, int size) Initializes aGstByteReader
sub-reader instance to containsize
bytes of data from the current position of this ByteReader.boolean
peekUint16Be
(Out<Short> val) Read an unsigned 16 bit big endian integer intoval
but keep the current position.boolean
peekUint16Le
(Out<Short> val) Read an unsigned 16 bit little endian integer intoval
but keep the current position.boolean
peekUint24Be
(Out<Integer> val) Read an unsigned 24 bit big endian integer intoval
but keep the current position.boolean
peekUint24Le
(Out<Integer> val) Read an unsigned 24 bit little endian integer intoval
but keep the current position.boolean
peekUint32Be
(Out<Integer> val) Read an unsigned 32 bit big endian integer intoval
but keep the current position.boolean
peekUint32Le
(Out<Integer> val) Read an unsigned 32 bit little endian integer intoval
but keep the current position.boolean
peekUint64Be
(Out<Long> val) Read an unsigned 64 bit big endian integer intoval
but keep the current position.boolean
peekUint64Le
(Out<Long> val) Read an unsigned 64 bit little endian integer intoval
but keep the current position.boolean
Read an unsigned 8 bit integer intoval
but keep the current position.int
readByte()
Read the value of the fieldbyte
.byte[]
readData()
Read the value of the fielddata
.int
readSize()
Read the value of the fieldsize
.boolean
setPos
(int pos) Sets the new position of aGstByteReader
instance topos
in bytes.boolean
skip
(int nbytes) Skipsnbytes
bytes of theGstByteReader
instance.boolean
Skips a NUL-terminated UTF-16 string in theGstByteReader
instance, advancing the current position to the byte after the string.boolean
Skips a NUL-terminated UTF-32 string in theGstByteReader
instance, advancing the current position to the byte after the string.boolean
Skips a NUL-terminated string in theGstByteReader
instance, advancing the current position to the byte after the string.void
writeByte
(int byte_) Write a value in the fieldbyte
.void
Write a value in the fielddata
.void
writeSize
(int size) Write a value in the fieldsize
.Methods inherited from class io.github.jwharm.javagi.base.ProxyInstance
equals, handle, hashCode
-
Constructor Details
-
ByteReader
Create a ByteReader proxy instance for the provided memory address.- Parameters:
address
- the memory address of the native object
-
ByteReader
Allocate a new ByteReader.- Parameters:
arena
- to control the memory allocation scope
-
ByteReader
public ByteReader()Allocate a new ByteReader. The memory is allocated withArena.ofAuto()
. -
ByteReader
Allocate a new ByteReader with the fields set to the provided values.- Parameters:
data
- value for the fielddata
size
- value for the fieldsize
byte_
- value for the fieldbyte_
arena
- to control the memory allocation scope
-
ByteReader
public ByteReader(byte[] data, int size, int byte_) Allocate a new ByteReader with the fields set to the provided values. The memory is allocated withArena.ofAuto()
.- Parameters:
data
- value for the fielddata
size
- value for the fieldsize
byte_
- value for the fieldbyte_
-
-
Method Details
-
getMemoryLayout
The memory layout of the native struct.- Returns:
- the memory layout
-
readData
public byte[] readData()Read the value of the fielddata
.- Returns:
- The value of the field
data
-
writeData
Write a value in the fielddata
.- Parameters:
data
- The new value for the fielddata
_arena
- to control the memory allocation scope
-
readSize
public int readSize()Read the value of the fieldsize
.- Returns:
- The value of the field
size
-
writeSize
public void writeSize(int size) Write a value in the fieldsize
.- Parameters:
size
- The new value for the fieldsize
-
readByte
public int readByte()Read the value of the fieldbyte
.- Returns:
- The value of the field
byte
-
writeByte
public void writeByte(int byte_) Write a value in the fieldbyte
.- Parameters:
byte_
- The new value for the fieldbyte
-
new_
Create a newGstByteReader
instance, which will read fromdata
.Free-function: gst_byte_reader_free
- Parameters:
data
- data from which theGstByteReader
should read- Returns:
- a new
GstByteReader
instance
-
dupData
Free-function: g_freeReturns a newly-allocated copy of the current data position if at least
size
bytes are left and updates the current position. Free with g_free() when no longer needed.- Parameters:
val
- address of aguint8
pointer variable in which to store the result- Returns:
true
if successful,false
otherwise.
-
dupStringUtf32
public boolean dupStringUtf32(int[] str) Free-function: g_freeReturns a newly-allocated copy of the current data position if there is a NUL-terminated UTF-32 string in the data (this could be an empty string as well), and advances the current position.
No input checking for valid UTF-32 is done. This function is endianness agnostic - you should not assume the UTF-32 characters are in host endianness.
This function will fail if no NUL-terminator was found in in the data.
Note: there is no peek or get variant of this function to ensure correct byte alignment of the UTF-32 string.
- Parameters:
str
- address of aguint32
pointer variable in which to store the result- Returns:
true
if a string could be read,false
otherwise. The string put intostr
must be freed with g_free() when no longer needed.
-
dupStringUtf8
Free-function: g_freeFIXME:Reads (copies) a NUL-terminated string in the
GstByteReader
instance, advancing the current position to the byte after the string. This will work for any NUL-terminated string with a character width of 8 bits, so ASCII, UTF-8, ISO-8859-N etc. No input checking for valid UTF-8 is done.This function will fail if no NUL-terminator was found in in the data.
- Parameters:
str
- address of agchar
pointer variable in which to store the result- Returns:
true
if a string could be read intostr
,false
otherwise. The string put intostr
must be freed with g_free() when no longer needed.
-
free
public void free()Frees aGstByteReader
instance, which was previously allocated by gst_byte_reader_new(). -
getData
Returns a constant pointer to the current data position if at leastsize
bytes are left and updates the current position.- Parameters:
val
- address of aguint8
pointer variable in which to store the result- Returns:
true
if successful,false
otherwise.
-
getFloat32Be
-
getFloat32Le
-
getFloat64Be
-
getFloat64Le
-
getInt16Be
-
getInt16Le
-
getInt24Be
-
getInt24Le
-
getInt32Be
-
getInt32Le
-
getInt64Be
-
getInt64Le
-
getInt8
-
getPos
public int getPos()Returns the current position of aGstByteReader
instance in bytes.- Returns:
- The current position of this ByteReader in bytes.
-
getRemaining
public int getRemaining()Returns the remaining number of bytes of aGstByteReader
instance.- Returns:
- The remaining number of bytes of this ByteReader instance.
-
getSize
public int getSize()Returns the total number of bytes of aGstByteReader
instance.- Returns:
- The total number of bytes of this ByteReader instance.
-
getStringUtf8
Returns a constant pointer to the current data position if there is a NUL-terminated string in the data (this could be just a NUL terminator), advancing the current position to the byte after the string. This will work for any NUL-terminated string with a character width of 8 bits, so ASCII, UTF-8, ISO-8859-N etc.No input checking for valid UTF-8 is done.
This function will fail if no NUL-terminator was found in in the data.
- Parameters:
str
- address of agchar
pointer variable in which to store the result- Returns:
true
if a string could be found,false
otherwise.
-
getSubReader
Initializes aGstByteReader
sub-reader instance to containsize
bytes of data from the current position of this ByteReader. This is useful to read chunked formats and make sure that one doesn't read beyond the size of the sub-chunk.Unlike gst_byte_reader_peek_sub_reader(), this function also modifies the position of this ByteReader and moves it forward by
size
bytes.- Parameters:
subReader
- aGstByteReader
instance to initialize as sub-readersize
- size ofsubReader
in bytes- Returns:
- FALSE on error or if this ByteReader does not contain
size
more bytes from the current position, and otherwise TRUE
-
getUint16Be
-
getUint16Le
-
getUint24Be
-
getUint24Le
-
getUint32Be
-
getUint32Le
-
getUint64Be
-
getUint64Le
-
getUint8
-
init
public void init(byte[] data) Initializes aGstByteReader
instance to read fromdata
. This function can be called on already initialized instances.- Parameters:
data
- data from which theGstByteReader
should read
-
maskedScanUint32
public int maskedScanUint32(int mask, int pattern, int offset, int size) Scan for patternpattern
with applied maskmask
in the byte reader data, starting from offsetoffset
relative to the current position.The bytes in
pattern
andmask
are interpreted left-to-right, regardless of endianness. All four bytes of the pattern must be present in the byte reader data for it to match, even if the first or last bytes are masked out.It is an error to call this function without making sure that there is enough data (offset+size bytes) in the byte reader.
- Parameters:
mask
- mask to apply to data before matching againstpattern
pattern
- pattern to match (after mask is applied)offset
- offset from which to start scanning, relative to the current positionsize
- number of bytes to scan from offset- Returns:
- offset of the first match, or -1 if no match was found.
Example:
// Assume the reader contains 0x00 0x01 0x02 ... 0xfe 0xff gst_byte_reader_masked_scan_uint32 (reader, 0xffffffff, 0x00010203, 0, 256); // -> returns 0 gst_byte_reader_masked_scan_uint32 (reader, 0xffffffff, 0x00010203, 1, 255); // -> returns -1 gst_byte_reader_masked_scan_uint32 (reader, 0xffffffff, 0x01020304, 1, 255); // -> returns 1 gst_byte_reader_masked_scan_uint32 (reader, 0xffff, 0x0001, 0, 256); // -> returns -1 gst_byte_reader_masked_scan_uint32 (reader, 0xffff, 0x0203, 0, 256); // -> returns 0 gst_byte_reader_masked_scan_uint32 (reader, 0xffff0000, 0x02030000, 0, 256); // -> returns 2 gst_byte_reader_masked_scan_uint32 (reader, 0xffff0000, 0x02030000, 0, 4); // -> returns -1
-
maskedScanUint32Peek
Scan for patternpattern
with applied maskmask
in the byte reader data, starting from offsetoffset
relative to the current position.The bytes in
pattern
andmask
are interpreted left-to-right, regardless of endianness. All four bytes of the pattern must be present in the byte reader data for it to match, even if the first or last bytes are masked out.It is an error to call this function without making sure that there is enough data (offset+size bytes) in the byte reader.
- Parameters:
mask
- mask to apply to data before matching againstpattern
pattern
- pattern to match (after mask is applied)offset
- offset from which to start scanning, relative to the current positionsize
- number of bytes to scan from offsetvalue
- pointer to uint32 to return matching data- Returns:
- offset of the first match, or -1 if no match was found.
-
peekData
Returns a constant pointer to the current data position if at leastsize
bytes are left and keeps the current position.- Parameters:
val
- address of aguint8
pointer variable in which to store the result- Returns:
true
if successful,false
otherwise.
-
peekFloat32Be
-
peekFloat32Le
-
peekFloat64Be
-
peekFloat64Le
-
peekInt16Be
-
peekInt16Le
-
peekInt24Be
-
peekInt24Le
-
peekInt32Be
-
peekInt32Le
-
peekInt64Be
-
peekInt64Le
-
peekInt8
-
peekStringUtf8
Returns a constant pointer to the current data position if there is a NUL-terminated string in the data (this could be just a NUL terminator). The current position will be maintained. This will work for any NUL-terminated string with a character width of 8 bits, so ASCII, UTF-8, ISO-8859-N etc.No input checking for valid UTF-8 is done.
This function will fail if no NUL-terminator was found in in the data.
- Parameters:
str
- address of agchar
pointer variable in which to store the result- Returns:
true
if a string could be skipped,false
otherwise.
-
peekSubReader
Initializes aGstByteReader
sub-reader instance to containsize
bytes of data from the current position of this ByteReader. This is useful to read chunked formats and make sure that one doesn't read beyond the size of the sub-chunk.Unlike gst_byte_reader_get_sub_reader(), this function does not modify the current position of this ByteReader.
- Parameters:
subReader
- aGstByteReader
instance to initialize as sub-readersize
- size ofsubReader
in bytes- Returns:
- FALSE on error or if this ByteReader does not contain
size
more bytes from the current position, and otherwise TRUE
-
peekUint16Be
-
peekUint16Le
-
peekUint24Be
-
peekUint24Le
-
peekUint32Be
-
peekUint32Le
-
peekUint64Be
-
peekUint64Le
-
peekUint8
-
setPos
public boolean setPos(int pos) Sets the new position of aGstByteReader
instance topos
in bytes.- Parameters:
pos
- The new position in bytes- Returns:
true
if the position could be set successfully,false
otherwise.
-
skip
public boolean skip(int nbytes) Skipsnbytes
bytes of theGstByteReader
instance.- Parameters:
nbytes
- the number of bytes to skip- Returns:
true
ifnbytes
bytes could be skipped,false
otherwise.
-
skipStringUtf16
public boolean skipStringUtf16()Skips a NUL-terminated UTF-16 string in theGstByteReader
instance, advancing the current position to the byte after the string.No input checking for valid UTF-16 is done.
This function will fail if no NUL-terminator was found in in the data.
- Returns:
true
if a string could be skipped,false
otherwise.
-
skipStringUtf32
public boolean skipStringUtf32()Skips a NUL-terminated UTF-32 string in theGstByteReader
instance, advancing the current position to the byte after the string.No input checking for valid UTF-32 is done.
This function will fail if no NUL-terminator was found in in the data.
- Returns:
true
if a string could be skipped,false
otherwise.
-
skipStringUtf8
public boolean skipStringUtf8()Skips a NUL-terminated string in theGstByteReader
instance, advancing the current position to the byte after the string. This will work for any NUL-terminated string with a character width of 8 bits, so ASCII, UTF-8, ISO-8859-N etc. No input checking for valid UTF-8 is done.This function will fail if no NUL-terminator was found in in the data.
- Returns:
true
if a string could be skipped,false
otherwise.
-