Buffer : Binary data buffer

更新时间:
2024-05-13
下载文档

Buffer : Binary data buffer

Buffer class is a global type with various constructors and accessors.

JSRE provides Buffer to manipulate binary data. Currently Buffer is an extension based on Uint8Array class. To facilitate application porting, the JSRE Buffer class is basically compatible with the Node.js Buffer class.

This module has been built-in in JSRE and does not require require() to import.

Example

// Creates a random-filled Buffer of length 10.
var buf1 = new Buffer(10);

// Creates a Buffer containing [0x1, 0x2, 0x3].
var buf2 = new Buffer([1, 2, 3]);

// Creates a Buffer containing UTF-8 string
var buf3 = new Buffer('Hello World!');

Support

The following shows Buffer module APIs available for each permissions.

 User ModePrivilege Mode
Buffer
Buffer.alloc
Buffer.allocSafe
Buffer.allocUnsafe
Buffer.byteLength
Buffer.concat
Buffer.from
Buffer.copyBytesFrom
Buffer.compare
Buffer.isBuffer
Buffer.isEncoding
Buffer.isUtf8
Buffer.isAscii
Buffer.isTypedArray
buf[]
buf.length
buf.copy
buf.compare
buf.equals
buf.fill
buf.fromString
buf.slice
buf.indexOf
buf.lastIndexOf
buf.includes
buf.toArray
buf.toString
buf.toJSON
buf.write
buf.writeInt8
buf.writeUInt8
buf.writeInt16LE
buf.writeInt16BE
buf.writeUInt16LE
buf.writeUInt16BE
buf.writeInt32LE
buf.writeInt32BE
buf.writeUInt32LE
buf.writeUInt32BE
buf.writeFloatLE
buf.writeFloatBE
buf.writeDoubleLE
buf.writeDoubleBE
buf.readInt8
buf.readUInt8
buf.readInt16LE
buf.readInt16BE
buf.readUInt16LE
buf.readUInt16BE
buf.readInt32LE
buf.readInt32BE
buf.readUInt32LE
buf.readUInt32BE
buf.readFloatLE
buf.readFloatBE
buf.readDoubleLE
buf.readDoubleBE

Buffer Class

new Buffer(size)

  • size {Integer} Size of the new buffer.
  • Returns: {Buffer} Buffer object.

Creates a new buffer of size bytes and initialize its data with random.

Example

var buf = new Buffer(32);

new Buffer(buffer[, offset[, length]])

  • buffer {Buffer} Source buffer.
  • offset {Integer} Offset of array. default: 0.
  • length {Integer} Number of elements copied. default: buffer.length.
  • Returns: {Buffer} Buffer object.

Creates a copy of an existing buffer. The buffer data is not shared between the two buffers.

Example

var buf1 = new Buffer(5);
var buf2 = new Buffer(buf1);

new Buffer(str[, encoding])

  • str {String} Source string.
  • encoding {String} Encoding format. default: 'utf-8'.
  • Returns: {Buffer} Buffer object.

Creates a new buffer which contains the string argument. 'utf-8', 'hex', 'base64', 'ascii', 'binary' is optional, 'utf-8' is default.

Example

var buffer = new Buffer(String.fromCharCode(65));

// prints: 1
console.log(buffer);

var buffer = new Buffer(String.fromCharCode(128));

// prints: 2
console.log(buffer);

var buffer = new Buffer(String.fromCharCode(2048));

// prints: 3
console.log(buffer);

var buffer = new Buffer('4142', 'hex');

// prints: AB
console.log(buffer.toString());

var buffer = new Buffer('Hello World');

// prints: Hello World
console.log(buffer.toString());

new Buffer(array[, offset[, length]])

  • array {Array} Given array.
  • offset {Integer} Offset of array. default: 0.
  • length {Integer} Number of elements copied. default: array.length.
  • Returns: {Buffer} Buffer object.

Creates a new Buffer from an array of numbers. The numbers are converted to integers first and their modulo 256 remainder is used for constructing the buffer.

Example

var buffer = new Buffer([65, 256 + 65, 65 - 256, 65.1]);

// prints: AAAA
console.log(buffer);

new Buffer(arrayBuffer[, offset[, length]])

  • arrayBuffer {Array} Given ArrayBuffer.
  • offset {Integer} Bytes offset of ArrayBuffer. default: 0.
  • length {Integer} Number of bytes copied. default: arrayBuffer.byteLength - offset.
  • Returns: {Buffer} Buffer object.

Creates a new Buffer use a range of memory specified by an ArrayBuffer. Multiple Buffers can use this method to share memory.

Example

var arrayBuffer = new ArrayBuffer(32);

// Shared memory.
// buf1[0...7] shared as buf2[0...7]
// buf2[4...7] shared as buf3[0...4]
var buf1 = new Buffer(arrayBuffer);
var buf2 = new Buffer(arrayBuffer, 0, 8);
var buf3 = new Buffer(arrayBuffer, 4, 4);

Buffer.byteLength(str[, encoding])

  • str {String} Source string.
  • encoding {String} String encoding. default: 'utf-8'.
  • Returns: {Integer} Byte length of source string.

Returns the byte length of a buffer representing the value of the string argument encoded with encoding.

Example

// prints: 1
console.log(Buffer.byteLength(String.fromCharCode(65)));

// prints: 2
console.log(Buffer.byteLength(String.fromCharCode(128)));

// prints: 3
console.log(Buffer.byteLength(String.fromCharCode(2048)));

// prints: 2
console.log(Buffer.byteLength('4142', 'hex'));

Buffer.concat(list[, length])

  • list {Array} An array of Buffer objects.
  • length {Integer} Max number of elements. default: array.length.
  • Returns: {Buffer} Concatenated buffer.

Returns the concatenation of the Buffer objects provided in the list array.

Example

var buffer = Buffer.concat([ new Buffer('He'),
                             new Buffer('llo'),
                             new Buffer(' Wo'),
                             new Buffer('rld') ]);
// prints: Hello World
console.log(buffer.toString());

Buffer.alloc(...)

Is same as new Buffer(...).

Buffer.allocSafe(...)

Is same as new Buffer(...), but filled with zero.

Buffer.allocUnsafe(...)

Is same as new Buffer(...).

Buffer.copyBytesFrom(view[, offset[, length]])

  • view {TypedArray} The source to copy.
  • offset {Integer} The starting offset within view. default: 0.
  • length {Integer} The number of elements from view to copy. default: view.length - offset.
  • Returns: {Buffer} The new buffer.

Copies the underlying memory of view into a new Buffer.

Example

var u16 = new Uint16Array([0, 0xffff]);
var buf = Buffer.copyBytesFrom(u16, 1, 1);
u16[1] = 0;
console.log(buf.length); // 2
console.log(buf[0]); // 255
console.log(buf[1]); // 255 

This function is available on EdgerOS 2.0.2 and above.

Buffer.from(...)

Is same as new Buffer(...).

Buffer.compare(buf1, buf2)

  • buf1 {Buffer}
  • buf2 {Buffer}
  • Returns: {Integer} Comparing results.

Compare two Buffer objects, It returns with 0 if the two buffers are the same. Otherwise it returns with -1 if the first different byte is lower for buf1, and 1 if the byte is higher. If the length of the two buffers are different, the comparison is performed until the lower length is reached. If all bytes are the same the function returns with -1 if buf1.length is less than buf2.length and 1 otherwise.

Example

var buf1 = new Buffer([2, 3, 4]);
var buf2 = new Buffer([2, 3, 4]);
var buf3 = new Buffer([1, 2, 3]);
var buf4 = new Buffer([1, 2, 3, 4]);

// prints: 0
console.log(Buffer.compare(buf1, buf2));

// prints: 1
console.log(Buffer.compare(buf2, buf3));

// prints: -1
console.log(Buffer.compare(buf3, buf4));

Buffer.isBuffer(obj)

  • obj {Object}
  • Returns: {Boolean}

Returns true if obj is an instance of Buffer. Returns false otherwise.

Example

// prints: true
console.log(Buffer.isBuffer(new Buffer(1)));

// prints: false
console.log(Buffer.isBuffer('str'));

Buffer.isEncoding(encoding)

  • encoding {String} Encoding name.
  • Returns: {Boolean} Whether it is a supported encoding type.

Get whether the specified encoding type is supported.

Example

// prints: true
console.log(Buffer.isEncoding('base64'));

Buffer.isUtf8(input)

  • input {Buffer | TypedArray | ArrayBuffer} The input to validate.
  • Returns: {Boolean} Is it a valid UTF-8 string.

This function returns true if input contains only valid UTF-8-encoded data, including the case in which input is empty.

This function is available on EdgerOS 1.10.1 and above.

Buffer.isAscii(input)

  • input {Buffer | TypedArray | ArrayBuffer} The input to validate.
  • Returns: {Boolean} Is it a valid ASCII string.

This function returns true if input contains only valid ASCII data, including the case in which input is empty.

This function is available on EdgerOS 2.0.2 and above.

Buffer.isTypedArray(input)

  • input {Any} Input argument.
  • Returns: {Boolean} Whether it is of TypedArray type.

Determine whether the input is of TypedArray type, including:

  • Int8Array
  • Uint8Array
  • Uint8ClampedArray
  • Int16Array
  • Uint16Array
  • Int32Array
  • Uint32Array
  • Float32Array
  • Float64Array
  • BigInt64Array
  • BigUint64Array

This function is available on EdgerOS 1.10.1 and above.

Buffer Object

buf[index]

The index operator [index] can be used to get and set the octet at position index in buf. The values refer to individual bytes, so the legal value range is between 0x00 and 0xFF (hex) or 0 and 255 (decimal).

This operator is inherited from Uint8Array, so its behavior on out-of-bounds access is the same as UInt8Array - that is, getting returns undefined and setting does nothing.

Example

var str = 'JSRE';
var buf = new Buffer(str.length);

for (var i = 0; i < str.length; i++) {
  buf[i] = str.charCodeAt(i);
}

console.log(buf.toString());

buf.length

  • {Integer}

Returns the capacity of the buffer in bytes.

Example

var buffer = new Buffer([0xc8, 0x80]);

// prints: 2
console.log(buffer.length);

var str = buffer.toString();

// prints: 1
console.log(str.length);

buf.byteLength

Is same as buf.length.

buf.copy(target[, targetStart[, sourceStart[, sourceEnd]]])

  • target {Buffer} The right-hand side of the comparison.
  • targetStart {Integer} target buffer start offset. default: 0.
  • sourceStart {Integer} source buffer start offset. default: 0.
  • sourceEnd {Integer} source buffer end offset (not include). default: source.length.

Copy a sequence of bytes from buf buffer to targetBuffer buffer. The source byte range is specified by sourceStart and sourceEnd and the destination byte offset is specified by targetStart. Only the targetBuffer is modified.

Example

var buffer1 = new Buffer('Hello XY world!');
var buffer2 = new Buffer('<JS>');

buffer2.copy(buffer1, 6, 1, 3);

// prints: Hello JS world!
console.log(buffer1);

buf.compare(target[, targetStart[, targetEnd[, sourceStart[, sourceEnd]]]])

  • target {Buffer} The right-hand side of the comparison.
  • targetStart {Integer} target buffer start offset. default: 0.
  • targetEnd {Integer} target buffer end offset (not include). default: target.length.
  • sourceStart {Integer} source buffer start offset. default: 0.
  • sourceEnd {Integer} source buffer end offset (not include). default: source.length.
  • Returns: {Integer} Comparing results.

This function performs a lexicographic comparison between two buffers. It returns with 0 if the two buffers are the same. Otherwise it returns with -1 if the first different byte is lower for buf, and 1 if the byte is higher. If the length of the two buffers are different, the comparison is performed until the lower length is reached. If all bytes are the same the function returns with -1 if buf.length is less than target.length and 1 otherwise.

Example

var buffer1 = new Buffer('AB');
var buffer2 = new Buffer('A');
var buffer3 = new Buffer('B');

// prints: 0
console.log(buffer1.compare(buffer1));

// prints: 1
console.log(buffer1.compare(buffer2));

// prints: -1
console.log(buffer1.compare(buffer3));

buf.equals(buffer)

  • buffer {Buffer} Buffer object.
  • Returns: {Boolean} Whether the content is consistent.

Compares whether two Buffers are identical, if the same returns true, otherwise returns false. The effect is the same as:

return buf.compare(otherBuffer) == 0;

Example

var buffer1 = new Buffer('A');
var buffer2 = new Buffer('A');
var buffer3 = new Buffer('B');

// prints: true
console.log(buffer1.equals(buffer2));

// prints: false
console.log(buffer1.equals(buffer3));

buf.fill(value[, start[, end]])

  • value {Integer} All bytes are set to this value.
  • start {Integer} Start position. default: 0.
  • end {Integer} End position (not includes). default: buf.length.
  • Returns: {Buffer} The original buffer.

Set all bytes of the buffer to value. The value is converted to integer first and its modulo 256 remainder is used for updating the buffer. Returns with buf.

Example

var buffer = new Buffer('Hello');

buffer.fill(65);

// prints: AAAAA
console.log(buffer);

buffer.fill(66 - 256);

// prints: BBBBB
console.log(buffer);

buf.fromString(str, [offset[, encoding]])

  • str {String} String need to be copied.
  • offset {Integer} Buffer offset. default: 0.
  • encoding {String} String encoding. default: 'utf-8'.
  • Returns: {Integer} Number of bytes written.

Copy a string to the specified location of the target buffer. If the buffer space insufficient, an exception will be thrown.

Example

var str = 'Test';
var buf = new Buffer(32);

buf.fromString(str);
buf.fromString(str, 6);

str = 'aa55';
buf.fromString(str, 10, 'hex');

buf.slice([start[, end]])

  • start {Integer} Start position. default: 0.
  • end {Integer} End position (not includes). default: buf.length.
  • Returns: {Buffer} A newly created buffer.

This function returns with a newly created buffer which contains the bytes of the buf buffer between start and end. The slice() method returns a shallow copy (memory reference) of a portion of a TypedArray into a new TypedArray object.

Example

var buffer = new Buffer('This is JavaScript!!!');

// prints: JavaScript
console.log(buffer.slice(8, 18));

buf.indexOf(value[, byteOffset][, encoding])

  • value {String} | {Buffer} | {Uint8Array} | {Number} What to search for.
  • byteOffset {Integer} Where to begin searching in buf. If negative, then offset is calculated from the end of buf. default: 0.
  • encoding {String} If value is a string, this is the encoding used to determine the binary representation of the string that will be searched for in buf. default: 'utf8'.
  • Returns: {Integer} The index of the first occurrence of value in buf, or -1 if buf does not contain value.

If value is:

  • a string, value is interpreted according to the character encoding in encoding.
  • a Buffer or Uint8Array, value will be used in its entirety.
  • a number, value will be interpreted as an unsigned 8-bit integer value between 0 and 255.

If value is not a string, number, or Buffer, this method will throw a TypeError. If value is a number, it will be coerced to a valid byte value, an integer between 0 and 255.

If value is an empty string or empty Buffer and byteOffset is less than buf.length, byteOffset will be returned. If value is empty and byteOffset is at least buf.length, buf.length will be returned.

Example

const buf = Buffer.from('this is a buffer');

console.log(buf.indexOf('this'));
// Prints: 0
console.log(buf.indexOf('is'));
// Prints: 2
console.log(buf.indexOf(Buffer.from('a buffer')));
// Prints: 8
console.log(buf.indexOf(97));
// Prints: 8 (97 is the decimal ASCII value for 'a')
console.log(buf.indexOf(Buffer.from('a buffer example')));
// Prints: -1
console.log(buf.indexOf(Buffer.from('a buffer example').slice(0, 8)));
// Prints: 8

buf.lastIndexOf(value[, byteOffset][, encoding])

  • value {String} | {Buffer} | {Uint8Array} | {Number} What to search for.
  • byteOffset {Integer} Where to begin searching in buf. If negative, then offset is calculated from the end of buf. default: buf.length.
  • encoding {String} If value is a string, this is the encoding used to determine the binary representation of the string that will be searched for in buf. default: 'utf8'.
  • Returns: {Integer} The index of the last occurrence of value in buf, or -1 if buf does not contain value.

Identical to buf.indexOf(), except the last occurrence of value is found rather than the first occurrence.

If value is not a string, number, or Buffer, this method will throw a TypeError. If value is a number, it will be coerced to a valid byte value, an integer between 0 and 255.

If value is an empty string or empty Buffer, byteOffset will be returned.

Example

const buf = Buffer.from('this buffer is a buffer');

console.log(buf.lastIndexOf('this'));
// Prints: 0
console.log(buf.lastIndexOf('buffer'));
// Prints: 17
console.log(buf.lastIndexOf(Buffer.from('buffer')));
// Prints: 17
console.log(buf.lastIndexOf(97));
// Prints: 15 (97 is the decimal ASCII value for 'a')
console.log(buf.lastIndexOf(Buffer.from('yolo')));
// Prints: -1
console.log(buf.lastIndexOf('buffer', 5));
// Prints: 5
console.log(buf.lastIndexOf('buffer', 4));
// Prints: -1

buf.includes(value[, byteOffset][, encoding])

  • value {String} | {Buffer} | {Uint8Array} | {Number} What to search for.
  • byteOffset {Integer} Where to begin searching in buf. If negative, then offset is calculated from the end of buf. default: 0.
  • encoding {String} If value is a string, this is the encoding used to determine the binary representation of the string that will be searched for in buf. default: 'utf8'.
  • Returns: {Boolean} true if value was found in buf, false otherwise..

Equivalent to buf.indexOf(...) !== -1.

buf.toArray([start[, end]])

  • start {Integer} Start position. default: 0.
  • end {Integer} End position (not includes). default: buffer.length.
  • Returns: {Array}

Returns a array created from the bytes stored in the buffer. By passing start and end the conversion can be limited to a subset of the buf buffer.

Example

var buffer = new Buffer('DEFG');

// prints: ['E', 'F']
console.log(buffer.toArray(1, 3));

buf.toString([encoding[, start[, end]]])

  • encoding {String} Encoding type. default: 'utf8'.
  • start {Integer} Start position. default: 0.
  • end {Integer} End position (not includes). default: buffer.length.
  • Returns: {String}

Decode buf into a string according to the character encoding specified by encoding. Pass in start and end to decode only a subset of buf.

Example

var buffer = new Buffer('DEFG');

// prints: 44454647
console.log(buffer.toString('hex'));

// prints: REVGRw==
console.log(buffer.toString('base64'));

buf.toString([start[, end]])

  • start {Integer} Start position. default: 0.
  • end {Integer} End position (not includes). default: buffer.length.
  • Returns: {String}

Returns a string created from the bytes stored in the buffer. By passing start and end the conversion can be limited to a subset of the buf buffer. If a single hex string is passed to the function, the whole buffer is converted to hexadecimal data.

Example

var buffer = new Buffer('DEFG');

// prints: EF
console.log(buffer.toString(1, 3));

buf.toJSON()

  • Returns: {String} the string format of buffer.

JSON.stringify() will call this function to generate a buffer string.

Example

var buf = Buffer.from([0x1, 0x2, 0x3, 0x4, 0x5]);
var json = JSON.stringify(buf);

// Print: {"type":"Buffer","data":[1,2,3,4,5]}
console.log(json);

var copy = JSON.parse(json, (key, value) => {
  return value && value.type === 'Buffer' ?
    Buffer.from(value.data) : value;
});

buf.write(string[, offset[, length[, encoding]]])

  • string {String} Data to be written into buffer.
  • offset {Integer} Start position of writing. default: 0.
  • length {Integer} How many bytes to write. default: buffer.length - offset.
  • encoding {String} Encodeing. default: 'utf-8'.
  • Returns: {Integer} Total number of bytes written.

Writes string into the buf buffer. The start position of the writing can be specified by offset and the maximum number of updated bytes can be limited by length. Returns total number of bytes written to the buffer.

Example

var buffer = new Buffer('......');
buffer.write('AB');
buffer.write('XY', 3);

// prints: AB.XY.
console.log(buffer);

var buffer = new Buffer('......');
buffer.write('ABCDEF', 1, 3);

// prints: .ABC..
console.log(buffer);

Buffer Read / Write

buf.writeInt8(value, offset)

  • value {Integer} Number to be written into the buffer.
  • offset {Integer} Start position of the writing.
  • Returns: {Number} Offset plus the number of bytes written.

Writes value into the buffer starting from offset position. The value must be a valid 8-bit signed integer.

buf.writeUInt8(value, offset)

  • value {Integer} Number to be written into the buffer.
  • offset {Integer} Start position of the writing.
  • Returns: {Number} Offset plus the number of bytes written.

Writes value into the buffer starting from offset position. The value must be a valid 8-bit unsigned integer.

buf.writeInt16LE(value, offset)

  • value {Integer} Number to be written into the buffer.
  • offset {Integer} Start position of the writing.
  • Returns: {Integer} Offset plus the number of bytes written.

Writes value into the buffer starting from offset position with little endian format. The value must be a valid 16-bit signed integer.

buf.writeInt16BE(value, offset)

  • value {Integer} Number to be written into the buffer.
  • offset {Integer} Start position of the writing.
  • Returns: {Integer} Offset plus the number of bytes written.

Writes value into the buffer starting from offset position with big endian format. The value must be a valid 16-bit signed integer.

buf.writeUInt16LE(value, offset)

  • value {Integer} Number to be written into the buffer.
  • offset {Integer} Start position of the writing.
  • Returns: {Integer} Offset plus the number of bytes written.

Writes value into the buffer starting from offset position with little endian format. The value must be a valid 16-bit unsigned integer.

buf.writeUInt16BE(value, offset)

  • value {Integer} Number to be written into the buffer.
  • offset {Integer} Start position of the writing.
  • Returns: {Integer} Offset plus the number of bytes written.

Writes value into the buffer starting from offset position with big endian format. The value must be a valid 16-bit unsigned integer.

buf.writeInt32LE(value, offset)

  • value {Integer} Number to be written into the buffer.
  • offset {Integer} Start position of the writing.
  • Returns: {Integer} Offset plus the number of bytes written.

Writes value into the buffer starting from offset position with little endian format. The value must be a valid 32-bit signed integer.

buf.writeInt32BE(value, offset)

  • value {Integer} Number to be written into the buffer.
  • offset {Integer} Start position of the writing.
  • Returns: {Integer} Offset plus the number of bytes written.

Writes value into the buffer starting from offset position with big endian format. The value must be a valid 32-bit signed integer.

buf.writeUInt32LE(value, offset)

  • value {Integer} Number to be written into the buffer.
  • offset {Integer} Start position of the writing.
  • Returns: {Integer} Offset plus the number of bytes written.

Writes value into the buffer starting from offset position with little endian format. The value must be a valid 32-bit unsigned integer.

buf.writeUInt32BE(value, offset)

  • value {Integer} Number to be written into the buffer.
  • offset {Integer} Start position of the writing.
  • Returns: {Integer} Offset plus the number of bytes written.

Writes value into the buffer starting from offset position with big endian format. The value must be a valid 32-bit unsigned integer.

buf.writeFloatLE(value, offset)

  • value {Number} Number to be written into the buffer.
  • offset {Integer} Start position of the writing.
  • Returns: {Integer} Offset plus the number of bytes written.

Writes value into the buffer starting from offset position with little endian format. The value save as a 32-bit float.

buf.writeFloatBE(value, offset)

  • value {Number} Number to be written into the buffer.
  • offset {Integer} Start position of the writing.
  • Returns: {Integer} Offset plus the number of bytes written.

Writes value into the buffer starting from offset position with big endian format. The value save as a 32-bit float.

buf.writeDoubleLE(value, offset)

  • value {Number} Number to be written into the buffer.
  • offset {Integer} Start position of the writing.
  • Returns: {Integer} Offset plus the number of bytes written.

Writes value into the buffer starting from offset position with little endian format. The value save as a 64-bit float.

buf.writeDoubleBE(value, offset)

  • value {Number} Number to be written into the buffer.
  • offset {Integer} Start position of the writing.
  • Returns: {Integer} Offset plus the number of bytes written.

Writes value into the buffer starting from offset position with big endian format. The value save as a 64-bit float.

buf.readInt8(offset)

  • offset {Number} Start position of buffer for reading.
  • Returns: {Number}

Reads a signed 8-bit integer from buf buffer starting from offset position.

buf.readUInt8(offset)

  • offset {Number} Start position of buffer for reading.
  • Returns: {Number}

Reads a signed 8-bit unsigned integer from buf buffer starting from offset position.

buf.readInt16LE(offset)

  • offset {Number} Start position of buffer for reading.
  • Returns: {Number}

Reads an unsigned 16-bit signed integer from buf buffer starting from offset position with little endian format.

buf.readInt16BE(offset)

  • offset {Number} Start position of buffer for reading.
  • Returns: {Number}

Reads an unsigned 16-bit signed integer from buf buffer starting from offset position with big endian format.

buf.readUInt16LE(offset)

  • offset {Number} Start position of buffer for reading.
  • Returns: {Number}

Reads an unsigned 16-bit unsigned integer from buf buffer starting from offset position with little endian format.

buf.readUInt16BE(offset)

  • offset {Number} Start position of buffer for reading.
  • Returns: {Number}

Reads an unsigned 16-bit unsigned integer from buf buffer starting from offset position with big endian format.

buf.readInt32LE(offset)

  • offset {Number} Start position of buffer for reading.
  • Returns: {Number}

Reads an unsigned 32-bit signed integer from buf buffer starting from offset position with little endian format.

buf.readInt32BE(offset)

  • offset {Number} Start position of buffer for reading.
  • Returns: {Number}

Reads an unsigned 32-bit signed integer from buf buffer starting from offset position with big endian format.

buf.readUInt32LE(offset)

  • offset {Number} Start position of buffer for reading.
  • Returns: {Number}

Reads an unsigned 32-bit unsigned integer from buf buffer starting from offset position with little endian format.

buf.readUInt32BE(offset)

  • offset {Number} Start position of buffer for reading.
  • Returns: {Number}

Reads an unsigned 32-bit unsigned integer from buf buffer starting from offset position with big endian format.

buf.readFloatLE(offset)

  • offset {Number} Start position of buffer for reading.
  • Returns: {Number}

Reads an unsigned 32-bit float from buf buffer starting from offset position with little endian format.

buf.readFloatBE(offset)

  • offset {Number} Start position of buffer for reading.
  • Returns: {Number}

Reads an unsigned 32-bit float from buf buffer starting from offset position with big endian format.

buf.readDoubleLE(offset)

  • offset {Number} Start position of buffer for reading.
  • Returns: {Number}

Reads an unsigned 64-bit float from buf buffer starting from offset position with little endian format.

buf.readDoubleBE(offset)

  • offset {Number} Start position of buffer for reading.
  • Returns: {Number}

Reads an unsigned 64-bit float from buf buffer starting from offset position with big endian format.

Buffer Constant

Buffer.MAX_LENGTH

  • {Integer}

Maximum number of bytes for a buffer object. Due to the tight resources of the embedded environment, the default is 64MB, and the limit can be raised through the quota file. If the limit is exceeded, you can use loops and other methods in the program to avoid memory waste.

Buffer.MAX_STRING_LENGTH

  • {Integer}

Current value is half of Buffer.MAX_LENGTH.

文档内容是否对您有所帮助?
有帮助
没帮助