ArrayReader.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. "use strict";
  2. var DataReader = require("./DataReader");
  3. var utils = require("../utils");
  4. function ArrayReader(data) {
  5. DataReader.call(this, data);
  6. for(var i = 0; i < this.data.length; i++) {
  7. data[i] = data[i] & 0xFF;
  8. }
  9. }
  10. utils.inherits(ArrayReader, DataReader);
  11. /**
  12. * @see DataReader.byteAt
  13. */
  14. ArrayReader.prototype.byteAt = function(i) {
  15. return this.data[this.zero + i];
  16. };
  17. /**
  18. * @see DataReader.lastIndexOfSignature
  19. */
  20. ArrayReader.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 - this.zero;
  28. }
  29. }
  30. return -1;
  31. };
  32. /**
  33. * @see DataReader.readAndCheckSignature
  34. */
  35. ArrayReader.prototype.readAndCheckSignature = function (sig) {
  36. var sig0 = sig.charCodeAt(0),
  37. sig1 = sig.charCodeAt(1),
  38. sig2 = sig.charCodeAt(2),
  39. sig3 = sig.charCodeAt(3),
  40. data = this.readData(4);
  41. return sig0 === data[0] && sig1 === data[1] && sig2 === data[2] && sig3 === data[3];
  42. };
  43. /**
  44. * @see DataReader.readData
  45. */
  46. ArrayReader.prototype.readData = function(size) {
  47. this.checkOffset(size);
  48. if(size === 0) {
  49. return [];
  50. }
  51. var result = this.data.slice(this.zero + this.index, this.zero + this.index + size);
  52. this.index += size;
  53. return result;
  54. };
  55. module.exports = ArrayReader;