DataBuffer
The DataBuffer
class is a wrapper around DataView
that adds functionality for cursor tracking, automatic buffer expansion, and more.
Tip
DataBuffer
automatically adjusts the cursor position during writes and reads. In practice, when creating custom types, you don’t need to worry about it.
Usage
import { DataBuffer } from "encodexx";
const buf = new DataBuffer();
buf.writeFloat64(12.4325);
buf.resetCursor();
console.log(buf.readFloat64());
Danger
Do not use resetCursor
on DataBuffer
instances obtained while creating custom types. This may lead to serialization errors and data corruption.
Use Cases
Encodexx returns a DataBuffer
as a buffer for data serialization and deserialization. You can also use it to create custom types that interact directly with the buffer.
Creating a DataBuffer from an ArrayBuffer
You can pass an ArrayBuffer
to the class constructor.
import { DataBuffer } from "encodexx";import { serializer } from "./schema.ts";
const arrayBuffer = new ArrayBuffer(200);// Do something with arrayBufferconst buffer = new DataBuffer(arrayBuffer);
Sending DataBuffer Over the Network
To send a DataBuffer
over the network, you need to convert it into an ArrayBuffer
or Uint8Array
.
import { serializer } from "./schema.ts";const body = serializer.encode(data).buffer; // Obtaining the ArrayBuffer
fetch(url, { method: "POST", body, headers: { // Indicate that binary data is being sent "Content-Type": "application/octet-stream", },});
To send data from a server (for example, using Express), use Node.js’s built-in Buffer
class:
import { serializer, getResponse } from "./schema.ts";
app.post("/", (req, res) => { const response = getResponse();
// Obtain the result as a Uint8Array and convert it to a Buffer const encoded = Buffer.from(serializer.encode(response).uint8Array);
res.set("Content-Type", "application/octet-stream"); res.send(encoded);});
API
constructor(newBuffer?: ArrayBuffer, size?: number)
- Constructor for a new instance. You can specify an initial buffer or initial size.
By default, DataBuffer
allocates 1 KB initially and increases in size as needed. You can change this value:
new DataBuffer(undefined, 1024 * 1024); // Immediately allocates 1 MB
Info
Encodexx automatically trims the buffer to the required size.
get length(): number;
- Gets the length of the used portion of the buffer.get buffer(): ArrayBuffer;
- Retrieves the used portion of the buffer.get uint8Array(): Uint8Array;
- Retrieves the used portion of the buffer asUint8Array
.get cursor(): number;
- Gets the current cursor position.resetCursor(): void;
- Resets the cursor position to 0.
writeBuffer(buffer: ArrayBuffer | Uint8Array): void;
- Writes an ArrayBuffer or Uint8Array of arbitrary length.readBuffer(length: number): ArrayBuffer;
- Reads an ArrayBuffer of the specified length.
writeUleb128(value: bigint): void;
- Writes a number in uleb128 format.readUleb128(): bigint;
- Reads a number in uleb128 format.
writeSleb128(value: bigint): void;
- Writes a number in sleb128 format.readSleb128(): bigint;
- Reads a number in sleb128 format.
writeInt8(val: number): void;
- Writes a number in int8 format.readInt8(): number;
- Reads a number in int8 format.
writeUint8(val: number): void;
- Writes a number in uint8 format.readUint8(): number;
- Reads a number in uint8 format.
writeInt16(val: number): void;
- Writes a number in int16 format.readInt16(): number;
- Reads a number in int16 format.
writeUint16(val: number): void;
- Writes a number in uint16 format.readUint16(): number;
- Reads a number in uint16 format.
writeInt32(val: number): void;
- Writes a number in int32 format.readInt32(): number;
- Reads a number in int32 format.
writeUint32(val: number): void;
- Writes a number in uint32 format.readUint32(): number;
- Reads a number in uint32 format.
writeFloat32(val: number): void;
- Writes a number in float32 format.readFloat32(): number;
- Reads a number in float32 format.
writeFloat64(val: number): void;
- Writes a number in float64 format.readFloat64(): number;
- Reads a number in float64 format.
writeBigInt64(val: bigint): void;
- Writes a number in bigint64 format.readBigInt64(): bigint;
- Reads a number in bigint64 format.
writeBigUint64(val: bigint): void;
- Writes a number in biguint64 format.readBigUint64(): bigint;
- Reads a number in biguint64 format.
writeString(val: string): void;
- Writes a string.readString(): string;
- Reads a string.
writeBoolean(val: boolean): void;
- Writes a boolean value:FF
fortrue
and00
forfalse
.readBoolean(): boolean;
- Reads a boolean value.
writeDate(val: Date): void;
- Writes a date.readDate(): Date;
- Reads a date.