Node.js Buffer applies what you learn
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);
Create a Buffer instance from the given array
[code]var buffer = new Buffer([10, 20, 30, 40]);
[code]var buffer = new Buffer('光明大神棍的博客', 'utf-8');
[code]buffer.write(string[, offset, length, encoding]) 参数 string 必填 要输入缓冲区的字符串 offset 可选 缓冲区开始写入的索引值,默认为 0 length 可选 写入的字节数,默认为 buffer.length encoding 可选 使用的编码。默认为 'utf8' 返回值: 实际写入的大小。如果 buffer 空间不足, 则只会写入部分字符串
[code]var buffer = new Buffer(100); var len = buffer.write('http://www.lamport.me/club'); console.log('事件输入的字符:' + len);
Read data from buffer
[code]buffer.toString([ending, start, end]) 参数 encoding - 使用的编码。默认为 'utf8' 。 start - 指定开始读取的索引位置,默认为 0。 end - 结束位置,默认为缓冲区的末尾。 返回值 解码缓冲区数据并以指定编码返回字符串
Case: buffer2.js
[code]var buffer = new Buffer(26); for (var i = 0; i < 26; i++) { buffer[i] = i + 97; } console.log(buffer.toString('ascii')); console.log(buffer.toString('ascii', 0, 4)); console.log(buffer.toString('utf8', 0, 4)); console.log(buffer.toString(undefined, 0, 4));
Convert Buffer to JSON object
toJSON(buffer)
Case: buffer3.js
[code]var buffer = new Buffer('http://www.lamport.me/club'); var data =buffer.toJSON(buffer); console.log(data);
Merge buffer
[code]Buffer.concat(list[, totalLength]) 参数 list - 用于合并的 Buffer 对象数组列表。 totalLength - 指定合并后Buffer对象的总长度。
Case: buffer4.js
[code]var bf1 = new Buffer("Hello "); var bf2 = new Buffer("World"); var bf3 = Buffer.concat([bf1, bf2]); console.log(bf3.toString());
[code]buf.copy(targetBuffer[, targetStart[, sourceStart[, sourceEnd]]]) 参数 targetBuffer - 要拷贝的 Buffer 对象。 targetStart - 数字, 可选, 默认: 0 sourceStart - 数字, 可选, 默认: 0 sourceEnd - 数字, 可选, 默认: buffer.length 返回值 没有返回值。
[code]var buffer1 = new Buffer('ABC'); var buffer2 = new Buffer(3); buffer1.copy(buffer2); console.log(buffer2.toString());
[code]buf.slice([start[, end]]) 参数 start - 数字, 可选, 默认: 0 end - 数字, 可选, 默认: buffer.length 返回值 返回一个新的缓冲区,它和旧缓冲区指向同一块内存,但是从索引 start 到 end 的位置剪切。
[code]var buffer = new Buffer("ABCD"); var buffer2 = buffer.slice(0, 2); console.log(buffer2.toString());
[code]buf.length; 返回值 返回 Buffer 对象所占据的内存长度。
[code]var buffer = new Buffer("abcd"); console.log(buffer.length);

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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!

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

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?

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

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

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!

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".
