arrayReader.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. 'use strict';
  2. var DataReader = require('./dataReader');
  3. function ArrayReader(data) {
  4. if (data) {
  5. this.data = data;
  6. this.length = this.data.length;
  7. this.index = 0;
  8. this.zero = 0;
  9. for(var i = 0; i < this.data.length; i++) {
  10. data[i] = data[i] & 0xFF;
  11. }
  12. }
  13. }
  14. ArrayReader.prototype = new DataReader();
  15. /**
  16. * @see DataReader.byteAt
  17. */
  18. ArrayReader.prototype.byteAt = function(i) {
  19. return this.data[this.zero + i];
  20. };
  21. /**
  22. * @see DataReader.lastIndexOfSignature
  23. */
  24. ArrayReader.prototype.lastIndexOfSignature = function(sig) {
  25. var sig0 = sig.charCodeAt(0),
  26. sig1 = sig.charCodeAt(1),
  27. sig2 = sig.charCodeAt(2),
  28. sig3 = sig.charCodeAt(3);
  29. for (var i = this.length - 4; i >= 0; --i) {
  30. if (this.data[i] === sig0 && this.data[i + 1] === sig1 && this.data[i + 2] === sig2 && this.data[i + 3] === sig3) {
  31. return i - this.zero;
  32. }
  33. }
  34. return -1;
  35. };
  36. /**
  37. * @see DataReader.readData
  38. */
  39. ArrayReader.prototype.readData = function(size) {
  40. this.checkOffset(size);
  41. if(size === 0) {
  42. return [];
  43. }
  44. var result = this.data.slice(this.zero + this.index, this.zero + this.index + size);
  45. this.index += size;
  46. return result;
  47. };
  48. module.exports = ArrayReader;