Creates a new BinaryEncoder that can write to the given buffer.
Static initializers are also available:
Buffer to encode.
Initial offset to use (0 by default).
Endianness to use (little endian by default).
Buffer being read/written.
Total byte length of buffer.
Number of bytes between current offset and end of buffer.
Endianness currently being used to read/write data.
Whether current offset is out of the buffer's bounds.
Offset where data is currently being read/written.
Static
allocCreates a new buffer with the given size and returns a BinaryEncoder for it.
Size of buffer to encode.
Initial offset to use (0 by default).
Endianness to use (little endian by default).
If size is not a non-negative integer.
Static
dynamicallyCreates a BinaryEncoder whose buffer size will dynamically grow to fit all of the data that is written in the given function. When the function ends, the buffer will be cropped so that it contains the exact amount of bytes required.
Notes
Function to run in a dynamic sizing context.
Encoder created with dynamic size.
Creates a BinaryEncoder whose buffer size will dynamically grow to fit all of the data that is written in the given function. When the function ends, the buffer will be cropped so that it contains the exact amount of bytes required.
Notes
Configurations for creating the encoder.
Encoder created with dynamic size.
If minimumSize
is not a non-negative integer or chunkSize
is
not a positive integer.
Writes a single numerical value as a byte (alias for uint8).
Single byte value to write.
If buffer cannot fit 1 more byte.
Writes a string to the buffer using Base64 encoding.
The characters to write.
Use chars instead (use "base64" as second argument).
If buffer cannot fit number of specified byte(s).
Checks whether this encoder can safely consume the given number of bytes from the given offset (or the current offset if omitted), and if not, increases the size of the buffer by just enough to do so.
Notes
Number of bytes to check.
Optional
fromOffset: numberOffset to check from, if different than current one.
Number of bytes that were added, if any.
If bytes or offset is negative (cannot add bytes to start).
Returns a new BinaryDecoder using this coder's buffer.
Initial offset to use (0 by default).
Endianness to use (little endian by default).
New decoder for this coder's buffer.
Returns a new BinaryEncoder using this coder's buffer.
Initial offset to use (0 by default).
Endianness to use (little endian by default).
New encoder for this coder's buffer.
Returns whether this coder can safely consume the given number of bytes from the given offset (or the current offset if omitted).
Number of bytes to check.
Optional
fromOffset: numberOffset to check from, if different than current one.
True if these bytes can be safely consumed, false otherwise.
Increases the size of the buffer by the given number of bytes.
Notes
Bytes to increase buffer size by.
If bytes
is not a non-negative integer.
Returns whether or not the offset is at the end of the file (i.e. reading anything will cause an exception).
True if offset is at EOF, false otherwise.
Use isOutOfBounds getter instead.
Preserves the original offset after calling the given function.
Use saveOffset method instead.
Moves the offset to a specific byte.
The value to set the offset to.
Moves the offset to a specific byte.
The value to set the offset to.
Passing bigints is no longer supported.
Sets the endianness to use for reading/writing.
Endianness to use.
Advances the offset by the given number of bytes.
The number of bytes to skip.
The new offset after skipping bytes.
Advances the offset by the given number of bytes.
The number of bytes to skip.
The new offset after skipping bytes.
Passing bigints is no longer supported.
Returns the current offset.
The current offset.
Use offset getter instead.
Writes a string to the buffer using the given encoding, then writes a null byte immediately after to terminate it.
The string to write.
Character encoding to write with ("utf-8" by default).
If buffer cannot fit number of specified byte(s).
Writes a 16-bit unsigned integer. Consumes 2 bytes.
The UInt16 to write.
If buffer cannot fit 2 more bytes.
Combines the given values using the given bit masks and writes them as a single 16-bit unsigned integer. Consumes 2 bytes.
// writing a flag and value
encoder.uint16({ bits: [1, 15], values: [1, 50] });
Values and the bit masks to use when combining them.
If buffer cannot fit 2 more bytes, if bit masks are not valid, or if values cannot fit into bit masks.
Writes a 32-bit unsigned integer. Consumes 4 bytes.
The UInt32 to write.
If buffer cannot fit 4 more bytes.
Combines the given values using the given bit masks and writes them as a single 32-bit unsigned integer. Consumes 4 bytes.
// writing a flag and value
encoder.uint32({ bits: [1, 31], values: [1, 50] });
Values and the bit masks to use when combining them.
If buffer cannot fit 4 more bytes, if bit masks are not valid, or if values cannot fit into bit masks.
Writes a 64-bit unsigned integer. Consumes 8 bytes.
The UInt64 to write.
If buffer cannot fit 8 more bytes.
Combines the given values using the given bit masks and writes them as a single 64-bit unsigned integer. Consumes 8 bytes.
// writing a flag and value (mixing numbers and bigints is fine)
encoder.uint64({ bits: [1, 63], values: [1, 50n] });
Values and the bit masks to use when combining them.
If buffer cannot fit 8 more bytes, if bit masks are not valid, or if values cannot fit into bit masks.
Writes an 8-bit unsigned integer. Consumes 1 byte.
The UInt8 to write.
If buffer cannot fit 1 more byte.
Combines the given values using the given bit masks and writes them as a single 8-bit unsigned integer. Consumes 1 byte.
// writing a flag and value
encoder.uint8({ bits: [1, 7], values: [1, 50] });
Values and the bit masks to use when combining them.
If buffer cannot fit 1 more byte, if bit masks are not valid, or if values cannot fit into bit masks.
Runs a function in which the encoder's size can dynamically grow to fit the data written. When the function ends, any unused bytes that did not exist before calling this function will be pruned.
Notes
Function to run in a dynamic sizing context.
Runs a function in which the encoder's size can dynamically grow to fit the data written. When the function ends, any unused bytes that did not exist before calling this function will be pruned.
Notes
Number of bytes to add on overflow (256 by default).
Function to run in a dynamic sizing context.
If chunkSize
is not a positive integer.
Temporarily sets the endianness to the given value, calls the given function, and then restores the original endianness.
Endianness to use in scope of function.
Function to call.
Result of the given function call.
Encodes binary data to a buffer.