Buffer module
JS was originally designed for browsers, so it can handle unicode-encoded strings well, but it cannot handle binary data well. This is a problem with Node.js, because Node.js is designed to send and receive data that is often transmitted in binary format on the network. for example:
- Send and receive data through TCP connection;
- Read binary data from images or compressed files;
- Read and write data from the file system;
- Process binary data streams from the network
The Buffer module brings a method to Node.js to store raw data, so binary data can be used in the context of js. Whenever it is necessary to process data moved in I/O operations in Node.js, it is possible to use the Buffer module.
Class: Buffer
The Buffer class is a global variable type used to directly process binary data. It can be built in a variety of ways.
The raw data is saved in an instance of the Buffer class. A Buffer instance is similar to an array of integers
1.new Buffer(size): Allocate a new buffer size 8-bit bytes whose size is size.
2.new Buffer(array): Assign a new buffer to use an 8-bit byte array.
3.new Buffer(str, [encoding]):encoding String type-What encoding method is used, and the parameters are optional.
4. Class method: Buffer.isEncoding(encoding): Return true if the given encoding encoding is valid, otherwise return false.
5. Class method: Buffer.isBuffer(obj): Test whether this obj is a Buffer. Returns Boolean
6. Class method: Buffer.concat(list, [totalLength]): list {Array} array type, Buffer array, used to be connected. totalLength {Number} type total size of all buffers of the above Buffer array.
In addition to reading files to get Buffer instances, it can also be constructed directly, for example:
The code copy is as follows:
var bin = new Buffer([ 0x48, 0x65, 0x6c, 0x6c, 0x6c ]);
Buffer is similar to a string. In addition to using the .length attribute to obtain the byte length, you can also use the [index] method to read the bytes at the specified position, for example:
The code copy is as follows:
bin[0]; // => 0x48;
Buffer and string can be converted to each other, for example, binary data can be converted to strings using a specified encoding:
The code copy is as follows:
var str = bin.toString('utf-8'); // => "hello"
Instead of returning a new Buffer, the .slice method returns a pointer to a location in the middle of the original Buffer, as shown below.
The code copy is as follows:
1.[ 0x48, 0x65, 0x6c, 0x6c, 0x6c]
2. ^ ^
3. | |
4. bin bin.slice(2)
Write to the buffer
The code copy is as follows:
var buffer = new Buffer(8);//Create a buffer with 8 bytes of memory allocated
console.log(buffer.write('a','utf8'));//Output 1
This will write the character "a" to the buffer, and the node returns the number of bytes written to the buffer after being encoded. The utf-8 encoding of the letter a here takes up 1 byte.
Copy buffer
Node.js provides a method to copy the entire contents of the Buffer object into another Buffer object. We can only copy between existing Buffer objects, so we have to create them.
The code copy is as follows:
buffer.copy(bufferToCopyTo)
Among them, bufferToCopyTo is the target Buffer object to be copied. The following example:
The code copy is as follows:
var buffer1 = new Buffer(8);
buffer1.write('nice to meet u','utf8');
var buffer2 = new Buffer(8);
buffer1.copy(buffer2);
console.log(buffer2.toString());//nice to meet u
Stream module
In UNIX type operating systems, streaming is a standard concept. There are three main streams as follows:
1. Standard input
2. Standard output
3. Standard errors
Readable stream
If the buffer is the way Node.js handles raw data, then the stream is usually the way Node.js moves data. Streams in Node.js are readable or writable. Many modules in Node.js use streams, including HTTP and file systems.
Suppose we create a classmates.txt file and read the name list from it so that we can use this data. Since data is a stream, this means that before completing file reading, you can act on the data from the first few bytes received. This is a common pattern in Node.js:
The code copy is as follows:
var fs = require('fs');
var stream = fs.ReadStream('classmates.txt');
stream.setEncoding('utf8');
stream.on('data', function (chunk) {
console.log('read some data')
});
stream.on('close', function () {
console.log('all the data is read')
});
In the above example, event data is triggered when new data is received. The closing event is triggered when the file read is completed.
Writable stream
Obviously, we can also create a writable stream to write data. This means that just a simple script can be used to read into the file and then write to another file:
The code copy is as follows:
var fs = require('fs');
var readableStream = fs.ReadStream('classmates.txt');
var writableStream = fs.writeStream('names.txt');
readableStream.setEncoding('utf8');
readableStream.on('data', function (chunk) {
writableStream.write(chunk);
});
readableStream.on('close', function () {
writableStream.end();
});
Now, when a data event is received, the data is written to the writable stream.
readable.setEncoding(encoding): Return: this
readable.resume(): Same as above. This method allows the readable stream to continue triggering the data event.
readable.pause(): Same as above. This method causes a stream in flow mode to stop triggering the data event, switch to non-flow mode, and leave subsequent available data in the internal buffer.
Class: stream.Writable
The Writable stream interface is an abstraction of the data you are writing to a target.
1.writable.write(chunk, [encoding], [callback]):
chunk {String | Buffer} Data to be written
encoding {String} encoding, if chunk is a string
callback {Function} Callback after data block writing
Return: {Boolean} true if the data has been processed all.
This method writes data to the underlying system and calls the given callback after the data is processed.
2.writable.cork(): Forced to trap all writes.
The stuck data will be written when the .uncork() or .end() call is called.
3.writable.end([chunk], [encoding], [callback])
chunk {String | Buffer} Optional, data to be written
encoding {String} encoding, if chunk is a string
callback {Function} optional, callback after the stream ends
Calling write() after calling end() will generate an error.
The code copy is as follows:
// Write 'hello, ' and then end with 'world!'
http.createServer(function (req, res) {
res.write('hello, ');
res.end('world!');
//Now is not allowed to continue writing
});