Home > Web Front-end > JS Tutorial > Node.js Buffer applies what you learn

Node.js Buffer applies what you learn

黄舟
Release: 2017-01-17 15:46:22
Original
1156 people have browsed it

Learning points:

Write to buffer

Read data from buffer

Convert Buffer to JSON object

Merge buffers

Copy buffer

Buffer clipping

Buffer length

Node.js Buffer (buffer)

This class is used Create a buffer area specifically to store binary data

Create the Buffer class

Create a Buffer instance with a length of 10 bytes

[code]var buffer = new Buffer(10);
Copy after login

Create a Buffer instance from the given array

[code]var buffer = new Buffer([10, 20, 30, 40]);
Copy after login
Create a Buffer instance through a string
[code]var buffer = new Buffer('光明大神棍的博客', 'utf-8');
Copy after login
Write to the buffer
[code]buffer.write(string[, offset, length, encoding])
参数
    string   必填 要输入缓冲区的字符串
    offset   可选 缓冲区开始写入的索引值,默认为 0 
    length   可选 写入的字节数,默认为 buffer.length
    encoding 可选 使用的编码。默认为 'utf8' 
返回值:
    实际写入的大小。如果 buffer 空间不足, 则只会写入部分字符串
Copy after login
Case: buffer.js
[code]var buffer = new Buffer(100);
var len = buffer.write('http://www.lamport.me/club');
console.log('事件输入的字符:' + len);
Copy after login

Node.js Buffer applies what you learn

Read data from buffer

[code]buffer.toString([ending, start, end])
参数
    encoding - 使用的编码。默认为 'utf8' 。
    start - 指定开始读取的索引位置,默认为 0。
    end - 结束位置,默认为缓冲区的末尾。
返回值
    解码缓冲区数据并以指定编码返回字符串
Copy after login

Case: buffer2.js

[code]var buffer = new Buffer(26);
for (var i = 0; i < 26; i++) {
    buffer[i] = i + 97;
}
console.log(buffer.toString(&#39;ascii&#39;));
console.log(buffer.toString(&#39;ascii&#39;, 0, 4));
console.log(buffer.toString(&#39;utf8&#39;, 0, 4));
console.log(buffer.toString(undefined, 0, 4));
Copy after login

Node.js Buffer applies what you learn

Convert Buffer to JSON object

toJSON(buffer)

Case: buffer3.js

[code]var buffer = new Buffer(&#39;http://www.lamport.me/club&#39;);
var data =buffer.toJSON(buffer);
console.log(data);
Copy after login

Node.js Buffer applies what you learn

Merge buffer

[code]Buffer.concat(list[, totalLength])
参数
    list - 用于合并的 Buffer 对象数组列表。
    totalLength - 指定合并后Buffer对象的总长度。
Copy after login

Case: buffer4.js

[code]var bf1 = new Buffer("Hello ");
var bf2 = new Buffer("World");
var bf3 = Buffer.concat([bf1, bf2]);
console.log(bf3.toString());
Copy after login

Node.js Buffer applies what you learn

##Copy buffer

[code]buf.copy(targetBuffer[, targetStart[, sourceStart[, sourceEnd]]])
参数
    targetBuffer - 要拷贝的 Buffer 对象。
    targetStart - 数字, 可选, 默认: 0
    sourceStart - 数字, 可选, 默认: 0
    sourceEnd - 数字, 可选, 默认: buffer.length
返回值
    没有返回值。
Copy after login

Case: buffer5.js

[code]var buffer1 = new Buffer(&#39;ABC&#39;);
var buffer2 = new Buffer(3);
buffer1.copy(buffer2);
console.log(buffer2.toString());
Copy after login

Node.js Buffer applies what you learn

Buffer clipping

[code]buf.slice([start[, end]])
参数
    start - 数字, 可选, 默认: 0
    end - 数字, 可选, 默认: buffer.length
返回值
    返回一个新的缓冲区,它和旧缓冲区指向同一块内存,但是从索引 start 到 end 的位置剪切。
Copy after login

Example: buffer6.js
[code]var buffer = new Buffer("ABCD");
var buffer2 = buffer.slice(0, 2);
console.log(buffer2.toString());
Copy after login

Node.js Buffer applies what you learn

Buffer length

[code]buf.length;
返回值
    返回 Buffer 对象所占据的内存长度。
Copy after login

Example:buffer7.js

[code]var buffer = new Buffer("abcd");
console.log(buffer.length);
Copy after login

Node.js Buffer applies what you learn

The above is the content of Node.js Buffer learning and application. For more related content, please pay attention to the PHP Chinese website (www.php .cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template