Personal understanding of typed data is the memory allocation area, the storage of different data is the view DataView
var buffers = [];
var json = {"id": +, "name": "China"};
var buf = new Buffer (json.stringify (JSON));
Define a Arraybuffer
var ab = new ArrayBuffer (6);//define a 6-byte memory area
var ablength = AB.BYTELENGTH;//6
Check if memory allocation is successful you need to compare ablength
if (ablength = = = 6) {//Success}else{//Failure}
Arraybuffer can store multiple types of data, and different types of storage are DataView
The types of DataView are as follows:
- Int8array:8-bit signed integer, 1 bytes in length.
- Uint8array:8-bit unsigned integer, 1 bytes in length.
- INT16ARRAY:16-bit signed integer, 2 bytes in length.
- uint16array:16-bit unsigned integer, 2 bytes in length.
- INT32ARRAY:32-bit signed integer, 4 bytes in length.
- uint32array:32-bit unsigned integer, 4 bytes in length.
- float32array:32 bit floating point number, length 4 bytes.
- float64array:64 bit floating point number, length 8 bytes.
Construction of Dataview,new DataView (ArrayBuffer); The parameter must be present and must be Arraybuffer
The DataView view provides the following methods for writing to memory: There are three parameters the first parameter is the byte ordinal, the second parameter is the write data, the third parameter is True|false optional, true is low-byte write, the default is high-byte write
- SetInt8: Writes a 1-byte 8-bit integer.
- SetUint8: Writes a 1-byte, 8-bit unsigned integer.
- SetInt16: Writes a 2-byte 16-bit integer.
- SetUint16: Writes a 2-byte, 16-bit unsigned integer.
- SetInt32: Writes a 4-byte 32-bit integer.
- SetUint32: Writes a 4-byte, 32-bit unsigned integer.
- SetFloat32: Writes a 4-byte 32-bit floating-point number.
- SetFloat64: Writes a 8-byte 64-bit floating-point number.
method to read memory
- GetInt8: Reads 1 bytes and returns a 8-bit integer.
- GetUint8: Reads 1 bytes and returns an unsigned 8-bit integer.
- GetInt16: Reads 2 bytes and returns a 16-bit integer.
- GetUint16: Reads 2 bytes and returns an unsigned 16-bit integer.
- GetInt32: Reads 4 bytes and returns a 32-bit integer.
- GetUint32: Reads 4 bytes and returns an unsigned 32-bit integer.
- GetFloat32: Reads 4 bytes and returns a 32-bit floating-point number.
- GetFloat64: Reads 8 bytes and returns a 64-bit floating-point number.
Storage of data
var dv = new DataView (AB);
Dv.setuint16 (0, Math.pow (2, 16)-1);
Dv.setuint16 (2, Math.pow (2, 16)-2);
Dv.setuint16 (4, buf.length);
Buffers.push (New Buffer (New Uint8array (AB)));
Buffers.push (BUF);
var resbuf = Buffer.concat (buffers);//The total combination of Buffer;
Unpacking the data
var a1 = resbuf.readuint16be (0);//Reads a 16-bit unsigned integer value from the Cache object, in high-byte-order storage mode
var a2 = Resbuf.readuint16be (2);
var a3 = Resbuf.readuint16be (4);
var str = Resbuf.slice (6, A3 + 6);//Take string contents
Finally, the A1,A2,A3,STR is the data that was just stored
JavaScript arraybuffer operations for typed arrays and views