Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Node.js Buffer


May 10, 2021 Node.js


Table of contents


Buffer

稳定性: 3 - 稳定

The pure Javascript language is Unicode-friendly and handles Unicode-encoded string data well, but it is difficult to process binary data. O perating byte streams is often required when working with TCP streams and file systems. Node provides mechanisms for operating, creating, and consuming byte streams.

Buffer .js Node, which can handle binary and non-Unicode encoded data.

The original data is stored in the Buffalo class instantiation. B uffer is similar to an array of integers, allocating memory to the original storage space in the V8 heap. Once a Buffer instance is created, the size cannot be changed.

Buffer class is a global object, so access to it does not require('buffer')

A clear encoding method is required when converting between buffers and Javascript string objects. Here are the different encodings of the string:

  • 'ascii' - 7-bit ASCII data. This encoding is very fast, and it removes the highest bit of content.

  • 'utf8' - Multi-byte encoded Unicode characters. Most Web pages and documents use this type of encoding.

  • 'utf16le' - 2 or 4 bytes, Little Endian (LE) encodes Unicode characters. Coding range (U-10000 to U-10FFFF).

  • 'ucs2' - 'utf16le'

  • 'base64' - Base64 character encoding.

  • 'binary' - The original binary information is encoded using only the first 8 bits of each character. I n cases where Buffer is required, you should try to avoid using this outdated encoding method. This encoding method will be deprecated in a future release.
  • 'hex' - each byte is binary encoded.

To Buffer array in Buffer, you need to be aware of the following rules:

  1. Buffer is a copy of memory, not a memory share.

  2. Buffer occupancy memory is interpreted as an array, not an array of bytes. For new Uint32Array(new Buffer([1,2,3,4])) Uint32Array which have members [1,2,3,4] than "0x1020304" or [0x4030201] [0x1020304]

Note: Node .js v0.8 simply references the buffer in array.buffer not cloning it.

Introducing an efficient approach, ArrayBuffer#slice() copied a slice, and Buffer#slice() created a new one.

Class: Buffer

The Buffer class is a global variable type that handles binary data directly. It can be built in a variety of ways.

new Buffer(size)

  • size Number type

Allocate a new size size unit of 8-bit bytes of buffer.

Note: size be less than kMaxLength or a RangeError

new Buffer(array)

  • array Array

Use an 8-bit array array array to assign a new buffer.

new Buffer(buffer)

  • buffer {Buffer}

Copy data from buffer to the Buffer instance.

new Buffer(str[, encoding])

  • str String type - A string that needs to be encoded.
  • encoding String type - encoding, optional.

Assign a new buffer that contains the incoming str string. encoding encoding defaults to 'utf8'

Class method: Buffer.isEncoding (encoding)

  • encoding used to test a given encoding string

If the parameter encoding is valid, return true, otherwise return false.

Class method: Buffer.isBuffer (obj)

  • obj object
  • Back: Boolean

obj buffer Buffer true, otherwise false is returned.

Class Method: Buffer.byteLength (string, encoding)

  • string String type
  • encoding String type, optional, default: 'utf8'
  • Back: Number type

The true byte length of the string is returned. E ncoding encoding defaults utf8 This is String.prototype.length because that method returns the number of characters in the string.

For example:

str = '\u00bd + \u00bc = \u00be';

console.log(str + ": " + str.length + " characters, " +
  Buffer.byteLength(str, 'utf8') + " bytes");

// ½ + ¼ = ¾: 9 characters, 12 bytes

Class method: Buffer.concat (list, totalLength)

  • list array that lists the arrays that are used to connect
  • totalLength size of all objects in the totalLength (Number Type) array

Returns a buffer object, which stitches together all the buffer objects in the parameter buffer array.

If the incoming array has no content, or totalLength is 0, a buffer of 0 length is returned.

If the array length is 1, the first member of the array is returned.

If the array length is greater than 0, a new Buffer instance is created.

If the totalLength parameter is not provided, it is calculated according to the buffer array, which adds an additional loop calculation, so it is faster to provide an accurate totallength parameter.

Class method: Buffer.compare (buf1, buf2)

  • buf1 {Buffer}
  • buf2 {Buffer}

As buf1.compare(buf2) used to sort arrays:

var arr = [Buffer('1234'), Buffer('0123')];
arr.sort(Buffer.compare);

buf.length

  • Number type

Return the number of bytes for this buffer. N ote that this is not necessarily the size of the content inside the buffer. length is the amount of memory allocated by the buffer object, and it does not change as the contents of the buffer object change.

buf = new Buffer(1234);

console.log(buf.length);
buf.write("some string", 0, "ascii");
console.log(buf.length);

// 1234
// 1234

length be changed, and changing length will result in unexpected results. If you want to change the length of the buffer, buf.slice to create a new buffer.

buf = new Buffer(10);
buf.write("abcdefghj", 0, "ascii");
console.log(buf.length); // 10
buf = buf.slice(0,5);
console.log(buf.length); // 5

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

  • string String type - written in buffer
  • offset Number type, optional parameters, default: 0
  • length Number type, optional parameters, default: buffer.length - offset
  • encoding String type, optional parameters, default: 'utf8'

The parameter string data is written to the buffer based on the parameter offset offset and the specified encoding encoding method. T he offset offset default is 0, and encoding encoding is utf8 T he length length is the bytes size of the string to be written. R eturns the number type, which indicates how many 8-bit byte streams were written. I f the buffer doesn't have enough space to put the entire string, it will write only a few strings. L ength defaults to buffer.length - offset. This method does not write part of the character.

buf = new Buffer(256);
len = buf.write('\u00bd + \u00bc = \u00be', 0);
console.log(len + " bytes: " + buf.toString('utf8', 0, len));

buf.writeUIntLE(value, offset, byteLength[, noAssert])

buf.writeUIntBE(value, offset, byteLength[, noAssert])

buf.writeIntLE(value, offset, byteLength[, noAssert])

buf.writeIntBE(value, offset, byteLength[, noAssert])

  • value type) is ready to write to the number of buffer bytes
  • offset (Number 0 <= offset <= buf.length
  • byteLength (Number 0 < byteLength <= 6
  • noAssert (Boolean) Default: false
  • Back: snr Type

Write value to buffer, which is determined by offset byteLength supports 48-bit calculations, such as: offset

var b = new Buffer(6);
b.writeUIntBE(0x1234567890ab, 0, 6);
// <Buffer 12 34 56 78 90 ab>

noAssert value is true the validity of value offset longer validated. The default is false

buf.readUIntLE(offset, byteLength[, noAssert])

buf.readUIntBE(offset, byteLength[, noAssert])

buf.readIntLE(offset, byteLength[, noAssert])

buf.readIntBE(offset, byteLength[, noAssert])

  • offset (Number 0 <= offset <= buf.length
  • byteLength (Number 0 < byteLength <= 6
  • noAssert (Boolean) Default: false
  • Back: snr Type

Digital reading below 48 bits is supported. For example:

var b = new Buffer(6);
b.writeUint16LE(0x90ab, 0);
b.writeUInt32LE(0x12345678, 2);
b.readUIntLE(0, 6).toString(16);  // 指定为 6 bytes (48 bits)
// 输出: '1234567890ab'

noAssert value is true, offset verifies that it exceeds the length of the buffer, which defaults false

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

  • encoding String type, optional parameters, default: 'utf8'
  • start Number type, optional parameters, default: 0
  • end Number type, optional parameters, default: buffer.length

Returns a decoded string type based on the encoding parameter (the default is 'utf8'). The range of values is also taken based on the incoming parameters start (0 by default) and end (buffer.length by default).

buf = new Buffer(26);
for (var i = 0 ; i < 26 ; i++) {
  buf[i] = i + 97; // 97 is ASCII a
}
buf.toString('ascii'); // 输出: abcdefghijklmnopqrstuvwxyz
buf.toString('ascii',0,5); // 输出: abcde
buf.toString('utf8',0,5); // 输出: abcde
buf.toString(undefined,0,5); // encoding defaults to 'utf8', 输出 abcde

Check out buffer.write() above.

buf.toJSON()

Returns a Buffer instance represented by JSON. JSON.stringify call string serialization of this Buffer instance by default.

For example:

var buf = new Buffer('test');
var json = JSON.stringify(buf);

console.log(json);
// '{"type":"Buffer","data":[116,101,115,116]}'

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

console.log(copy);
// <Buffer 74 65 73 74>

buf[index]

Gets or sets the 8-bit byte that specifies the index location. This value refers to a single byte, so it must be taken in a legal range, 16-0x00 to 0xFF, or 0 to 255.

For example: copy an ASCII encoded string to a buffer, one byte at a time to copy:

str = "node.js";
buf = new Buffer(str.length);

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

console.log(buf);

// node.js

buf.equals(otherBuffer)

  • otherBuffer {Buffer}

If this and otherBuffer the same content, return true.

buf.compare(otherBuffer)

  • otherBuffer {Buffer}

Returns a number that this otherBuffer or the same.

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

  • targetBuffer Buffer object - Buffer to copy into
  • targetStart Number type, optional parameters, default: 0
  • sourceStart Number type, optional parameters, default: 0
  • sourceEnd Number type, optional parameters, default: buffer.length

Buffer copies, sources and targets can be the same. targetStart start offset and sourceStart start offset default is 0. sourceEnd source end offset defaults to the source's length buffer.length

For example, create 2 Buffer and copy the 16 to 19 bits of buf1 content after the 8th bit of buff2.

buf1 = new Buffer(26);
buf2 = new Buffer(26);

for (var i = 0 ; i < 26 ; i++) {
  buf1[i] = i + 97; // 97 is ASCII a
  buf2[i] = 33; // ASCII !
}

buf1.copy(buf2, 8, 16, 20);
console.log(buf2.toString('ascii', 0, 25));

// !!!!!!!!qrst!!!!!!!!!!!!!

For example: In the same buffer, copy from one region to another:

buf = new Buffer(26);

for (var i = 0 ; i < 26 ; i++) {
  buf[i] = i + 97; // 97 is ASCII a
}

buf.copy(buf, 0, 4, 10);
console.log(buf.toString());

// efghijghijklmnopqrstuvwxyz

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

  • start Number type, optional parameters, default: 0
  • end Number type, optional parameters, default: buffer.length

Returns a new buffer, which will reference the same memory address as the old buffer, offset and crop the index based end start 0 and buffer.length The negative index is calculated from the end of the buffer.

Modifying this new buffer instance slice will also change the original buffer!

For example: Create a Buffer of the ASCII letter, slice the slice, and then modify a byte on the source Buffer.

var buf1 = new Buffer(26);

for (var i = 0 ; i < 26 ; i++) {
  buf1[i] = i + 97; // 97 is ASCII a
}

var buf2 = buf1.slice(0, 3);
console.log(buf2.toString('ascii', 0, buf2.length));
buf1[0] = 33;
console.log(buf2.toString('ascii', 0, buf2.length));

// abc
// !bc

buf.readUInt8(offset[, noAssert])

  • offset Number type
  • noAssert Boolean, optional parameter, default: false
  • Return: Number type

From this buffer object, read a signed 8-bit integer shaping based on the specified offset.

If the noAssert true, the offset will not be validated. I f so offset may exceed the end of the buffer. The default is false

For example:

var buf = new Buffer(4);

buf[0] = 0x3;
buf[1] = 0x4;
buf[2] = 0x23;
buf[3] = 0x42;

for (ii = 0; ii < buf.length; ii++) {
  console.log(buf.readUInt8(ii));
}

// 0x3
// 0x4
// 0x23
// 0x42

buf.readUInt16LE(offset[, noAssert])

buf.readUInt16BE(offset[, noAssert])

  • offset Number type
  • noAssert Boolean, optional parameter, default: false
  • Return: Number type

From the buffer object, a signed 16-bit integer is read in a special endian byte order format, depending on the specified offset.

If the noAssert true, the offset will not be validated. T his offset may exceed the end of the buffer. The default is false

For example:

var buf = new Buffer(4);

buf[0] = 0x3;
buf[1] = 0x4;
buf[2] = 0x23;
buf[3] = 0x42;

console.log(buf.readUInt16BE(0));
console.log(buf.readUInt16LE(0));
console.log(buf.readUInt16BE(1));
console.log(buf.readUInt16LE(1));
console.log(buf.readUInt16BE(2));
console.log(buf.readUInt16LE(2));

// 0x0304
// 0x0403
// 0x0423
// 0x2304
// 0x2342
// 0x4223

buf.readUInt32LE(offset[, noAssert])

buf.readUInt32BE(offset[, noAssert])

  • offset Number type
  • noAssert Boolean, optional parameter, default: false
  • Return: Number type

From this buffer object, a signed 32-bit integer is read in the specified endian byte order format, depending on the specified offset.

If the noAssert true, the offset will not be validated. T his offset may exceed the end of the buffer. The default is false

For example:

var buf = new Buffer(4);

buf[0] = 0x3;
buf[1] = 0x4;
buf[2] = 0x23;
buf[3] = 0x42;

console.log(buf.readUInt32BE(0));
console.log(buf.readUInt32LE(0));

// 0x03042342
// 0x42230403

buf.readInt8(offset[, noAssert])

  • offset Number type
  • noAssert Boolean, optional parameter, default: false
  • Return: Number type

From this buffer object, read a signed 8-bit integer based on the specified offset.

If the noAssert true, the offset will not be validated. T his means that the offer may exceed the end of the buffer. The default is false

Returns buffer.readUInt8 unless the buffer contains a signed value that complements 2.

buf.readInt16LE(offset[, noAssert])

buf.readInt16BE(offset[, noAssert])

  • offset Number type
  • noAssert Boolean, optional parameter, default: false
  • Return: Number type

From this buffer object, a signed 16-bit integer is read in a special endian format, depending on the specified offset.

If the noAssert true, the offset will not be validated. T his means that the offer may exceed the end of the buffer. The default is false

Return is buffer.readUInt16 unless the buffer contains a signed value that complements 2.

buf.readInt32LE(offset[, noAssert])

buf.readInt32BE(offset[, noAssert])

  • offset Number type
  • noAssert Boolean, optional parameter, default: false
  • Return: Number type

From this buffer object, a signed 32-bit integer is read in the specified endian byte order format, depending on the specified offset.

If the noAssert true, the offset will not be validated. T his offset may exceed the end of the buffer. The default is false

Returns buffer.readUInt32 unless the buffer contains a signed value that complements 2.

buf.readFloatLE(offset[, noAssert])

buf.readFloatBE(offset[, noAssert])

  • offset Number type
  • noAssert Boolean, optional parameter, default: false
  • Return: Number type

From this buffer object, a 32-bit floating point is read in the specified endian byte order format, depending on the specified offset.

If the noAssert true, the offset will not be validated. T his offset may exceed the end of the buffer. The default is false

For example:

var buf = new Buffer(4);

buf[0] = 0x00;
buf[1] = 0x00;
buf[2] = 0x80;
buf[3] = 0x3f;

console.log(buf.readFloatLE(0));

// 0x01

buf.readDoubleLE(offset[, noAssert])

buf.readDoubleBE(offset[, noAssert])

  • offset Number type
  • noAssert Boolean, optional parameter, default: false
  • Return: Number type

From this buffer object, a 64-bit double is read in the specified endian byte order format, depending on the specified offset.

If the noAssert true, the offset will not be validated. T his offset may exceed the end of the buffer. The default is false

For example:

var buf = new Buffer(8);

buf[0] = 0x55;
buf[1] = 0x55;
buf[2] = 0x55;
buf[3] = 0x55;
buf[4] = 0x55;
buf[5] = 0x55;
buf[6] = 0xd5;
buf[7] = 0x3f;

console.log(buf.readDoubleLE(0));

// 0。3333333333333333

buf.writeUInt8(value, offset[, noAssert])

  • value Number type
  • offset Number type
  • noAssert Boolean, optional parameter, default: false

Write value to offset based on value offset. Note: value must be a legitimate signed 8-bit integer.

If the noAssert true, the offset will not be validated. T his value may be too offset may exceed the end of the buffer, causing the value to be discarded. D on't use this parameter unless you're very sure about it. The default is false

For example:

var buf = new Buffer(4);
buf.writeUInt8(0x3, 0);
buf.writeUInt8(0x4, 1);
buf.writeUInt8(0x23, 2);
buf.writeUInt8(0x42, 3);

console.log(buf);

// <Buffer 03 04 23 42>

buf.writeUInt16LE(value, offset[, noAssert])

buf.writeUInt16BE(value, offset[, noAssert])

  • value Number type
  • offset Number type
  • noAssert Boolean, optional parameter, default: false

Value is written offset based on the incoming offset offset and value specified endian format. Note: value must be a legitimate signed 16-bit integer.

If the noAssert true, the value offset will not be validated. T his value may be too offset may exceed the end of the buffer, causing the value to be discarded. T ry not to use this parameter unless you are very sure about it. The default is false

For example:

var buf = new Buffer(4);
buf.writeUInt16BE(0xdead, 0);
buf.writeUInt16BE(0xbeef, 2);

console.log(buf);

buf.writeUInt16LE(0xdead, 0);
buf.writeUInt16LE(0xbeef, 2);

console.log(buf);

// <Buffer de ad be ef>
// <Buffer ad de ef be>

buf.writeUInt32LE(value, offset[, noAssert])

buf.writeUInt32BE(value, offset[, noAssert])

  • value Number type
  • offset Number type
  • noAssert Boolean, optional parameter, default: false

Value is written offset based on the incoming offset offset and value specified endian format. Note: value must be a legitimate signed 32-bit integer.

If the noAssert true, the value offset will not be validated. T his value may be too large, or offset may exceed the end of the buffer, causing value value be discarded. T ry not to use this parameter unless you are very sure about it. The default is false

For example:

var buf = new Buffer(4);
buf.writeUInt32BE(0xfeedface, 0);

console.log(buf);

buf.writeUInt32LE(0xfeedface, 0);

console.log(buf);

// <Buffer fe ed fa ce>
// <Buffer ce fa ed fe>

buf.writeInt8(value, offset[, noAssert])

  • value Number type
  • offset Number type
  • noAssert Boolean, optional parameter, default: false

Write value to offset based on value offset. Note: value must be a legitimate signed 8-bit integer.

If the noAssert true, the value offset will not be validated. T his value may be too large, or offset may exceed the end of the buffer, causing value value be discarded. T ry not to use this parameter unless you are very sure about it. The default is false

Works buffer.writeUInt8 unless you write a signed integer with 2 complements to the buffer. 有符号整数

buf.writeInt16LE(value, offset[, noAssert])

buf.writeInt16BE(value, offset[, noAssert])

  • value Number type
  • offset Number type
  • noAssert Boolean, optional parameter, default: false

Value is written offset based on the incoming offset offset and value specified endian format. Note: value must be a legitimate signed 16-bit integer.

If the noAssert true, the value offset will not be validated. T his value may be too large, or offset may exceed the end of the buffer, causing value value be discarded. T ry not to use this parameter unless you are very sure about it. The default is false

Works buffer.writeUInt16* unless you write a signed integer with 2 complements to the buffer

buf.writeInt32LE(value, offset[, noAssert])

buf.writeInt32BE(value, offset[, noAssert])

  • value Number type
  • offset Number type
  • noAssert Boolean, optional parameter, default: false

Value is written offset based on the incoming offset offset and value specified endian format. Note: value must be a legitimate signed 32-bit integer.

If the noAssert true, the value offset will not be validated. T his value may be too large, or offset may exceed the end of the buffer, causing value value be discarded. T ry not to use this parameter unless you are very sure about it. The default is false

Works buffer.writeUInt32* unless you write a signed integer with 2 complements, signed shaping, to buffer.

buf.writeFloatLE(value, offset[, noAssert])

buf.writeFloatBE(value, offset[, noAssert])

  • value Number type
  • offset Number type
  • noAssert Boolean, optional parameter, default: false

Value is written offset based on the incoming offset offset and value specified endian format. Note: When value not a value of a 32-bit floating-point type, the result will be uncertain.

If the noAssert true, the value offset will not be validated. T his means that the value may be too large, or the offset may exceed the end of the buffer, causing value be discarded. T ry not to use this parameter unless you are very sure about it. The default is false

For example:

var buf = new Buffer(4);
buf.writeFloatBE(0xcafebabe, 0);

console.log(buf);

buf.writeFloatLE(0xcafebabe, 0);

console.log(buf);

// <Buffer 4f 4a fe bb>
// <Buffer bb fe 4a 4f>

buf.writeDoubleLE(value, offset[, noAssert])

buf.writeDoubleBE(value, offset[, noAssert])

  • value Number type
  • offset Number type
  • noAssert Boolean, optional parameter, default: false

Value is written offset based on the incoming offset offset and value specified endian format. Note: value be a valid 64-bit double type value.

If the noAssert true, the value offset will not be validated. T his value may be too large, or offset may exceed the end of the buffer, causing value value be discarded. T ry not to use this parameter unless you are very sure about it. The default is false

For example:

var buf = new Buffer(8);
buf.writeDoubleBE(0xdeadbeefcafebabe, 0);

console.log(buf);

buf.writeDoubleLE(0xdeadbeefcafebabe, 0);

console.log(buf);

// <Buffer 43 eb d5 b7 dd f9 5f d7>
// <Buffer d7 5f f9 dd b7 d5 eb 43>

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

  • value
  • offset Number type, Optional
  • end Number type, Optional

Fill this buffer with the specified value If offset specified (0 by default) end (by default buffer.length the entire buffer is populated.

var b = new Buffer(50);
b.fill("h");

buffer.iNSPECT_MAX_BYTES

  • Number type, default: 50

Sets how many bytes will be returned when the buffer.inspect() method is called. The user module can override this value.

Note that this property require('buffer') module. This property is not in the global variable Buffer, nor is it in the example of the buffer.

Class: SlowBuffer

Returns a Buffer that is not managed by Buffer

A large number of independently allocated Buffer easy to bring garbage, in order to avoid this situation, less than 4KB of space are cut from a larger independent object. T his strategy improves both performance and memory usage. V8 does not need to track and clean up too many Persistent objects.

When a developer needs to keep a small piece of data in the pool for a period of time, it is better to create a Buffer instance with FlowBuffer that is not managed by the pool and copy the data as follows:

// need to keep around a few small chunks of memory
var store = [];

socket。on('readable', function() {
  var data = socket。read();
  // allocate for retained data
  var sb = new SlowBuffer(10);
  // copy the data into the new allocation
  data。copy(sb, 0, 0, 10);
  store。push(sb);
});

Use with caution only as a last resort when you often find excessive memory retention in their applications.