{"bugs":{"url":"https://github.com/vjeux/jDataView/issues"},"dist":{"shasum":"febdf9b60bf5697b12d890cbdd6f4ada984d362b","tarball":"https://registry.npmjs.org/jDataView/-/jDataView-1.1.0.tgz","integrity":"sha512-57nkWZD1J39GH2hIMHuv67tz9S2crRH5o1kFcurP8DgTv3WBbEOTnS6M0cRi19WguBwRnDHtmPgY0u30A7ZwUw==","signatures":[{"sig":"MEUCIQCn9R4xBHvK474IFc7DMS7JqEXWezUbkEdgTd4OBnJ+OgIgOdE0n3g8xyskUrWJXdudufqJQlFV48qsEZG5sJsvQ7E=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}]},"main":"src/jdataview.js","name":"jDataView","_from":"..\\jDataView","files":["src/jdataview.js"],"author":{"url":"http://blog.vjeux.com/","name":"Vjeux","email":"vjeuxx@gmail.com"},"readme":"<a href=\"http://blog.vjeux.com/2011/javascript/jdataview-read-binary-file.html\">jDataView</a> - A unique way to read a binary file in Javascript.\r\n================================\r\n\r\njDataView provides a standard way to read binary files in all the browsers. It follows the [DataView Specification](http://www.khronos.org/registry/webgl/doc/spec/TypedArray-spec.html#6) and even extends it for a more practical use.\r\n\r\nExplanation\r\n=========\r\n\r\nThere are three ways to read a binary file from the browser.\r\n\r\n* The first one is to download the file through XHR with [charset=x-user-defined](https://developer.mozilla.org/en/using_xmlhttprequest#Receiving_binary_data). You get the file as a **String**, and you have to rewrite all the decoding functions (getUint16, getFloat32, ...). All the browsers support this.\r\n\r\n* Then browsers that implemented WebGL also added **ArrayBuffers**. 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. It is supported by Firefox 4 and Chrome 7.\r\n\r\n* A new revision of the specification added **DataViews**. It is a view around your buffer that can read arbitrary data types directly through functions: getUint32, getFloat64 ... Only Chrome 9 supports it.\r\n\r\nAnd one way to read a binary file from the server.\r\n\r\n* **NodeJS Buffers**. They appeared in [Node 0.4.0](http://nodejs.org/docs/v0.4.0/api/buffers.html). [Node 0.5.0](http://nodejs.org/docs/v0.5.0/api/buffers.html) added a DataView-like API. And [Node 0.6.0](http://nodejs.org/docs/v0.6.0/api/buffers.html) changed the API naming convention.\r\n\r\njDataView provides the DataView API using the best available option between Strings, TypedArrays, NodeJS Buffers and DataViews.\r\n\r\nAPI\r\n===\r\nSee the [Typed Array Specification](http://www.khronos.org/registry/typedarray/specs/latest/#8) subsection�8 (�The�DataView View�Type�) for a detailed API.\r\n\r\nConstructor\r\n-----------------\r\n* new **jDataView**(buffer, offset, length, littleEndian=true)\r\n    * buffer can be either a String, an ArrayBuffer, or a Node.js Buffer\r\n    * littleEndian is a default value for the view\r\n\r\nSpecification API\r\n-------------------------\r\nThe wrapper satisfies all the specification getters.\r\n\r\n* **getInt8**(byteOffset)\r\n* **getUint8**(byteOffset)\r\n* **getInt16**(byteOffset, littleEndian)\r\n* **getUint16**(byteOffset, littleEndian)\r\n* **getInt32**(byteOffset, littleEndian)\r\n* **getUint32**(byteOffset, littleEndian)\r\n* **getFloat32**(byteOffset, littleEndian)\r\n* **getFloat64**(byteOffset, littleEndian)\r\n\r\n\r\nExtended Specification\r\n---------------------------------\r\nAddition of a littleEndian argument to the constructor. It will be the default value of the getters if their littleEndian value is undefined.\r\n\r\n* **jDataView**(buffer, offset, length, littleEndian=true)\r\n\r\nThe byteOffset parameter is now optional. If you omit it, it will read right after the latest read offset. You can interact with the internal pointer with those two functions.\r\n\r\n* **seek**(byteOffset)\r\n    * Moves the internal pointer to the position\r\n* **tell**()\r\n    * Returns the current position\r\n\r\nAddition of getChar and getString utilities.\r\n\r\n* **getChar**(byteOffset)\r\n* **getString**(length, byteOffset)\r\n\r\nAddition of createBuffer, a utility to easily create buffers with the latest available storage type (String or ArrayBuffer).\r\n\r\n* **createBuffer**(byte1, byte2, ...)\r\n\r\nShortcomings\r\n==========\r\n\r\n* Only the Read API is being wrapped, jDataView does not provide any `set` method.\r\n* I found that most files we want to read are in littleEndian due to x86 architecture. I changed the default behavior of getters to be littleEndian instead of bigEndian.\r\n\r\nExample\r\n======\r\nFirst we need a file. Either you get it through XHR or use the createBuffer utility.\r\n\r\n```javascript\r\nvar file = jDataView.createBuffer(\r\n\t0x10, 0x01, 0x00, 0x00, // Int32 - 272\r\n\t0x90, 0xcf, 0x1b, 0x47, // Float32 - 39887.5625\r\n\t0, 0, 0, 0, 0, 0, 0, 0, // 8 blank bytes\r\n\t0x4d, 0x44, 0x32, 0x30, // String - MD20\r\n\t0x61                    // Char - a\r\n);\r\n```\r\n\r\nNow we use the DataView as defined in the specification, the only thing that changes is the j before jDataView.\r\n\r\n```javascript\r\nvar view = new jDataView(file);\r\nvar version = view.getInt32(0); // 272\r\nvar float = view.getFloat32(4); // 39887.5625\r\n```\r\n\r\nThe wrapper extends the specification to make the DataView easier to use.\r\n\r\n```javascript\r\nvar view = new jDataView(file);\r\n// A position counter is managed. Remove the argument to read right after the last read.\r\nversion = view.getInt32(); // 272\r\nfloat = view.getFloat32(); // 39887.5625\r\n\r\n// You can move around with tell() and seek()\r\nview.seek(view.tell() + 8);\r\n\r\n// Two helpers: getChar and getString will make your life easier\r\nvar tag = view.getString(4); // MD20\r\nvar char = view.getChar(); // a\r\n```\r\n\r\nYou can use a <a href=\"http://blog.vjeux.com/2011/javascript/jquery-binary-ajax.html\">patched version of jQuery</a> that supports ArrayBuffer for AJAX.\r\n\r\n```javascript\r\n$.get(\r\n  'data.bin',\r\n  function (view) {\r\n    var tag = view.getString(4); // 'MD20'\r\n    var version = view.getUint32(); // 732\r\n  },\r\n  'dataview'\r\n);\r\n```\r\n\r\nChangelog\r\n========\r\n* **December 22 2011**: Added IE6-9 support by [scintill](https://github.com/scintill)\r\n* **November 30 2011**:\r\n  * Added NodeJS Buffer support + NPM Package.\r\n  * Added support for NaN and Infinity in the float shim.\r\n  * Added ```buffer```, ```byteLength``` and ```byteOffset``` attributes.\r\n  * Fixed bugs using non zero ```byteOffset` and added more bound checks.\r\n* **September 21 2011**: Added a missing ```littleEndian``` argument on getInt16.\r\n* **April 28 2011**: Seeking to the end of file no longer throws an error.\r\n* **April 26 2011**: Fixed a bug with extremely large unsigned 32bit being considered as signed. ([Solution](http://stackoverflow.com/questions/1240408/reading-bytes-from-a-javascript-string/2954435#2954435)). \r\n* **April 8 2011**: Added littleEndian argument on the constructor. Opera 11.50 does not fully implement DataView, improved check.\r\n\r\nDemos\r\n==== \r\n\r\n* A <a href=\"http://fooo.fr/~vjeux/github/jsDataView/demo/untar/untar.html\">simple tar viewer</a>. It is a \"Hello World\" demo of how easy it is to use the library.\r\n\r\n* A <a href=\"http://fooo.fr/~vjeux/github/jsWoWModelViewer/modelviewer.html\">World of Warcraft Model Viewer</a>. It uses jDataView to read the binary file and then WebGL to display it.\r\n<a href=\"http://fooo.fr/~vjeux/github/jsWoWModelViewer/modelviewer.html\"><img src=\"http://fooo.fr/~vjeux/github/jsWoWModelViewer/images/modelviewer.png\"></a>\r\n\r\n* A <a href=\"http://www.visual-experiments.com/2011/04/05/photosynth-webgl-viewer/\">PhotoSynth WebGL Viewer</a> by Visual Experiments. It uses jDataView to read the binary file and then WebGL to display it.\r\n<a href=\"http://www.visual-experiments.com/2011/04/05/photosynth-webgl-viewer/\"><img src=\"http://i.imgur.com/HRHXo.jpg\"/></a>\r\n\r\nPlease tell me if you made something with jDataView :)\r\n\r\nLicence: [Do What The Fuck You Want To Public License](http://sam.zoy.org/wtfpl/)\r\n","_npmUser":{"name":"rreverser","email":"me@rreverser.com"},"homepage":"http://blog.vjeux.com/2011/javascript/jdataview-read-binary-file.html","keywords":["buffer","binary","file","read"],"deprecated":"According to new npm naming restrictions, jDataView was renamed to jdataview in registry and this package is left only for backward compatibility.","repository":{"url":"git://github.com/vjeux/jDataView.git","type":"git"},"_npmVersion":"1.2.30","description":"A unique way to read a binary file in the browser and the server.","directories":{},"maintainers":[{"name":"rreverser","email":"me@rreverser.com"}],"readmeFilename":"README.md","_id":"jDataView@1.1.0","version":"1.1.0"}