dom-bufferstream.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*jslint browser: true, devel: true, bitwise: false, debug: true, eqeq: false, es5: true, evil: false, forin: false, newcap: false, nomen: true, plusplus: true, regexp: false, unparam: false, sloppy: true, stupid: false, sub: false, todo: true, vars: true, white: true */
  2. function DOMBufferStream(arrayBuffer, offset, length, bigEndian, global, parentOffset) {
  3. this.global = global;
  4. offset = offset || 0;
  5. length = length || (arrayBuffer.byteLength - offset);
  6. this.arrayBuffer = arrayBuffer.slice(offset, offset + length);
  7. this.view = new global.DataView(this.arrayBuffer, 0, this.arrayBuffer.byteLength);
  8. this.setBigEndian(bigEndian);
  9. this.offset = 0;
  10. this.parentOffset = (parentOffset || 0) + offset;
  11. }
  12. DOMBufferStream.prototype = {
  13. setBigEndian: function(bigEndian) {
  14. this.littleEndian = !bigEndian;
  15. },
  16. nextUInt8: function() {
  17. var value = this.view.getUint8(this.offset);
  18. this.offset += 1;
  19. return value;
  20. },
  21. nextInt8: function() {
  22. var value = this.view.getInt8(this.offset);
  23. this.offset += 1;
  24. return value;
  25. },
  26. nextUInt16: function() {
  27. var value = this.view.getUint16(this.offset, this.littleEndian);
  28. this.offset += 2;
  29. return value;
  30. },
  31. nextUInt32: function() {
  32. var value = this.view.getUint32(this.offset, this.littleEndian);
  33. this.offset += 4;
  34. return value;
  35. },
  36. nextInt16: function() {
  37. var value = this.view.getInt16(this.offset, this.littleEndian);
  38. this.offset += 2;
  39. return value;
  40. },
  41. nextInt32: function() {
  42. var value = this.view.getInt32(this.offset, this.littleEndian);
  43. this.offset += 4;
  44. return value;
  45. },
  46. nextFloat: function() {
  47. var value = this.view.getFloat32(this.offset, this.littleEndian);
  48. this.offset += 4;
  49. return value;
  50. },
  51. nextDouble: function() {
  52. var value = this.view.getFloat64(this.offset, this.littleEndian);
  53. this.offset += 8;
  54. return value;
  55. },
  56. nextBuffer: function(length) {
  57. //this won't work in IE10
  58. var value = this.arrayBuffer.slice(this.offset, this.offset + length);
  59. this.offset += length;
  60. return value;
  61. },
  62. remainingLength: function() {
  63. return this.arrayBuffer.byteLength - this.offset;
  64. },
  65. nextString: function(length) {
  66. var value = this.arrayBuffer.slice(this.offset, this.offset + length);
  67. value = String.fromCharCode.apply(null, new this.global.Uint8Array(value));
  68. this.offset += length;
  69. return value;
  70. },
  71. mark: function() {
  72. var self = this;
  73. return {
  74. openWithOffset: function(offset) {
  75. offset = (offset || 0) + this.offset;
  76. return new DOMBufferStream(self.arrayBuffer, offset, self.arrayBuffer.byteLength - offset, !self.littleEndian, self.global, self.parentOffset);
  77. },
  78. offset: this.offset,
  79. getParentOffset: function() {
  80. return self.parentOffset;
  81. }
  82. };
  83. },
  84. offsetFrom: function(marker) {
  85. return this.parentOffset + this.offset - (marker.offset + marker.getParentOffset());
  86. },
  87. skip: function(amount) {
  88. this.offset += amount;
  89. },
  90. branch: function(offset, length) {
  91. length = typeof length === 'number' ? length : this.arrayBuffer.byteLength - (this.offset + offset);
  92. return new DOMBufferStream(this.arrayBuffer, this.offset + offset, length, !this.littleEndian, this.global, this.parentOffset);
  93. }
  94. };
  95. module.exports = DOMBufferStream;