Skip to content

Buffers and typed arrays ​

ByteArray, Buffer, the typed arrays and their encodings.

Always available

No import needed. These names are in scope in every file.

TextEncoding enum ​

ts
enum TextEncoding

Text encoding accepted by ByteArray.from(string, ...), toString, readString and writeString. The runtime rejects unknown encodings and non-string arguments at the call site.

  • TextEncoding.UTF8: UTF-8. The default for both decode and encode.
  • TextEncoding.ASCII: 7-bit ASCII; bytes above 127 decode to ?.
  • TextEncoding.LATIN1: ISO-8859-1; one byte per code point, no transformation.
  • TextEncoding.HEX: Lowercase hexadecimal; two characters per byte.
  • TextEncoding.BASE64: Standard base64.

BufferEncoding enum ​

ts
enum BufferEncoding

Buffer's old name for TextEncoding; the same enum.

  • BufferEncoding.UTF8: UTF-8. The default for both decode and encode.
  • BufferEncoding.ASCII: 7-bit ASCII; bytes above 127 decode to ?.
  • BufferEncoding.LATIN1: ISO-8859-1; one byte per code point, no transformation.
  • BufferEncoding.HEX: Lowercase hexadecimal; two characters per byte.
  • BufferEncoding.BASE64: Standard base64.

Endian enum ​

ts
enum Endian

Byte order of a ByteArray's multi-byte reads and writes.

  • Endian.LITTLE: Least significant byte first. The default; matches the machines and the GPU.
  • Endian.BIG: Most significant byte first.

ByteArray class ​

ts
class ByteArray

A growable sequence of bytes, held by reference: everything holding it sees the same bytes. Streamed read*/write* move position; get*/set* take a byte offset and leave it alone. A read past the end and an offset out of range are RangeErrors; a write past the end grows the array.

new ByteArray() ​

ts
constructor(size?: int)

size zero bytes (default 0).

ByteArray.length ​

ts
length: int

Length in bytes. Setting it grows (zero-filled) or truncates.

ByteArray.byteLength ​

ts
readonly byteLength: int

Buffer's spelling of length. Read-only.

ByteArray.position ​

ts
position: int

The cursor for the streamed read*/write* methods.

ByteArray.bytesAvailable ​

ts
readonly bytesAvailable: int

length - position.

ByteArray.endian ​

ts
endian: Endian

Byte order for multi-byte values. Endian.LITTLE by default.

ByteArray.readInt8 ​

ts
readInt8(): int

Read a signed 8-bit integer at position; advances position. @returns The value.

ByteArray.readUInt8 ​

ts
readUInt8(): uint

Read an unsigned 8-bit integer at position; advances position. @returns The value.

ByteArray.readInt16 ​

ts
readInt16(): int

Read a signed 16-bit integer at position in the set byte order; advances position. @returns The value.

ByteArray.readUInt16 ​

ts
readUInt16(): uint

Read an unsigned 16-bit integer at position in the set byte order; advances position. @returns The value.

ByteArray.readInt32 ​

ts
readInt32(): int

Read a signed 32-bit integer at position in the set byte order; advances position. @returns The value.

ByteArray.readUInt32 ​

ts
readUInt32(): uint

Read an unsigned 32-bit integer at position in the set byte order; advances position. @returns The value.

ByteArray.readFloat32 ​

ts
readFloat32(): float

Read a 32-bit float at position in the set byte order; advances position. @returns The value.

ByteArray.readFloat64 ​

ts
readFloat64(): float64

Read a 64-bit float at position in the set byte order; advances position. @returns The value.

ByteArray.readBool ​

ts
readBool(): boolean

Read a boolean (as a byte) at position; advances position. @returns True if the byte is nonzero.

ByteArray.writeInt8 ​

ts
writeInt8(value: int): void

Write a signed 8-bit integer at position; advances position. @param value The value to write.

ByteArray.writeUInt8 ​

ts
writeUInt8(value: uint): void

Write an unsigned 8-bit integer at position; advances position. @param value The value to write.

ByteArray.writeInt16 ​

ts
writeInt16(value: int): void

Write a signed 16-bit integer at position in the set byte order; advances position. @param value The value to write.

ByteArray.writeUInt16 ​

ts
writeUInt16(value: uint): void

Write an unsigned 16-bit integer at position in the set byte order; advances position. @param value The value to write.

ByteArray.writeInt32 ​

ts
writeInt32(value: int): void

Write a signed 32-bit integer at position in the set byte order; advances position. @param value The value to write.

ByteArray.writeUInt32 ​

ts
writeUInt32(value: float64): void

Write an unsigned 32-bit integer at position in the set byte order; advances position. @param value The value to write.

ByteArray.writeFloat32 ​

ts
writeFloat32(value: float64): void

Write a 32-bit float at position in the set byte order; advances position. @param value The value to write.

ByteArray.writeFloat64 ​

ts
writeFloat64(value: float64): void

Write a 64-bit float at position in the set byte order; advances position. @param value The value to write.

ByteArray.writeBool ​

ts
writeBool(value: boolean): void

Write a boolean at position as a single byte; advances position. @param value Nonzero values write as 1.

ByteArray.getInt8 ​

ts
getInt8(offset: int): int

Read a signed 8-bit integer at offset; does not change position. @param offset Byte offset in the array. @returns The value.

ByteArray.getUInt8 ​

ts
getUInt8(offset: int): uint

Read an unsigned 8-bit integer at offset; does not change position. @param offset Byte offset in the array. @returns The value.

ByteArray.getInt16 ​

ts
getInt16(offset: int): int

Read a signed 16-bit integer at offset in the set byte order; does not change position. @param offset Byte offset in the array. @returns The value.

ByteArray.getUInt16 ​

ts
getUInt16(offset: int): uint

Read an unsigned 16-bit integer at offset in the set byte order; does not change position. @param offset Byte offset in the array. @returns The value.

ByteArray.getInt32 ​

ts
getInt32(offset: int): int

Read a signed 32-bit integer at offset in the set byte order; does not change position. @param offset Byte offset in the array. @returns The value.

ByteArray.getUInt32 ​

ts
getUInt32(offset: int): uint

Read an unsigned 32-bit integer at offset in the set byte order; does not change position. @param offset Byte offset in the array. @returns The value.

ByteArray.getFloat32 ​

ts
getFloat32(offset: int): float

Read a 32-bit float at offset in the set byte order; does not change position. @param offset Byte offset in the array. @returns The value.

ByteArray.getFloat64 ​

ts
getFloat64(offset: int): float64

Read a 64-bit float at offset in the set byte order; does not change position. @param offset Byte offset in the array. @returns The value.

ByteArray.getBool ​

ts
getBool(offset: int): boolean

Read a boolean at offset as a single byte; does not change position. @param offset Byte offset in the array. @returns True if the byte is nonzero.

ByteArray.setInt8 ​

ts
setInt8(offset: int, value: int): void

Write a signed 8-bit integer at offset; does not change position. @param offset Byte offset in the array. @param value The value to write.

ByteArray.setUInt8 ​

ts
setUInt8(offset: int, value: uint): void

Write an unsigned 8-bit integer at offset; does not change position. @param offset Byte offset in the array. @param value The value to write.

ByteArray.setInt16 ​

ts
setInt16(offset: int, value: int): void

Write a signed 16-bit integer at offset in the set byte order; does not change position. @param offset Byte offset in the array. @param value The value to write.

ByteArray.setUInt16 ​

ts
setUInt16(offset: int, value: uint): void

Write an unsigned 16-bit integer at offset in the set byte order; does not change position. @param offset Byte offset in the array. @param value The value to write.

ByteArray.setInt32 ​

ts
setInt32(offset: int, value: int): void

Write a signed 32-bit integer at offset in the set byte order; does not change position. @param offset Byte offset in the array. @param value The value to write.

ByteArray.setUInt32 ​

ts
setUInt32(offset: int, value: float64): void

Write an unsigned 32-bit integer at offset in the set byte order; does not change position. @param offset Byte offset in the array. @param value The value to write.

ByteArray.setFloat32 ​

ts
setFloat32(offset: int, value: float64): void

Write a 32-bit float at offset in the set byte order; does not change position. @param offset Byte offset in the array. @param value The value to write.

ByteArray.setFloat64 ​

ts
setFloat64(offset: int, value: float64): void

Write a 64-bit float at offset in the set byte order; does not change position. @param offset Byte offset in the array. @param value The value to write.

ByteArray.setBool ​

ts
setBool(offset: int, value: boolean): void

Write a boolean at offset as a single byte; does not change position. @param offset Byte offset in the array. @param value Nonzero values write as 1.

ByteArray.readBytes ​

ts
readBytes(dest: ByteArray, offset?: int, length?: int): void

Copy length bytes from position into dest at offset (default 0), growing dest; length 0 or omitted means all the bytes available. Advances position.

ByteArray.writeBytes ​

ts
writeBytes(src: ByteArray, offset?: int, length?: int): void

Copy length bytes of src starting at offset (default 0) in at position, growing this array; length 0 or omitted means the rest of src. Advances position.

ByteArray.readUTFBytes ​

ts
readUTFBytes(length: int): string

Read length bytes as UTF-8 text.

ByteArray.writeUTFBytes ​

ts
writeUTFBytes(text: string): void

Write text as UTF-8 bytes.

ByteArray.readString ​

ts
readString(length: int, encoding?: TextEncoding): string

Read length bytes as text in encoding (default TextEncoding.UTF8).

ByteArray.writeString ​

ts
writeString(text: string, encoding?: TextEncoding): void

Write text encoded in encoding (default TextEncoding.UTF8).

ByteArray.clear ​

ts
clear(): void

Length 0 and position 0.

ByteArray.slice ​

ts
slice(start?: int, end?: int): ByteArray

Return a fresh array copying bytes in the half-open range [start, end). end defaults to length. Out-of-range indices are clamped; an empty range yields an empty array.

ByteArray.toString ​

ts
toString(encoding?: TextEncoding): string

Decode the whole array to a string. encoding defaults to TextEncoding.UTF8. Throws on unknown encodings or non-string arguments.

ByteArray.from ​

ts
static from(data: any, encoding?: TextEncoding): ByteArray

Build an array from data. An Array<int> is treated as a byte sequence (each element masked to 8 bits). A ByteArray is copied. A typed array is copied element by element, each masked to 8 bits. A numeric FixedArray copies its bytes, packed at native width, in little-endian order. A string is encoded using encoding (default TextEncoding.UTF8). Throws if encoding is provided but is not a string. Other input types yield an empty array.

Example

ts
const bytes: ByteArray = ByteArray.from("hi");
const buf: Buffer = Buffer.from("hi");
console.log(bytes.length, buf.length); // 2 2

Buffer class ​

ts
class Buffer extends ByteArray

The old name of ByteArray: an alias of the same type, kept so existing code compiles unchanged. Buffer.alloc(n), Buffer.from(...), byteLength and BufferEncoding all work on it. Prefer ByteArray.

Buffer.alloc ​

ts
static alloc(size: int): ByteArray

new ByteArray(size): a zero-filled array of size bytes.

TypedArray class ​

ts
class TypedArray

Type only: the runtime hands you instances; you never construct it yourself.

The type name for "any typed array".

All eight spellings use the fixed-array runtime representation. t is TypedArray is true for every one. t is Uint8Array is true exactly when the element type is uint8, and likewise for the other seven spellings.

TypedArray.length ​

ts
readonly length: int

Number of elements in the view.

TypedArray.byteLength ​

ts
readonly byteLength: int

Total byte size of the view.

TypedArray.set ​

ts
set(source: any, offset?: int): void

Copy source (an array or another typed array) in starting at offset (default 0), converting each element to this element type. Throws RangeError when source.length + offset exceeds length or offset is negative.

Parameters

  • source: Array or typed array whose elements to copy.
  • offset: Optional starting index in this array; defaults to 0.

Int8Array class ​

ts
class Int8Array extends TypedArray

Type only: the runtime hands you instances; you never construct it yourself.

Old-grammar declaration for the FixedArray<int8> alias. new Int8Array(x) is compiler-lowered to __fa_construct; no runtime global exists.

new Int8Array() ​

ts
constructor()
constructor(length: int)
constructor(source: ByteArray)
constructor(source: TypedArray)
constructor(values: Array<int>)

Create an empty typed array.

Int8Array.slice ​

ts
slice(begin?: int, end?: int): Int8Array

A COPY of elements [begin, end) in new storage. Negative indices count from the end; both clamp to the length.

Parameters

  • begin: Optional starting index; defaults to 0.
  • end: Optional ending index (exclusive); defaults to length.

Returns: A new Int8Array containing the copied elements.

Uint8Array class ​

ts
class Uint8Array extends TypedArray

Type only: the runtime hands you instances; you never construct it yourself.

Old-grammar declaration for the FixedArray<uint8> alias. new Uint8Array(x) is compiler-lowered to __fa_construct; no runtime global exists.

new Uint8Array() ​

ts
constructor()
constructor(length: int)
constructor(source: ByteArray)
constructor(source: TypedArray)
constructor(values: Array<int>)

Create an empty typed array.

Uint8Array.slice ​

ts
slice(begin?: int, end?: int): Uint8Array

A COPY of elements [begin, end) in new storage. Negative indices count from the end; both clamp to the length.

Parameters

  • begin: Optional starting index; defaults to 0.
  • end: Optional ending index (exclusive); defaults to length.

Returns: A new Uint8Array containing the copied elements.

Int16Array class ​

ts
class Int16Array extends TypedArray

Type only: the runtime hands you instances; you never construct it yourself.

Old-grammar declaration for the FixedArray<int16> alias. new Int16Array(x) is compiler-lowered to __fa_construct; no runtime global exists.

new Int16Array() ​

ts
constructor()
constructor(length: int)
constructor(source: ByteArray)
constructor(source: TypedArray)
constructor(values: Array<int>)

Create an empty typed array.

Int16Array.slice ​

ts
slice(begin?: int, end?: int): Int16Array

A COPY of elements [begin, end) in new storage. Negative indices count from the end; both clamp to the length.

Parameters

  • begin: Optional starting index; defaults to 0.
  • end: Optional ending index (exclusive); defaults to length.

Returns: A new Int16Array containing the copied elements.

Uint16Array class ​

ts
class Uint16Array extends TypedArray

Type only: the runtime hands you instances; you never construct it yourself.

Old-grammar declaration for the FixedArray<uint16> alias. new Uint16Array(x) is compiler-lowered to __fa_construct; no runtime global exists.

new Uint16Array() ​

ts
constructor()
constructor(length: int)
constructor(source: ByteArray)
constructor(source: TypedArray)
constructor(values: Array<int>)

Create an empty typed array.

Uint16Array.slice ​

ts
slice(begin?: int, end?: int): Uint16Array

A COPY of elements [begin, end) in new storage. Negative indices count from the end; both clamp to the length.

Parameters

  • begin: Optional starting index; defaults to 0.
  • end: Optional ending index (exclusive); defaults to length.

Returns: A new Uint16Array containing the copied elements.

Int32Array class ​

ts
class Int32Array extends TypedArray

Type only: the runtime hands you instances; you never construct it yourself.

Old-grammar declaration for the FixedArray<int32> alias. new Int32Array(x) is compiler-lowered to __fa_construct; no runtime global exists.

new Int32Array() ​

ts
constructor()
constructor(length: int)
constructor(source: ByteArray)
constructor(source: TypedArray)
constructor(values: Array<int>)

Create an empty typed array.

Int32Array.slice ​

ts
slice(begin?: int, end?: int): Int32Array

A COPY of elements [begin, end) in new storage. Negative indices count from the end; both clamp to the length.

Parameters

  • begin: Optional starting index; defaults to 0.
  • end: Optional ending index (exclusive); defaults to length.

Returns: A new Int32Array containing the copied elements.

Uint32Array class ​

ts
class Uint32Array extends TypedArray

Type only: the runtime hands you instances; you never construct it yourself.

Old-grammar declaration for the FixedArray<uint32> alias. new Uint32Array(x) is compiler-lowered to __fa_construct; no runtime global exists.

new Uint32Array() ​

ts
constructor()
constructor(length: int)
constructor(source: ByteArray)
constructor(source: TypedArray)
constructor(values: Array<int>)

Create an empty typed array.

Uint32Array.slice ​

ts
slice(begin?: int, end?: int): Uint32Array

A COPY of elements [begin, end) in new storage. Negative indices count from the end; both clamp to the length.

Parameters

  • begin: Optional starting index; defaults to 0.
  • end: Optional ending index (exclusive); defaults to length.

Returns: A new Uint32Array containing the copied elements.

Float32Array class ​

ts
class Float32Array extends TypedArray

Type only: the runtime hands you instances; you never construct it yourself.

Old-grammar declaration for the FixedArray<float32> alias. new Float32Array(x) is compiler-lowered to __fa_construct; no runtime global exists.

new Float32Array() ​

ts
constructor()
constructor(length: int)
constructor(source: ByteArray)
constructor(source: TypedArray)
constructor(values: Array<float>)

Create an empty typed array.

Float32Array.slice ​

ts
slice(begin?: int, end?: int): Float32Array

A COPY of elements [begin, end) in new storage. Negative indices count from the end; both clamp to the length.

Parameters

  • begin: Optional starting index; defaults to 0.
  • end: Optional ending index (exclusive); defaults to length.

Returns: A new Float32Array containing the copied elements.

Float64Array class ​

ts
class Float64Array extends TypedArray

Type only: the runtime hands you instances; you never construct it yourself.

Old-grammar declaration for the FixedArray<float64> alias. new Float64Array(x) is compiler-lowered to __fa_construct; no runtime global exists.

new Float64Array() ​

ts
constructor()
constructor(length: int)
constructor(source: ByteArray)
constructor(source: TypedArray)
constructor(values: Array<float>)

Create an empty typed array.

Float64Array.slice ​

ts
slice(begin?: int, end?: int): Float64Array

A COPY of elements [begin, end) in new storage. Negative indices count from the end; both clamp to the length.

Parameters

  • begin: Optional starting index; defaults to 0.
  • end: Optional ending index (exclusive); defaults to length.

Returns: A new Float64Array containing the copied elements.


Generated from lang/types/builtin_buffer.d.tks in the ThicketScript repository.