Initializes a new instance of the BytesIO class.
The initial binary buffer to populate the
BytesIO instance with. Defaults to an empty buffer.
Gets the raw, underlying Buffer instance managed by this
BytesIO object.
The underlying Buffer instance.
Gets the total size of the underlying buffer in bytes.
The byte length of the buffer.
Gets the current read/write pointer (offset index) within the buffer.
The current pointer position.
Reads binary data from the current pointer position. Advances the internal pointer by the number of bytes successfully read.
Optionallength:
number
The number of bytes to read. - If undefined or not
provided, reads all remaining bytes from the current pointer to the
end of the buffer. - If provided, reads at most
length bytes from the current pointer position.
A new Buffer containing the read bytes. If the pointer is out of
bounds or length is less than 1, returns an empty buffer.
Reads a signed 64-bit BigInt in big-endian format (BigInt64BE)
from the current pointer position. Automatically advances the pointer by the
specified size of bytes.
The number of bytes to advance the pointer after reading. Defaults to
8.
The read signed 64-bit BigInt (value ranges from -9,223,372,036,854,775,808n to 9,223,372,036,854,775,807n).
Reads a signed 64-bit BigInt in little-endian format
(BigInt64LE) from the current pointer position. Automatically
advances the pointer by the specified size of bytes.
The number of bytes to advance the pointer after reading. Defaults to
8.
The read signed 64-bit BigInt (value ranges from -9,223,372,036,854,775,808n to 9,223,372,036,854,775,807n).
Reads an unsigned 64-bit BigInt in big-endian format
(BigUInt64BE) from the current pointer position. Automatically
advances the pointer by the specified size of bytes.
The number of bytes to advance the pointer after reading. Defaults to
8.
The read unsigned 64-bit BigInt (value ranges from 0n to 18,446,744,073,709,551,615n).
Reads an unsigned 64-bit BigInt in little-endian format
(BigUInt64LE) from the current pointer position. Automatically
advances the pointer by the specified size of bytes.
The number of bytes to advance the pointer after reading. Defaults to
8.
The read unsigned 64-bit BigInt (value ranges from 0n to 18,446,744,073,709,551,615n).
Reads a 64-bit double-precision floating-point number in big-endian format
(DoubleBE) from the current pointer position. Automatically
advances the pointer by the specified size of bytes.
The number of bytes to advance the pointer after reading. Defaults to
8.
The read double-precision floating-point number.
Reads a 64-bit double-precision floating-point number in little-endian
format (DoubleLE) from the current pointer position.
Automatically advances the pointer by the specified size of
bytes.
The number of bytes to advance the pointer after reading. Defaults to
8.
The read double-precision floating-point number.
Reads a 32-bit single-precision floating-point number in big-endian format
(FloatBE) from the current pointer position. Automatically
advances the pointer by the specified size of bytes.
The number of bytes to advance the pointer after reading. Defaults to
4.
The read single-precision floating-point number.
Reads a 32-bit single-precision floating-point number in little-endian
format (FloatLE) from the current pointer position.
Automatically advances the pointer by the specified size of
bytes.
The number of bytes to advance the pointer after reading. Defaults to
4.
The read single-precision floating-point number.
Reads a signed 32-bit integer in big-endian format (Int32BE)
from the current pointer position. Automatically advances the pointer by the
specified size of bytes.
The number of bytes to advance the pointer after reading. Defaults to
4.
The read signed 32-bit integer (value ranges from -2,147,483,648 to 2,147,483,647).
Reads a signed 32-bit integer in little-endian format (Int32LE)
from the current pointer position. Automatically advances the pointer by the
specified size of bytes.
The number of bytes to advance the pointer after reading. Defaults to
4.
The read signed 32-bit integer (value ranges from -2,147,483,648 to 2,147,483,647).
Reads an unsigned 32-bit integer in big-endian format
(UInt32BE) from the current pointer position. Automatically
advances the pointer by the specified size of bytes.
The number of bytes to advance the pointer after reading. Defaults to
4.
The read unsigned 32-bit integer.
Reads an unsigned 32-bit integer in little-endian format
(UInt32LE) from the current pointer position. Automatically
advances the pointer by the specified size of bytes.
The number of bytes to advance the pointer after reading. Defaults to
4.
The read unsigned 32-bit integer (value ranges from 0 to 4,294,967,295).
Adjusts the position of the internal read/write pointer (offset cursor).
The byte offset relative to the reference point defined by
whence. - If whence is 0, must
be 0 or positive. - If whence is 2, must be
less than 0.
The reference point for the seek operation: -
0 (default): Absolute positioning from the start of the
buffer. - 1: Relative positioning from the current
pointer position. - 2: Relative positioning from the end
of the buffer (expects a negative offset).
The new pointer position (relative to the start of the buffer).
Returns a new BytesIO instance populated with a subarray/slice
of the current buffer. The slice is created using the standard
Buffer.subarray method, meaning it shares the same allocated
memory as the original buffer.
Arguments passed directly to the underlying
Buffer.subarray method. Typically
[start[, end]], where start is the starting
index (default 0) and end is the ending index (exclusive,
default buffer length).
A new BytesIO instance containing the sliced view of the buffer.
Returns a JSON representation of the underlying buffer. This is useful for
serializing the binary data, representing it as an object containing a
type property (typically "Buffer") and a
data array of byte values.
An object representing the buffer in JSON format.
Decodes the underlying buffer to a string according to the specified encoding.
Arguments passed directly to the underlying
Buffer.toString method. Typically
[encoding[, start[, end]]], where: -
encoding is the character encoding to use (e.g.,
'utf8', 'hex', 'base64'). -
start is the byte offset to start decoding (default 0). -
end is the byte offset to stop decoding (exclusive,
default buffer length).
The decoded string representation of the buffer.
Appends the provided binary data to the absolute end of the underlying buffer.
The Buffer containing the binary data to append.
The current BytesIO instance, enabling method chaining.
Staticalloc
Allocates a new BytesIO instance populated with a zero-filled
buffer of the specified size.
The number of bytes to allocate for the initial buffer.
A new BytesIO instance with the allocated buffer.
Staticconcat
Concatenates an array of Buffer instances into a single
BytesIO instance.
An array of Buffer objects to be joined together in
order.
A new BytesIO instance containing the concatenated binary data.
Staticfrom
Creates a new BytesIO instance from the given input data,
delegating to Buffer.from.
The input data. Can be a Buffer, Uint8Array,
ArrayBuffer, SharedArrayBuffer, an
Array of numbers, a string, or an object
with a valueOf or Symbol.toPrimitive method.
Optionalencode:
any
The character encoding to use if the input is a string
(e.g., 'utf8', 'hex',
'base64').
A new BytesIO instance initialized with the parsed input data.
A versatile in-memory binary stream class utilizing Node.js
Buffers. Mimics the behavior of Python's built-inio.BytesIOmodule, providing a stream-like interface for reading and writing binary data in memory.Supports sequential and random-access operations using a seekable internal pointer, and provides convenient helper methods to read and write integers, floats, doubles, and raw buffers.
Remarks
This class is highly optimized for performance and is particularly useful in environments requiring low-level binary manipulation, such as network protocols, file parsers, and custom serializers.
Example
Writing and Reading Data:
Example
Seeking the Stream Pointer: