Quark #

Modules #

Quark v1.3.0 Documentation


quark/buffer #

Encoding #

All encoding algorithms of string

  • @type Encoding = "binary"|"ascii"|"base64"|"hex"|"utf-8"|"utf8"|"utf-16"|"utf16"|"ucs4"

Class: Buffer #

Buffer #

This class represents a binary buffer.

buffer.copy(target,targetStart?,sourceStart?,sourceEnd?) #

Copy current buffer data to the target buffer.

  • @param target: ArrayBufferView The target buffer to copy data into.
  • @param targetStart?: Uint The starting index in the target buffer, Default is 0.
  • @param sourceStart?: Uint The starting index in the source buffer, Default is 0.
  • @param sourceEnd?: Uint The ending index in the source buffer, Default is the length of the source buffer.
  • @return Uint The number of bytes copied.
  • @throws {TypeError} If the target is not a valid typed array.
  • @throws {RangeError} If the target start index is out of range.
  • @throws {RangeError} If the source start or end index is out of range.

For example:

const source = new Buffer([1, 2, 3, 4, 5]);
const target = new Buffer(3);
const bytesCopied = source.copy(target, 0, 1, 4);
console.log(bytesCopied); // Outputs: 3
console.log(target); // Outputs: Uint8Array(3) [2, 3, 4]

buffer.slice(start?,end?) #

slice the buffer data into a new buffer, shared memory with the original buffer.

buffer.subarray(begin?,end?) #

subarray the buffer data into a new buffer, shared memory with the original buffer.

buffer.toJSON() #

JSON representation of the buffer.

  • @return

buffer.write(source,offset?) #

writing data to self buffer.

  • @param source: ArrayBufferView
  • @param offset?: Uint
  • @return Uint

buffer.compare(target,start?,end?,thisStart?,thisEnd?) #

Compare this buffer with another buffer or Uint8Array.

  • @param target: Uint8Array The target buffer or Uint8Array to compare with.
  • @param start?: Uint The starting index in the target buffer, Default is 0.
  • @param end?: Uint The ending index in the target buffer, Default is the length of the target buffer.
  • @param thisStart?: Uint The starting index in this buffer, Default is 0.
  • @param thisEnd?: Uint The ending index in this buffer, Default is the length of this buffer.
  • @return Int Returns 0 if the buffers are equal, a negative number if this buffer is less than the target, or a positive number if this buffer is greater than the target.
  • @throws {TypeError} If the target is not a valid typed array.
  • @throws {RangeError} If the start or end indices are out of range.

For example:

const buf1 = new Buffer([1, 2, 3]);
const buf2 = new Buffer([1, 2, 3, 4]);
console.log(buf1.compare(buf2)); // Outputs: -1 (buf1 is less than buf2)
console.log(buf2.compare(buf1)); // Outputs: 1 (buf2 is greater than buf1)
console.log(buf1.compare(buf1)); // Outputs: 0 (buf1 is equal to itself)

buffer.equals(b) #

Check if this buffer is equal to another buffer or Uint8Array.

  • @param b: Uint8Array The buffer or Uint8Array to compare with.
  • @return boolean Returns true if the buffers are equal, false otherwise.
  • @throws {TypeError} If the argument is not a valid Uint8Array.

For example:

const buf1 = new Buffer([1, 2, 3]);
const buf2 = new Buffer([1, 2, 3]);
const buf3 = new Buffer([4, 5, 6]);
console.log(buf1.equals(buf2)); // Outputs: true (buf1 is equal to buf2)
console.log(buf1.equals(buf3)); // Outputs: false (buf1 is not equal to buf3)

buffer.toString(encoding?,start?,end?) #

Convert the buffer to a string using the specified encoding.

  • @param encoding?: Encoding The encoding to use, Default is 'utf8'.
  • @param start?: Uint The starting index, Default is 0.
  • @param end?: Uint The ending index, Default is the length of the buffer.
  • @return string The string representation of the buffer.

For example:

const buf = new Buffer([72, 101, 108, 108,111]);
console.log(buf.toString('utf8')); // Outputs: Hello

buffer.readInt8(offset?) #

Read int8 from the buffer at the specified offset.

buffer.readUInt8(offset?) #

Read uint8 from the buffer at the specified offset.

Zero #

Zero buffer

  • @const Zero:

alloc(size,initFill?) #

Alloc new buffer

isTypedArray(arr) #

Tests whether it is a typed array

isUint8Array(arr) #

Tests whether it is a Uint8Array

isBuffer(arr) #

Tests whether it is a buffer data type

compare(a,b) #

Compare sizes of two buffers

toString(src,encoding?,start?,end?) #

Decode data into a string using the specified algorithm

fromString(str,targetEn?) #

Specify the algorithm to encode the string and return the encoded data

  • @param str: string The string to encode.
  • @param targetEn?: Encoding Optional, the encoding algorithm to use, Default is 'utf8'.
  • @return Uint8Array A Uint8Array containing the encoded data.

For example:

const encoded = fromString("Hello, World!", "utf8");
console.log(encoded); // Outputs: Uint8Array(13) [72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33]

from(from,encoding?) #

From string create a buffer

  • @param from: string a string to create a buffer from
  • @param encoding?: Encoding Optional, this specifies the encoding to use.
  • @return Buffer

from(from) #

From ArrayBufferView create a buffer

  • @param from: ArrayBufferView a TypedArray or DataView to create a buffer from
  • @return Buffer

from(from,byteOffset?,length?) #

From ArrayBufferLike create a buffer

  • @param from: ArrayBufferLike an ArrayBuffer or SharedArrayBuffer to create a buffer from
  • @param byteOffset?: Uint Optional, the offset in bytes to start from, Default is 0.
  • @param length?: Uint Optional, the length in bytes of the buffer, Default is the remaining length of the ArrayBuffer.

For example:

const buffer = from(new ArrayBuffer(10), 0, 10);
console.log(buffer.byteLength); // Outputs: 10

from(from,mapFn?) #

From Iterable or ArrayLike create a buffer

  • @param from: Iterable<Int>|ArrayLike<Int> an iterable or array-like object to create a buffer from
  • @param mapFn?: MapFn Optional, a mapping function to apply to each element.

For example:

const buffer = from([1, 2, 3, 4], (v, k) => v * 2);
console.log(buffer); // Outputs: Uint8Array(4) [2, 4, 6, 8]

concat(list,length?) #

Concat multiple buffers into a new buffer

  • @param list: ArrayLike<Int>[] An array of byte arrays to concatenate.
  • @param length?: Uint Optional, the total length of the new buffer. If not provided,
    it will be calculated based on the lengths of the byte arrays in the list.
  • @return Buffer A new Buffer containing the concatenated data.

For example:

const buffer1 = new Buffer([1, 2, 3]);
const buffer2 = new Buffer([4, 5, 6]);
const concatenated = concat([buffer1, buffer2]);
console.log(concatenated); // Outputs: Uint8Array(6) [1, 2, 3, 4, 5, 6]
  • @throws {TypeError} If the provided list is not an array-like object or if
    any of the elements in the list are not of type Uint8Array or Buffer.
  • @throws {RangeError} If the specified length is negative.
  • @throws {Error} If the concatenation fails for any reason.