Your Search Results

    DataView Redirect 2

    An ArrayBuffer is a useful object for representing an arbitrary chunk of data. In many cases, such data will be read from disk or from the network, and will not follow the alignment restrictions that are imposed on the Typed Array Views described earlier. In addition, the data will often be heterogeneous in nature and have a defined byte order.

    The DataView view provides a low-level interface for reading such data from and writing it to an ArrayBuffer.

    Constructors

    DataView DataView(ArrayBuffer buffer, optional unsigned long byteOffset, optional unsigned long byteLength);

    Method overview

    Read

    byte getInt8(unsigned long byteOffset);
    unsigned byte getUint8(unsigned long byteOffset);
    short getInt16(unsigned long byteOffset, optional boolean littleEndian);
    unsigned short getUint16(unsigned long byteOffset, optional boolean littleEndian);
    long getInt32(unsigned long byteOffset, optional boolean littleEndian);
    unsigned long getUint32(unsigned long byteOffset, optional boolean littleEndian);
    float getFloat32(unsigned long byteOffset, optional boolean littleEndian);
    double getFloat64(unsigned long byteOffset, optional boolean littleEndian);

    Write

    void setInt8(unsigned long byteOffset, byte value);
    void setUint8(unsigned long byteOffset, unsigned byte value);
    void setInt16(unsigned long byteOffset, short value, optional boolean littleEndian);
    void setUint16(unsigned long byteOffset, unsigned short value, optional boolean littleEndian);
    void setInt32(unsigned long byteOffset, long value, optional boolean littleEndian);
    void setUint32(unsigned long byteOffset, unsigned long value, optional boolean littleEndian);
    void setFloat32(unsigned long byteOffset, float value, optional boolean littleEndian);
    void setFloat64(unsigned long byteOffset, double value, optional boolean littleEndian);

    Constructor

    Returns a new DataView object using the passed ArrayBuffer for its storage.

    DataView DataView(
      ArrayBuffer buffer,
      optional unsigned long byteOffset,
      optional unsigned long byteLength
    );
    
    Parameters
    buffer
    An existing ArrayBuffer to use as the storage for the new DataView object.
    byteOffset Optional
    The offset, in bytes, to the first byte in the specified buffer for the new view to reference. If not specified, the view of the buffer will start with the first byte.
    byteLength Optional
    The number of elements in the byte array. If unspecified, length of the view will match the buffer's length.
    Return value

    A new DataView object representing the specified data buffer.

    Exceptions thrown
    INDEX_SIZE_ERR
    The byteOffset and byteLength result in the specified view extending past the end of the buffer.

    Methods

    getInt8()

    Gets a signed 8-bit integer at the specified byte offset from the start of the view.

    byte getInt8(
      unsigned long byteOffset
    );
    
    Parameters
    offset
    The offset, in byte, from the start of the view where to read the data.
    Exceptions thrown
    INDEX_SIZE_ERR
    The byteOffset is set such as it would read beyond the end of the view

    getUint8()

    Gets an unsigned 8-bit integer at the specified byte offset from the start of the view.

    byte getUint8(
      unsigned long byteOffset
    );
    
    Parameters
    offset
    The offset, in byte, from the start of the view where to read the data.
    Exceptions thrown
    INDEX_SIZE_ERR
    The byteOffset is set such as it would read beyond the end of the view

    getInt16()

    Gets a signed 16-bit integer at the specified byte offset from the start of the view.

    short getInt16(
      unsigned long byteOffset,
      optional boolean littleEndian
    );
    
    Parameters
    byteOffset
    The offset, in byte, from the start of the view where to read the data.
    littleEndian
    Indicates whether the 16-bit int is stored in little- or big-endian format. If false or undefined, a big-endian value is read.
    Exceptions thrown
    INDEX_SIZE_ERR
    The byteOffset is set such as it would read beyond the end of the view

    getUint16()

    Gets an unsigned 16-bit integer at the specified byte offset from the start of the view.

    unsigned short getUint16(
      unsigned long byteOffset,
      optional boolean littleEndian
    );
    
    Parameters
    byteOffset
    The offset, in byte, from the start of the view where to read the data.
    littleEndian
    Indicates whether the 16-bit int is stored in little- or big-endian format. If false or undefined, a big-endian value is read.
    Exceptions thrown
    INDEX_SIZE_ERR
    The byteOffset is set such as it would read beyond the end of the view

    Browser compatibility

    Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
    Basic support 9 15.0 (15.0) 10 12.1 5.1
    Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
    Basic support 4.0 15.0 (15.0) ? 12.0 4.2

    Detect endianness

    You'll probably need to detect the type of architecture your script is running, here is a little trick to check it :

    var littleEndian = (function() {
      var buffer = new ArrayBuffer(2);
      new DataView(buffer).setInt16(0, 256, true);
      return new Int16Array(buffer)[0] === 256;
    })();
    console.log(littleEndian); // true or false
    

    See also

    Document Tags and Contributors

    Contributors to this page: Sheppy
    Last updated by: Sheppy,