DataView view
If a piece of data includes multiple types (such as HTTP data from a server), it is possible to manipulate the DataView view in addition to building a composite view of the Arraybuffer object.
The DataView view provides more operational options and supports setting the byte order. Originally, in the design purpose, the Arraybuffer object of various types of views, is used to network cards, sound cards and other native devices to transmit data, so the use of the local byte order can be, and DataView is designed to deal with the data transmitted from the network equipment, So the big endian byte order or the small end byte sequence can be set by itself.
DataView itself is also a constructor that takes a Arraybuffer object as a parameter and generates a view.
DataView (ArrayBuffer buffer [, byte start position [, length]]);
Here is an example.
var New ArrayBuffer (); var New DataView (buffer);
Example 2: Using DataView can also avoid some exceptions
//Convert a string into a Blob objectvarBlob =NewBlob ([' Chinese string '], {type:' Text/plain '});//convert BLOB objects to ArrayBuffervarReader =NewFileReader (); Reader.readasarraybuffer (BLOB); Reader.onload=function(e) {console.info (reader.result);//ArrayBuffer {} //frequently encountered exceptions uncaught rangeerror:byte length of Int16array should be a multiple of 2 //var buf = new Int16array (reader.result); //Console.info (BUF); //convert Arraybufferview to blob varBUF =NewDataView (Reader.result); Console.info (BUF); //DataView {}Reader.readastext (NewBlob ([buf]), ' Utf-8 '); Reader.onload=function() {console.info (reader.result);//Chinese string };}
The DataView view provides the following methods for reading 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.
The parameters for this series of Get methods are a sequence of bytes that indicate which byte to start reading from.
var New ArrayBuffer (); var New DataView (buffer); // reads a 8-bit unsigned integer from the 1th byte var v1 = dv.getuint8 (0); // reads a 16-bit unsigned integer from the 2nd byte var v2 = dv.getuint16 (1// reads a 16-bit unsigned integer var v3 from the 4th byte = Dv.getuint16 (3);
The above code reads the first 5 bytes of the Arraybuffer object, which has a 8-bit integer and two 16-bit integers.
If you read two or more bytes at one time, you have to be clear about how the data is stored, whether it is a small endian or an end byte sequence. By default, the Get method of DataView uses the big-endian byte-order to interpret the data, and if you need to use a small-endian byte-order interpretation, you must specify true in the second argument of the Get method.
// small-endian byte-order var true ); // Big endian byte order var false ); // Big endian byte order var v3 = dv.getuint16 (3);
The DataView view provides the following methods for writing memory:
- 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.
This series of set methods accepts two parameters, the first parameter is the byte ordinal, which is the byte from which the write begins, and the second parameter is the data written. For those methods that write two or more bytes, you need to specify a third argument, false or undefined to write with a big endian byte-order, and true to use a small byte-order write.
// in the 1th byte, a 32-bit integer with a value of 25 is written in a endian byte order false // in the 5th Byte, write a 32-bit integer value of 25 in the endian byte sequence dv.setint32 (4, +// in the 9th byte, Writes a 32-bit floating-point number with a value of 2.5 in the small-endian byte order true);
If you are unsure of the byte order of the computer you are using, you can use the following method of judging.
var littleendian = (function() { varnew ArrayBuffer (2); New true ); return New Int16array (buffer) [0] = = =;}) ();
Returns true if the byte order is small, or if False is the endian byte.
More typed arrays: http://www.cnblogs.com/tianma3798/p/4294433.html
More Dataview:https://developer.mozilla.org/en-us/docs/web/javascript/reference/global_objects/dataview
HTML5 Arraybufferview DataView