Skip to content
Zade Viggers edited this page Dec 10, 2023 · 1 revision

Note

This page has been updated for jDataView 3

Check out the original jDataView article by Vjeux, jDataView's original author.

Now that DataView is natively available in all JavaScript engines, jDataView 3 acts as a layer on top of the native DataView, with powerful methods for dealing with non-standard binary data types like strings and arbitrary-length integers, as well as ergonomic improvements such as sequential writes.

The following is an archive of jDataView's README Explanation section from 2011.

Explanation

There are three ways to read a binary file from the browser.

  • The first one is to download the file through XHR with charset=x-user-defined. You get the file as a String, convert it to byte Array and you have to rewrite all the decoding and encoding functions (getUint16, getFloat32, ...). All the browsers support this.

  • Then browsers that implemented Canvas also added CanvasPixelArray as part of ImageData. It is fast byte array that is created and used internally by <canvas /> element for manipulating low-level image data. We can create such host element and use it as factory for our own instances of this array.

  • Then browsers that implemented WebGL added ArrayBuffer. It is a plain buffer that can be read with views called TypedArrays (Int32Array, Float64Array, ...). You can use them to decode the file but this is not very handy. It has big drawback, it can't read non-aligned data (but we can actually hack that). So they replaced CanvasPixelArray with Uint8ClampedArray (same as Uint8Array, but cuts off numbers outside 0..255 range).

  • A new revision of the specification added DataViews. It is a view around your buffer that can read/write arbitrary data types directly through functions: getUint32, getFloat64 ...

And one way to read a binary file from the server.

jDataView provided a polyfill for the DataView API with it's own convenient extensions using the best available option between Arrays, TypedArrays, NodeJS Buffers and DataViews.

Clone this wiki locally