DataView Object
You can use a DataView object to read and write the different kinds of binary data to any location in the ArrayBuffer.
dataView = new DataView(DataView(buffer, byteOffset, byteLength));
If both byteOffset and byteLength are omitted, the DataView spans the entire ArrayBuffer range. If the byteLength is omitted, the DataView extends from the given byteOffset until the end of the ArrayBuffer. If the given byteOffset and byteLength references an area beyond the end of the ArrayBuffer, an exception is raised.
The following table lists the properties of the DataView object.
|
Property |
Description |
|---|---|
|
Read-only. Gets the ArrayBuffer that is referenced by this view. |
|
|
Read-only. The length of this view from the start of its ArrayBuffer, in bytes, as fixed at construction time. |
|
|
Read-only. The offset of this view from the start of its ArrayBuffer, in bytes, as fixed at construction time. |
The following table lists the methods of the DataViewobject.
|
Method |
Description |
|---|---|
|
Gets the Int8 value at the specified byte offset from the start of the view. |
|
|
Gets the Uint8 value at the specified byte offset from the start of the view. |
|
|
Gets the Int16 value at the specified byte offset from the start of the view. |
|
|
Gets the Uint16 value at the specified byte offset from the start of the view. |
|
|
Gets the Int32 value at the specified byte offset from the start of the view. |
|
|
Gets the Uint32 value at the specified byte offset from the start of the view. |
|
|
Gets the Float32 value at the specified byte offset from the start of the view. |
|
|
Gets the Float64 value at the specified byte offset from the start of the view. |
|
|
Stores a Int8 value at the specified byte offset from the start of the view. |
|
|
Stores a Uint8 value at the specified byte offset from the start of the view. |
|
|
Stores a Int16 value at the specified byte offset from the start of the view. |
|
|
Stores a Uint16 value at the specified byte offset from the start of the view. |
|
|
Stores a Int32 value at the specified byte offset from the start of the view. |
|
|
Stores a Uint32 value at the specified byte offset from the start of the view. |
|
|
Stores a Float32 value at the specified byte offset from the start of the view. |
|
|
Stores a Float64 value at the specified byte offset from the start of the view. |
The following example shows how to use a DataView object to process the binary data acquired from an XmlHttpRequest:
var req = new XMLHttpRequest(); req.open('GET', "http://www.example.com"); req.responseType = "arraybuffer"; req.send(); req.onreadystatechange = function () { if (req.readyState === 4) { var buffer = req.response; var dataView = new DataView(buffer); var ints = new Int32Array(dataView.byteLength / 4); for (var i = 0; i < ints.length; i++) { ints[i] = dataview.getInt32(i * 4); } alert(ints[10]); } }
Supported in the Internet Explorer 10 standards document mode. Also supported in Windows Store apps. See Version Information.
Not supported in the following document modes: Quirks, Internet Explorer 6 standards, Internet Explorer 7 standards, Internet Explorer 8 standards, Internet Explorer 9 standards.