uint8ArrayReader.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. 'use strict';
  2. var DataReader = require('./dataReader');
  3. function Uint8ArrayReader(data) {
  4. if (data) {
  5. this.data = data;
  6. this.length = this.data.length;
  7. this.index = 0;
  8. }
  9. }
  10. Uint8ArrayReader.prototype = new DataReader();
  11. /**
  12. * @see DataReader.byteAt
  13. */
  14. Uint8ArrayReader.prototype.byteAt = function(i) {
  15. return this.data[i];
  16. };
  17. /**
  18. * @see DataReader.lastIndexOfSignature
  19. */
  20. Uint8ArrayReader.prototype.lastIndexOfSignature = function(sig) {
  21. var sig0 = sig.charCodeAt(0),
  22. sig1 = sig.charCodeAt(1),
  23. sig2 = sig.charCodeAt(2),
  24. sig3 = sig.charCodeAt(3);
  25. for (var i = this.length - 4; i >= 0; --i) {
  26. if (this.data[i] === sig0 && this.data[i + 1] === sig1 && this.data[i + 2] === sig2 && this.data[i + 3] === sig3) {
  27. return i;
  28. }
  29. }
  30. return -1;
  31. };
  32. /**
  33. * @see DataReader.readData
  34. */
  35. Uint8ArrayReader.prototype.readData = function(size) {
  36. this.checkOffset(size);
  37. if(size === 0) {
  38. // in IE10, when using subarray(idx, idx), we get the array [0x00] instead of [].
  39. return new Uint8Array(0);
  40. }
  41. var result = this.data.subarray(this.index, this.index + size);
  42. this.index += size;
  43. return result;
  44. };
  45. module.exports = Uint8ArrayReader;