// Write string to buffer
const buffer = $bytes.newBuffer($toBytes("Hello"));
buffer.writeString(", world!");
// Get buffer content
const bufferContent = $toString(buffer.bytes());
console.log(bufferContent); // Hello, world!
// Create a new buffer string
const strBuffer = $bytes.newBufferString("String buffer");
strBuffer.writeString(" test");
const strBufferContent = strBuffer.string();
console.log(strBufferContent); // String buffer test
// Create a byte reader
const reader = $bytes.newReader($toBytes("Bytes reader test"));
const readerBuffer = new Uint8Array(100); // Empty buffer
const bytesRead = reader.read(readerBuffer); // Read into buffer
console.log(bytesRead, "bytes read") // 17 bytes read
const readerContent = $toString(readerBuffer.subarray(0, bytesRead));
console.log(readerContent); // Bytes reader test
// Buffer methods
const testBuffer = $bytes.newBuffer($toBytes(""));
testBuffer.writeString("Test");
testBuffer.writeByte(32); // Space
testBuffer.writeString("methods");
const testBufferContent = testBuffer.string();
console.log(testBufferContent); // Test methods
// Read methods
const readBuffer = $bytes.newBuffer($toBytes("Read test"));
const readByte = readBuffer.readByte();
console.log("Read byte:", String.fromCharCode(readByte)); // Read byte: R
const nextBytes = new Uint8Array(5);
readBuffer.read(nextBytes);
console.log("Next bytes:", $toString(nextBytes)); // Next bytes: ead t