stringReader.js 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  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. this.zero = 0;
  12. }
  13. StringReader.prototype = new DataReader();
  14. /**
  15. * @see DataReader.byteAt
  16. */
  17. StringReader.prototype.byteAt = function(i) {
  18. return this.data.charCodeAt(this.zero + i);
  19. };
  20. /**
  21. * @see DataReader.lastIndexOfSignature
  22. */
  23. StringReader.prototype.lastIndexOfSignature = function(sig) {
  24. return this.data.lastIndexOf(sig) - this.zero;
  25. };
  26. /**
  27. * @see DataReader.readData
  28. */
  29. StringReader.prototype.readData = function(size) {
  30. this.checkOffset(size);
  31. // this will work because the constructor applied the "& 0xff" mask.
  32. var result = this.data.slice(this.zero + this.index, this.zero + this.index + size);
  33. this.index += size;
  34. return result;
  35. };
  36. module.exports = StringReader;