buffer

Object buffer

Binary data cache object for IO read-write data processing

Buffer object is global basic class, can be created with new Buffer(…) by anytime:

1
var buf = new Buffer();

Static function

isBuffer

Detects whether a given variable is a Buffer object

1
Buffer.isBuffer(v);

Parameter Usage:

Results:

1
2
3
4
5
6
exports.hi = v => {
var buf = new Buffer("abcd");
var str = "abcd"
console.log(Buffer.isBuffer(buf));
console.log(Buffer.isBuffer(str));
}

from

Use other Buffer to create Buffer object

1
Buffer.from(buffer,byteOffset,length);

Parameter Usage:

Results:

Example:

1
2
3
4
exports.hi = v => {
var buf = Buffer.from(new Buffer("abcd"), 1, 2);
console.log(buf.toString());
}

Use string to create Buffer object

1
Buffer.from(str,byteOffset,length);

Parameter Usage:

Results:

Example:

1
2
3
4
exports.hi = v => {
var buf = Buffer.from("abcd", 0, 2);
console.log(buf.toString());
}

Use string to create Buffer object

1
Buffer.from(str,codec);

Parameter Usage:

Results:

Example:

1
2
3
4
exports.hi = v => {
var buf = new Buffer(Awesome!", "utf8");
console.log(buf.toString());
}

concat

Concatenate data in multiple cache regions

1
Buffer.concat(buflist,cutLength);

Parameter Usage:

Results:

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
exports.hi = v => {
var buf1 = new Buffer("abcd");
var buf2 = new Buffer("efg");
var buf3 = new Buffer();
var bufArray = [buf1];
var bufRes = Buffer.concat(bufArray);
console.log(bufRes.toString());

bufArray = [buf1, buf2];
bufRes = Buffer.concat(bufArray);
console.log(bufRes.toString());

bufRes = Buffer.concat(bufArray, 6);
console.log(bufRes.toString());

buf1 = new Buffer([0x31, 0x32, 0x33, 0x34]);
buf2 = new Buffer([0x35, 0x36, 0x37, 0x38]);
bufArray = [buf1, buf2];
bufRes = Buffer.concat(bufArray);
console.log(bufRes.toString());

var buf2 = Buffer.concat([]);
console.log(buf2.length);
}

alloc

Allocates a new cache area of specified length. If the size is 0, a zero-length buffer is created.

1
Buffer.alloc(size,fill,codec);

Parameter Usage:

Results:

Example:

1
2
3
4
exports.hi = v => {
var buf1 = Buffer.alloc(10, 0, "utf8");
console.log(buf1.toString());
}

Allocates a new cache area of specified length. If the size is 0, a zero-length buffer is created.

1
Buffer Buffer.alloc(size,fill,codec);

Parameter Usage:

Results:

Example:

1
2
3
4
5
6
exports.hi = v => {
var buf1 = Buffer.alloc(11, 'aGVsbG8gd29ybGQ=', 'base64');
var buf2 = Buffer.alloc(16, 'aGVsbG8gd29ybGQ=', 'base64');
console.log(buf1.toString());
console.log(buf2.toString());
}

Allocates a new cache area of specified length. If the size is 0, a zero-length buffer is created.

1
Buffer.alloc(size,fill,codec);

Parameter Usage:

Results:

Example:

1
2
3
4
5
6
exports.hi = v => {
var buf1 = Buffer.alloc(11, new Buffer('aGVsbG8gd29ybGQ='), 'base64');
var buf2 = Buffer.alloc(16, new Buffer('aGVsbG8gd29ybGQ='), 'base64');
console.log(buf1.toString());
console.log(buf2.toString());
}

allocUnsafe

Allocates a new cache area of specified length. If the size is 0, a zero-length buffer is created.

1
Buffer.allocUnsafe(size);

Parameter Usage:

Results:

Example:

1
2
3
4
exports.hi = v => {
var buf1 = Buffer.allocUnsafe(10);
console.log(buf1.length);
}

allocUnsafeSlow

Allocates a new cache area of specified length. If the size is 0, a zero-length buffer is created.

1
Buffer.allocUnsafeSlow(size);

Parameter Usage:

Results:

Example:

1
2
3
4
exports.hi = v => {
var buf1 = Buffer.allocUnsafeSlow(10);
console.log(buf1.length);
}

isEncoding

Check whether the encoding format is supported

1
Buffer.isEncoding(codec);

Parameter Usage:

Results:

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
exports.hi = v => {
console.log(Buffer.isEncoding('utf8')); // true
console.log(Buffer.isEncoding('utf-8')); // true
console.log(Buffer.isEncoding('gbk')); // false
console.log(Buffer.isEncoding('gb2312')); // false
console.log(Buffer.isEncoding('hex')); // true
console.log(Buffer.isEncoding('base64')); // true
console.log(Buffer.isEncoding('jis')); // false
console.log(Buffer.isEncoding('aaabbbccc')); // false
console.log(Buffer.isEncoding('binary')); // false
console.log(Buffer.isEncoding('latin1')); // false
console.log(Buffer.isEncoding('big5')); // false
}

Member attribute

length

Integer, Get the size of the cached object

1
readonly Integer Buffer.length;

member function

Buffer

Cache object constructors

1
Buffer.Buffer(Array datas);

Parameter Usage:

1
2
3
4
exports.hi = v => {
var buf = new Buffer([0x31, 0x32, 0x33, 0x34])
console.log(buf.toString());
}

Cache object constructors

1
Buffer.Buffer(ArrayBuffer datas);

Parameter Usage:

1
2
3
4
5
6
7
exports.hi = v => {
var arr = new Uint16Array(2);
arr[0] = 5000;
arr[1] = 4000;
var buf = new Buffer(arr.buffer);
console.log(buf.hex());
}

Cache object constructors

1
Buffer.Buffer(TypedArray datas);

Parameter Usage:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
exports.hi = v => {
var arr = new Uint8Array(2);
arr[0] = 50;
arr[1] = 40;
var buf = new Buffer(arr);
console.log(buf.hex());

var arr = new Uint16Array(2);
arr[0] = 5000;
arr[1] = 4000;
var buf = new Buffer(arr);
console.log(buf.hex());

var arr = new Uint8Array([0x10, 0x20, 0x30]);
var arr1 = new Uint8Array(arr.buffer, 1, 2);
var buf = new Buffer(arr1);
console.log(buf.hex());
}

Cache object constructors

1
Buffer.Buffer(ArrayBufferView datas);

Parameter Usage:

1
2
3
4
5
6
7
exports.hi = v => {
var arr = new DataView(new ArrayBuffer(2));
arr.setInt8(0, 0x10);
arr.setInt8(1, 0x20);
var buf = new Buffer(arr);
console.log(buf.hex());
}

Cache object constructors

1
Buffer.Buffer(Buffer buffer);

Parameter Usage:

1
2
3
4
exports.hi = v => {
var buf = new Buffer(new Buffer("abcd"));
console.log(buf.toString());
}

Cache object constructors

1
2
Buffer.Buffer(String str,
String codec = "utf8");

Parameter Usage:

1
2
3
4
exports.hi = v => {
var buf = new Buffer("abcd", "utf8");
console.log(buf.toString());
}

Cache object constructors

1
Buffer.Buffer(Integer size = 0);

Parameter Usage:

Example:

1
2
3
4
exports.hi = v => {
var buf = new Buffer(100);
console.log(buf.length);
}

resize

resize cached object

1
Buffer.resize(Integer sz);

Parameter Usage:

Example:

1
2
3
4
5
exports.hi = v => {
var buf = new Buffer("abcded");
buf.resize(10);
console.log(buf);
}

append

Write a binary array in the end of cached object

1
Buffer.append(Buffer data);

Parameter Usage:

Example:

1
2
3
4
5
6
exports.hi = v => {
var buf = new Buffer([0x31, 0x32, 0x33, 0x34]);
buf.append("abcd");
buf.append([0x31, 0x32, 0x33, 0x34]);
console.log(buf.toString());
}

Write a string at the end of the cached object, which is written in utf-8 format

1
2
Buffer.append(String str,
String codec = "utf8");

Parameter Usage:

Example:

1
2
3
4
5
6
exports.hi = v => {
var buf = new Buffer([0x31, 0x32, 0x33, 0x34]);
buf.append("3132", "hex");
buf.append("MTIzNA==", "base64");
console.log(buf.toString());
}

write

Write specific string to cached object, default string is utf-8, Only part of the data is written when the boundary is crossed

1
2
3
4
Integer Buffer.write(String str,
Integer offset = 0,
Integer length = -1,
String codec = "utf8");

Parameter Usage:

Results:

Example:

1
2
3
4
5
exports.hi = v => {
var buf = new Buffer(10);
buf.write("abcd", 0, 4, 'utf8')
console.log(buf.toString('utf8', 0, 4));
}

Write specific string to cached object, default string is utf-8, Only part of the data is written when the boundary is crossed

1
2
3
Integer Buffer.write(String str,
Integer offset = 0,
String codec = "utf8");

Parameter Usage:

Results:

Example:

1
2
3
4
5
exports.hi = v => {
buf = new Buffer(10);
buf.write("MTIzNA==", 0, "base64");
console.log(buf.toString("utf8", 0, 4));
}

Write specific string to cached object, default string is utf-8, Only part of the data is written when the boundary is crossed

1
2
Integer Buffer.write(String str,
String codec = "utf8");

Parameter Usage:

Results:

Example:

1
2
3
4
5
exports.hi = v => {
buf = new Buffer(10);
buf.write("MTIzNA==", "base64");
console.log(buf.toString("utf8"));
}

fill

Fill in specific content data to buffer object

1
2
3
Buffer Buffer.fill(Integer v,
Integer offset = 0,
Integer end = -1);

Parameter Usage:

Results:

Example:

1
2
3
4
5
exports.hi = v => {
var buf = new Buffer(10);
buf.fill(1);
console.log(buf.toString());
}

Fill in specific content data to buffer object

1
2
3
Buffer Buffer.fill(Buffer v,
Integer offset = 0,
Integer end = -1);

Parameter Usage:

Results:

Example:

1
2
3
4
5
exports.hi = v => {
var buf = new Buffer(10);
buf.fill(new Buffer("abc"));
console.log(buf.toString());
}

Fill in specific content data to buffer object

1
2
3
Buffer Buffer.fill(String v,
Integer offset = 0,
Integer end = -1);

Parameter Usage:

Results:

Example:

1
2
3
4
5
exports.hi = v => {
var buf = new Buffer(10);
buf.fill("abc");
console.log(buf.toString());
}

indexOf

Return the location in Buffer where the specificdata first appears

1
2
Integer Buffer.indexOf(Integer v,
Integer offset = 0);

Parameter Usage:

Results:

Example:

1
2
3
4
5
exports.hi = v => {
var buf = new Buffer([0x31, 0x32, 0x33, 0x34, 0x00]);
console.log(buf.indexOf(0x33));
console.log(buf.indexOf(0x00));
}

Return the location in Buffer where the specificdata first appears

1
2
Integer Buffer.indexOf(String v,
Integer offset = 0);

Parameter Usage:

Results:

Example:

1
2
3
4
5
exports.hi = v => {
var buf = new Buffer("cacdbfcde");
console.log(buf.indexOf("cd", 1));

}

compare

Compares the contents of the cache area

1
Integer Buffer.compare(Buffer buf);

Parameter Usage:

Results:

Example:

1
2
3
4
5
6
exports.hi = v => {
var buf = new Buffer("abcd");
console.log(buf.compare(new Buffer("abcd")));
console.log(buf.compare(new Buffer("abc")));
console.log(buf.compare(new Buffer("abcde")));
}

copy

Copy data from the source cache object region to the target cache object region

1
2
3
4
Integer Buffer.copy(Buffer targetBuffer,
Integer targetStart = 0,
Integer sourceStart = 0,
Integer sourceEnd = -1);

Parameter Usage:

Results:

Example:

1
2
3
4
5
6
7
8
9
exports.hi = v => {
var buf1 = new Buffer([0x31, 0x32, 0x33]);
var arr = [0x34, 0x35, 0x36];

var buf2 = new Buffer(arr);
var sz = buf1.copy(buf2);
console.log(sz);
console.log(buf2.toString());
}

slice

Return a new cache object,Contains data that specifies the start to end of the cache

1
Buffer Buffer.slice(Integer start = 0);

Parameter Usage:

Results:

Example:

1
2
3
4
5
exports.hi = v => {
var buf = new Buffer(10);
buf.write("abcdefghih");
console.log(buf.slice(8).toString());
}

Return a new cache object,Contains data for the specified range, and returns only a valid portion of the data if the range exceeds the cache

1
2
Buffer Buffer.slice(Integer start,
Integer end);

Parameter Usage:

Results:

Example:

1
2
3
4
5
6
exports.hi = v => {
var buf = new Buffer(10);
buf.write("abcdefghih");
console.log(buf.slice(0, 3).toString());
console.log(buf.slice(0, 11).toString());
}

join

Puts all elements of the current object into a string

1
String Buffer.join(String separator = ",");

Parameter Usage:

Results:

Example:

1
2
3
4
exports.hi = v => {
var a = new Buffer([192, 168, 0, 1]);
console.log(a.join('.'));
}

reverse

Return a new cache object,Contains the inverted order of the current object data

1
Buffer Buffer.reverse();

Results:

Example:

1
2
3
4
exports.hi = v => {
var a = new Buffer("abcd");
console.log(a.reverse().toString());
}

equals

Compares whether the current object is equal to a given object

1
Boolean Buffer.equals(object expected);

Parameter Usage:

Results:

Example:

1
2
3
4
5
exports.hi = v => {
var buf = new Buffer("abcd");
console.log(buf.equals(new Buffer("abcd")));
console.log(buf.equals(new Buffer("abc")));
}

hex

Use hex to cache object content

1
String Buffer.hex();

Results:

base64

use base64 encodes to cache object content

1
String Buffer.base64();

Results:

keys

Return all arrays of binary data

1
Iterator Buffer.keys();

Results:

values

Return all arrays of binary data

1
Iterator Buffer.values();

Results:

entries

Return including object data [index, byte] Right iterator

1
Iterator Buffer.entries();

Results:

toArray

Return all arrays of binary data

1
Array Buffer.toArray();

Results:

Example:

1
2
3
4
exports.hi = v => {
var buf = new Buffer("buffer");
console.log(buf.toArray());
}

toString

Return encoding string of binary data

1
2
3
String Buffer.toString(String codec,
Integer offset = 0,
Integer end);

Parameter Usage:

Results:

Example:

1
2
3
4
exports.hi = v => {
var buf = new Buffer([0x31, 0x32, 0x33, 0x34]);
console.log(buf.toString("utf8", 1, 3));
}

Return encoding string of binary data

1
2
String Buffer.toString(String codec,
Integer offset = 0);

Parameter Usage:

Results:

Example:

1
2
3
4
5
6
exports.hi = v => {
var buf = new Buffer([0x31, 0x32, 0x33, 0x34]);
console.log(buf.toString("utf8", 1));
console.log(buf.toString("hex", 2));
console.log(buf.toString("base64", 2));
}

Return binary utf8 string

1
String Buffer.toString();

Results:

Example:

1
2
3
4
exports.hi = v => {
var buf = new Buffer([0x31, 0x32, 0x33, 0x34]);
console.log(buf.toString());
}

toJSON

Return JSON format of the object,Return a collection of readable properties defined by an object

1
String Buffer.toJSON();

Results:

Example:

1
2
3
4
exports.hi = v => {
var buf = new Buffer("buffer");
console.log(buf.toJSON());
}