Stream.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var terminology_1 = require("../encoding/terminology");
  4. /**
  5. * A stream represents an ordered sequence of tokens.
  6. */
  7. var Stream = /** @class */ (function () {
  8. /**
  9. *
  10. * @constructor
  11. * @param {!(Array.<number>|Uint8Array)} tokens Array of tokens that provide
  12. * the stream.
  13. */
  14. function Stream(tokens) {
  15. /** @type {!Array.<number>} */
  16. this.tokens = Array.from(tokens);
  17. // Reversed as push/pop is more efficient than shift/unshift.
  18. this.tokens.reverse();
  19. }
  20. /**
  21. * @return {boolean} True if end-of-stream has been hit.
  22. */
  23. Stream.prototype.endOfStream = function () {
  24. return !this.tokens.length;
  25. };
  26. /**
  27. * When a token is read from a stream, the first token in the
  28. * stream must be returned and subsequently removed, and
  29. * end-of-stream must be returned otherwise.
  30. *
  31. * @return {number} Get the next token from the stream, or
  32. * end_of_stream.
  33. */
  34. Stream.prototype.read = function () {
  35. if (!this.tokens.length)
  36. return terminology_1.end_of_stream;
  37. return this.tokens.pop();
  38. };
  39. /**
  40. * When one or more tokens are prepended to a stream, those tokens
  41. * must be inserted, in given order, before the first token in the
  42. * stream.
  43. *
  44. * @param {(number|!Array.<number>)} token The token(s) to prepend to the
  45. * stream.
  46. */
  47. Stream.prototype.prepend = function (token) {
  48. if (Array.isArray(token)) {
  49. var tokens = (token);
  50. while (tokens.length)
  51. this.tokens.push(tokens.pop());
  52. }
  53. else {
  54. this.tokens.push(token);
  55. }
  56. };
  57. /**
  58. * When one or more tokens are pushed to a stream, those tokens
  59. * must be inserted, in given order, after the last token in the
  60. * stream.
  61. *
  62. * @param {(number|!Array.<number>)} token The tokens(s) to push to the
  63. * stream.
  64. */
  65. Stream.prototype.push = function (token) {
  66. if (Array.isArray(token)) {
  67. var tokens = (token);
  68. while (tokens.length)
  69. this.tokens.unshift(tokens.shift());
  70. }
  71. else {
  72. this.tokens.unshift(token);
  73. }
  74. };
  75. return Stream;
  76. }());
  77. exports.Stream = Stream;
  78. //# sourceMappingURL=Stream.js.map