stringReader.js 958 B

123456789101112131415161718192021222324252627282930313233343536
  1. 'use strict';
  2. var DataReader = require('./dataReader');
  3. var utils = require('./utils');
  4. function StringReader(data, optimizedBinaryString) {
  5. this.data = data;
  6. if (!optimizedBinaryString) {
  7. this.data = utils.string2binary(this.data);
  8. }
  9. this.length = this.data.length;
  10. this.index = 0;
  11. }
  12. StringReader.prototype = new DataReader();
  13. /**
  14. * @see DataReader.byteAt
  15. */
  16. StringReader.prototype.byteAt = function(i) {
  17. return this.data.charCodeAt(i);
  18. };
  19. /**
  20. * @see DataReader.lastIndexOfSignature
  21. */
  22. StringReader.prototype.lastIndexOfSignature = function(sig) {
  23. return this.data.lastIndexOf(sig);
  24. };
  25. /**
  26. * @see DataReader.readData
  27. */
  28. StringReader.prototype.readData = function(size) {
  29. this.checkOffset(size);
  30. // this will work because the constructor applied the "& 0xff" mask.
  31. var result = this.data.slice(this.index, this.index + size);
  32. this.index += size;
  33. return result;
  34. };
  35. module.exports = StringReader;