DataView

  • Revision slug: JavaScript_typed_arrays/DataView
  • Revision title: DataView
  • Revision id: 4530
  • Created:
  • Creator: vjeux
  • Is current revision? No
  • Comment 14 words added

Revision Content

{{ draft() }}

Note: DataView is not yet implemented in Gecko. It is implemented in Chrome 9.

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(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_inline() }}
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_inline() }}
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.
INDEX_SIZE_ERR
The byteOffset is set such as it would read beyond the end of the view

See also

Revision Source

<p>{{ draft() }}</p>
<div class="note"><strong>Note:</strong> <code>DataView</code> is not yet implemented in Gecko. It is implemented in Chrome 9.</div>
<p>An <code>ArrayBuffer</code> 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 <a href="/en/JavaScript_typed_arrays/ArrayBufferView" title="en/JavaScript_typed_arrays/ArrayBufferView">Typed Array Views</a> described earlier. In addition, the data will often be heterogeneous in nature and have a defined byte order.</p>
<p>The <code>DataView</code> view provides a low-level interface for reading such data from and writing it to an <code><a href="/en/JavaScript_typed_arrays/ArrayBuffer" title="en/JavaScript_typed_arrays/ArrayBuffer">ArrayBuffer</a></code>.</p>
<h2>Constructors</h2>
<table class="standard-table"> <tbody> <tr> <td><code>DataView(<a href="/en/JavaScript_typed_arrays/ArrayBuffer" title="en/JavaScript_typed_arrays/ArrayBuffer">ArrayBuffer</a> buffer, optional unsigned long byteOffset, optional unsigned long byteLength);</code></td> </tr> </tbody>
</table>
<h2 name="Method_overview">Method overview</h2>
<h3 name="Read">Read</h3>
<table class="standard-table"> <tbody> <tr> <td><code>byte <a href="#getInt8">getInt8</a>(unsigned long byteOffset);</code></td> </tr> <tr> <td><code>unsigned byte <a href="#getUint8">getUint8</a>(unsigned long byteOffset);</code></td> </tr> <tr> <td><code>short <a href="#getInt8">getInt16</a>(unsigned long byteOffset, optional boolean littleEndian);</code></td> </tr> <tr> <td><code>unsigned short <a href="#getUint16">getUint16</a>(unsigned long byteOffset, optional boolean littleEndian);</code></td> </tr> <tr> <td><code>long <a href="#getInt32">getInt32</a>(unsigned long byteOffset, optional boolean littleEndian);</code></td> </tr> <tr> <td><code>unsigned long <a href="#getUint32">getUint32</a>(unsigned long byteOffset, optional boolean littleEndian);</code></td> </tr> <tr> <td><code>float <a href="#getFloat32">getFloat32</a>(unsigned long byteOffset, optional boolean littleEndian);</code></td> </tr> <tr> <td><code>double <a href="#getFloat64">getFloat64</a>(unsigned long byteOffset, optional boolean littleEndian);</code></td> </tr> </tbody>
</table>
<h3 name="Write">Write</h3>
<table class="standard-table"> <tbody> <tr> <td><code>void <a href="#setInt8">setInt8</a>(unsigned long byteOffset, byte value);</code></td> </tr> <tr> <td><code>void <a href="#setUint8">setUint8</a>(unsigned long byteOffset, unsigned byte value);</code></td> </tr> <tr> <td><code>void <a href="#setInt16">setInt16</a>(unsigned long byteOffset, short value, optional boolean littleEndian);</code></td> </tr> <tr> <td><code>void <a href="#setUint16">setUint16</a>(unsigned long byteOffset, unsigned short value, optional boolean littleEndian);</code></td> </tr> <tr> <td><code>void <a href="#setInt32">setInt32</a>(unsigned long byteOffset, long value, optional boolean littleEndian);</code></td> </tr> <tr> <td><code>void <a href="#setUint32">setUint32</a>(unsigned long byteOffset, unsigned long value, optional boolean littleEndian);</code></td> </tr> <tr> <td><code>void <a href="#setFloat32">setFloat32</a>(unsigned long byteOffset, float value, optional boolean littleEndian);</code></td> </tr> <tr> <td><code>void <a href="#setFloat64">setFloat64</a>(unsigned long byteOffset, double value, optional boolean littleEndian);</code></td> </tr> </tbody>
</table>
<h2>Constructor</h2>
<p>Returns a new <code>DataView</code> object using the passed <a href="/en/JavaScript_typed_arrays/ArrayBuffer" title="en/JavaScript_typed_arrays/ArrayBuffer">ArrayBuffer</a> for its storage.</p>
<pre>DataView DataView(
  ArrayBuffer buffer,
  optional unsigned long byteOffset,
  optional unsigned long byteLength
);
</pre>
<h6>Parameters</h6>
<dl> <dt><code>buffer</code></dt> <dd>An existing <a href="/en/JavaScript_typed_arrays/ArrayBuffer" title="en/JavaScript_typed_arrays/ArrayBuffer"><code>ArrayBuffer</code></a> to use as the storage for the new <code>DataView</code> object.</dd> <dt><code>byteOffset</code> {{ optional_inline() }}</dt> <dd>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.</dd> <dt><code>byteLength</code> {{ optional_inline() }}</dt> <dd>The number of elements in the byte array. If unspecified, length of the view will match the buffer's length.</dd>
</dl>
<h6>Return value</h6>
<p>A new <code>DataView</code> object representing the specified data buffer.</p>
<h6>Exceptions thrown</h6>
<dl> <dt><code>INDEX_SIZE_ERR</code></dt> <dd>The <code>byteOffset</code> and <code>byteLength</code> result in the specified view extending past the end of the buffer.</dd>
</dl>
<h2>Methods</h2>
<h3 id="getInt8">getInt8()</h3>
<p>Gets a signed 8-bit integer at the specified byte offset from the start of the view.</p>
<pre>byte getInt8(
  unsigned long byteOffset
);
</pre>
<h6>Parameters</h6>
<dl> <dt><code>offset</code></dt> <dd>The offset, in byte, from the start of the view where to read the data.</dd>
</dl>
<h6>Exceptions thrown</h6>
<dl> <dt><code>INDEX_SIZE_ERR</code></dt> <dd>The <code>byteOffset</code> is set such as it would read beyond the end of the view</dd>
</dl>
<h3 id="getUint8">getUint8()</h3>
<p>Gets an unsigned 8-bit integer at the specified byte offset from the start of the view.</p>
<pre>byte getUint8(
  unsigned long byteOffset
);
</pre>
<h6>Parameters</h6>
<dl> <dt><code>offset</code></dt> <dd>The offset, in byte, from the start of the view where to read the data.</dd>
</dl>
<dl> <dt><code>INDEX_SIZE_ERR</code></dt> <dd>The <code>byteOffset</code> is set such as it would read beyond the end of the view</dd>
</dl>
<h2>See also</h2>
<ul> <li><a class=" external" href="http://www.khronos.org/registry/typedarray/specs/latest/#8">DataView Specification</a></li> <li><a class=" external" href="http://www.khronos.org/registry/typedarray/specs/latest/">Typed Array Specification</a></li> <li><a href="/en/JavaScript_typed_arrays" title="en/JavaScript typed arrays">JavaScript typed arrays</a></li> <li><a class=" link-https" href="https://github.com/vjeux/jsDataView">jDataView</a>: a Javascript library that provides the DataView API to all browsers.</li>
</ul>
Revert to this revision