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

Node.js Buffer applies what you learn

Jan 17, 2017 pm 03:46 PM

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)!


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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Detailed graphic explanation of the memory and GC of the Node V8 engine Detailed graphic explanation of the memory and GC of the Node V8 engine Mar 29, 2023 pm 06:02 PM

This article will give you an in-depth understanding of the memory and garbage collector (GC) of the NodeJS V8 engine. I hope it will be helpful to you!

An article about memory control in Node An article about memory control in Node Apr 26, 2023 pm 05:37 PM

The Node service built based on non-blocking and event-driven has the advantage of low memory consumption and is very suitable for handling massive network requests. Under the premise of massive requests, issues related to "memory control" need to be considered. 1. V8’s garbage collection mechanism and memory limitations Js is controlled by the garbage collection machine

Let's talk about how to choose the best Node.js Docker image? Let's talk about how to choose the best Node.js Docker image? Dec 13, 2022 pm 08:00 PM

Choosing a Docker image for Node may seem like a trivial matter, but the size and potential vulnerabilities of the image can have a significant impact on your CI/CD process and security. So how do we choose the best Node.js Docker image?

Let's talk in depth about the File module in Node Let's talk in depth about the File module in Node Apr 24, 2023 pm 05:49 PM

The file module is an encapsulation of underlying file operations, such as file reading/writing/opening/closing/delete adding, etc. The biggest feature of the file module is that all methods provide two versions of **synchronous** and **asynchronous**, with Methods with the sync suffix are all synchronization methods, and those without are all heterogeneous methods.

Node.js 19 is officially released, let's talk about its 6 major features! Node.js 19 is officially released, let's talk about its 6 major features! Nov 16, 2022 pm 08:34 PM

Node 19 has been officially released. This article will give you a detailed explanation of the 6 major features of Node.js 19. I hope it will be helpful to you!

Let's talk about the GC (garbage collection) mechanism in Node.js Let's talk about the GC (garbage collection) mechanism in Node.js Nov 29, 2022 pm 08:44 PM

How does Node.js do GC (garbage collection)? The following article will take you through it.

Let's talk about the event loop in Node Let's talk about the event loop in Node Apr 11, 2023 pm 07:08 PM

The event loop is a fundamental part of Node.js and enables asynchronous programming by ensuring that the main thread is not blocked. Understanding the event loop is crucial to building efficient applications. The following article will give you an in-depth understanding of the event loop in Node. I hope it will be helpful to you!

What should I do if node cannot use npm command? What should I do if node cannot use npm command? Feb 08, 2023 am 10:09 AM

The reason why node cannot use the npm command is because the environment variables are not configured correctly. The solution is: 1. Open "System Properties"; 2. Find "Environment Variables" -> "System Variables", and then edit the environment variables; 3. Find the location of nodejs folder; 4. Click "OK".

See all articles