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(buffer, byteOffset, byteLength);

dataView

Required. The variable name to which the DataView object is assigned.

buffer

The ArrayBuffer that the DataView represents.

byteOffset

Optional. Specifies the offset in bytes from the start of the buffer at which the DataView should begin.

byteLength

Optional. Specifies the length (in bytes) of the section of the buffer that the DataView should represent.

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

buffer Property

Read-only. Gets the ArrayBuffer that is referenced by this view.

byteLength Property

Read-only. The length of this view from the start of its ArrayBuffer, in bytes, as fixed at construction time.

byteOffset Property

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

getInt8 Method

Gets the Int8 value at the specified byte offset from the start of the view.

getUint8 Method (DataView)

Gets the Uint8 value at the specified byte offset from the start of the view.

getInt16 Method (DataView)

Gets the Int16 value at the specified byte offset from the start of the view.

getUint16 Method (DataView)

Gets the Uint16 value at the specified byte offset from the start of the view.

getInt32 Method (DataView)

Gets the Int32 value at the specified byte offset from the start of the view.

getUint32 Method (DataView)

Gets the Uint32 value at the specified byte offset from the start of the view.

getFloat32 Method (DataView)

Gets the Float32 value at the specified byte offset from the start of the view.

getFloat64 Method (DataView)

Gets the Float64 value at the specified byte offset from the start of the view.

setInt8 Method (DataView)

Stores a Int8 value at the specified byte offset from the start of the view.

setUint8 Method (DataView)

Stores a Uint8 value at the specified byte offset from the start of the view.

setInt16 Method (DataView)

Stores a Int16 value at the specified byte offset from the start of the view.

setUint16 Method (DataView)

Stores a Uint16 value at the specified byte offset from the start of the view.

setInt32 Method (DataView)

Stores a Int32 value at the specified byte offset from the start of the view.

setUint32 Method (DataView)

Stores a Uint32 value at the specified byte offset from the start of the view.

setFloat32 Method (DataView)

Stores a Float32 value at the specified byte offset from the start of the view.

setFloat64 Method (DataView)

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 following document modes: Internet Explorer 10 standards and Internet Explorer 11 standards. Also supported in Store apps (Windows 8 and Windows Phone 8.1). 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.

Show:
© 2014 Microsoft