or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

buffer-conversion.mdbuffer-management.mdfloating-point-operations.mdindex.mdinteger-operations.mdstring-operations.mdvarint-operations.md
tile.json

buffer-management.mddocs/

Buffer Management

Core buffer creation, resizing, and management operations for working with binary data containers. These operations handle buffer lifecycle, position management, and data manipulation.

Capabilities

Static Factory Methods

Create ByteBuffer instances using various factory methods.

/**
 * Allocate a new ByteBuffer with specified capacity
 * @param {number} capacity - Initial capacity in bytes (default: 16)
 * @param {boolean} littleEndian - Use little endian byte order (default: false)
 * @param {boolean} noAssert - Skip assertions for performance (default: false)
 * @returns {ByteBuffer} New ByteBuffer instance
 */
ByteBuffer.allocate(capacity, littleEndian, noAssert);

/**
 * Wrap existing buffer or string data
 * @param {Buffer|ArrayBuffer|Uint8Array|string|Array} buffer - Data to wrap
 * @param {string} encoding - String encoding ('base64', 'hex', 'binary', 'utf8', 'debug')
 * @param {boolean} littleEndian - Use little endian byte order (default: false)
 * @param {boolean} noAssert - Skip assertions for performance (default: false)
 * @returns {ByteBuffer} New ByteBuffer wrapping the data
 */
ByteBuffer.wrap(buffer, encoding, littleEndian, noAssert);

/**
 * Concatenate multiple buffers into a single ByteBuffer
 * @param {Array<ByteBuffer|Buffer|ArrayBuffer|string>} buffers - Buffers to concatenate
 * @param {string} encoding - String encoding for string buffers
 * @param {boolean} littleEndian - Use little endian byte order (default: false)
 * @param {boolean} noAssert - Skip assertions for performance (default: false)
 * @returns {ByteBuffer} New ByteBuffer containing concatenated data
 */
ByteBuffer.concat(buffers, encoding, littleEndian, noAssert);

/**
 * Test if object is a ByteBuffer instance
 * @param {*} bb - Object to test
 * @returns {boolean} True if object is a ByteBuffer
 */
ByteBuffer.isByteBuffer(bb);

Usage Examples:

const ByteBuffer = require("bytebuffer");

// Allocate new buffer
const bb1 = ByteBuffer.allocate(32, true); // 32 bytes, little endian

// Wrap existing data
const bb2 = ByteBuffer.wrap([1, 2, 3, 4]);
const bb3 = ByteBuffer.wrap("SGVsbG8gV29ybGQ=", "base64");

// Concatenate buffers
const combined = ByteBuffer.concat([bb1, bb2, bb3]);

// Type checking
if (ByteBuffer.isByteBuffer(combined)) {
    console.log("It's a ByteBuffer!");
}

Constructor

Create ByteBuffer instances directly using the constructor.

/**
 * ByteBuffer constructor
 * @param {number} capacity - Initial capacity in bytes (default: 16)
 * @param {boolean} littleEndian - Use little endian byte order (default: false)
 * @param {boolean} noAssert - Skip assertions for performance (default: false)
 */
new ByteBuffer(capacity, littleEndian, noAssert);

Buffer Properties and Information

Access buffer properties and get information about the buffer state.

/**
 * Get the capacity of the backing buffer
 * @returns {number} Buffer capacity in bytes
 */
capacity();

/**
 * Get the number of remaining readable bytes
 * @returns {number} Bytes remaining between offset and limit
 */
remaining();

/**
 * Get the accessor type being used
 * @returns {function} Buffer constructor (Buffer, Uint8Array, or DataView)
 */
ByteBuffer.accessor();

/**
 * Get the type of backing buffer
 * @returns {string} "arraybuffer" or "buffer"
 */
ByteBuffer.type();

Buffer Resizing and Capacity Management

Manage buffer size and ensure adequate capacity for operations.

/**
 * Resize backing buffer to new capacity
 * @param {number} capacity - New capacity in bytes
 * @returns {ByteBuffer} This ByteBuffer for chaining
 */
resize(capacity);

/**
 * Ensure buffer has at least the specified capacity
 * @param {number} capacity - Minimum required capacity
 * @returns {ByteBuffer} This ByteBuffer for chaining
 */
ensureCapacity(capacity);

Usage Examples:

const bb = ByteBuffer.allocate(16);

// Check capacity
console.log(bb.capacity()); // 16

// Ensure we have enough space
bb.ensureCapacity(64);      // Resizes if needed
console.log(bb.capacity()); // 64

// Manual resize
bb.resize(128);
console.log(bb.capacity()); // 128

Position and Limit Management

Control the buffer's position and limit for reading and writing operations.

/**
 * Clear the buffer by resetting offset to 0 and limit to capacity
 * @returns {ByteBuffer} This ByteBuffer for chaining
 */
clear();

/**
 * Flip buffer from write mode to read mode
 * Sets limit to current offset and offset to 0
 * @returns {ByteBuffer} This ByteBuffer for chaining
 */
flip();

/**
 * Mark the current position for later reset
 * @param {number} offset - Position to mark (default: current offset)
 * @returns {ByteBuffer} This ByteBuffer for chaining
 */
mark(offset);

/**
 * Reset offset to the marked position
 * @returns {ByteBuffer} This ByteBuffer for chaining
 */
reset();

/**
 * Skip bytes by moving the offset forward or backward
 * @param {number} length - Number of bytes to skip (can be negative)
 * @returns {ByteBuffer} This ByteBuffer for chaining
 */
skip(length);

Usage Examples:

const bb = ByteBuffer.allocate(32);

// Write some data
bb.writeInt32(42);
bb.writeInt32(84);

// Prepare for reading
bb.flip();
console.log(bb.offset); // 0
console.log(bb.limit);  // 8

// Mark position and read
bb.mark();
const value1 = bb.readInt32(); // 42
bb.reset();
const value2 = bb.readInt32(); // 42 again

// Skip and read
bb.skip(4);
const value3 = bb.readInt32(); // 84

Buffer Manipulation Operations

Operations for copying, cloning, and manipulating buffer contents.

/**
 * Create a copy of buffer contents
 * @param {number} begin - Start offset (default: 0)
 * @param {number} end - End offset (default: limit)
 * @returns {ByteBuffer} New ByteBuffer with copied data
 */
copy(begin, end);

/**
 * Create a clone of this ByteBuffer
 * @param {boolean} copy - Whether to copy buffer contents (default: false)
 * @returns {ByteBuffer} Cloned ByteBuffer
 */
clone(copy);

/**
 * Create a slice view of this buffer
 * @param {number} begin - Start offset (default: offset)
 * @param {number} end - End offset (default: limit)
 * @returns {ByteBuffer} New ByteBuffer slice
 */
slice(begin, end);

/**
 * Copy data to another ByteBuffer
 * @param {ByteBuffer} target - Target ByteBuffer
 * @param {number} targetOffset - Target offset (default: target.offset)
 * @param {number} sourceOffset - Source offset (default: this.offset)
 * @param {number} sourceLimit - Source limit (default: this.limit)
 * @returns {ByteBuffer} This ByteBuffer for chaining
 */
copyTo(target, targetOffset, sourceOffset, sourceLimit);

/**
 * Reverse buffer contents
 * @param {number} begin - Start offset (default: offset)
 * @param {number} end - End offset (default: limit)
 * @returns {ByteBuffer} This ByteBuffer for chaining
 */
reverse(begin, end);

/**
 * Compact buffer by moving remaining data to beginning
 * @param {number} begin - Start offset (default: offset)
 * @param {number} end - End offset (default: limit)
 * @returns {ByteBuffer} This ByteBuffer for chaining
 */
compact(begin, end);

/**
 * Fill buffer range with specified value
 * @param {number} value - Byte value to fill with (0-255)
 * @param {number} begin - Start offset (default: offset)
 * @param {number} end - End offset (default: limit)
 * @returns {ByteBuffer} This ByteBuffer for chaining
 */
fill(value, begin, end);

Usage Examples:

const bb = ByteBuffer.allocate(16);
bb.writeInt32(42);
bb.writeInt32(84);

// Create a copy
const copy = bb.copy();

// Create a slice
bb.flip();
const slice = bb.slice(0, 4); // First 4 bytes

// Reverse contents
bb.reverse();

// Fill with zeros
bb.clear();
bb.fill(0);

// Compact buffer
bb.writeInt32(42);
bb.skip(8); // Move past some data
bb.writeInt32(84);
bb.compact(); // Move remaining data to beginning

Data Appending and Prepending

Add data to the beginning or end of the buffer.

/**
 * Append data to the buffer
 * @param {ByteBuffer|Buffer|ArrayBuffer|Uint8Array|string|number} source - Data to append
 * @param {string} encoding - String encoding if source is string
 * @param {number} offset - Offset to append at (default: current offset)
 * @returns {ByteBuffer} This ByteBuffer for chaining
 */
append(source, encoding, offset);

/**
 * Append this buffer to another ByteBuffer
 * @param {ByteBuffer} target - Target ByteBuffer
 * @param {number} offset - Offset in target (default: target.offset)
 * @returns {ByteBuffer} This ByteBuffer for chaining
 */
appendTo(target, offset);

/**
 * Prepend data to the buffer
 * @param {ByteBuffer|Buffer|ArrayBuffer|Uint8Array|string|number} source - Data to prepend
 * @param {string} encoding - String encoding if source is string
 * @param {number} offset - Offset to prepend at (default: current offset)
 * @returns {ByteBuffer} This ByteBuffer for chaining
 */
prepend(source, encoding, offset);

/**
 * Prepend this buffer to another ByteBuffer
 * @param {ByteBuffer} target - Target ByteBuffer
 * @param {number} offset - Offset in target (default: target.offset)
 * @returns {ByteBuffer} This ByteBuffer for chaining
 */
prependTo(target, offset);

Configuration Methods

Configure buffer behavior and settings.

/**
 * Enable or disable assertions
 * @param {boolean} assert - Enable assertions
 * @returns {ByteBuffer} This ByteBuffer for chaining
 */
assert(assert);

/**
 * Set byte order
 * @param {boolean} littleEndian - Use little endian byte order
 * @returns {ByteBuffer} This ByteBuffer for chaining
 */
order(littleEndian);

/**
 * Switch to little endian byte order
 * @param {boolean} littleEndian - Enable little endian (default: true)
 * @returns {ByteBuffer} This ByteBuffer for chaining
 */
LE(littleEndian);

/**
 * Switch to big endian byte order
 * @param {boolean} bigEndian - Enable big endian (default: true)
 * @returns {ByteBuffer} This ByteBuffer for chaining
 */
BE(bigEndian);

Usage Examples:

const bb = ByteBuffer.allocate(16);

// Configure endianness
bb.LE();  // Switch to little endian
bb.writeInt32(0x12345678);

bb.BE();  // Switch to big endian
bb.writeInt32(0x12345678);

// Enable assertions for debugging
bb.assert(true);

// Chain operations
bb.clear()
  .LE()
  .writeInt32(42)
  .writeFloat(3.14)
  .flip();