stream.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * JavaScript code in this page
  4. *
  5. * Copyright 2022 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * JavaScript code in this page
  21. */
  22. "use strict";
  23. Object.defineProperty(exports, "__esModule", {
  24. value: true
  25. });
  26. exports.StringStream = exports.Stream = exports.NullStream = void 0;
  27. var _base_stream = require("./base_stream.js");
  28. var _util = require("../shared/util.js");
  29. class Stream extends _base_stream.BaseStream {
  30. constructor(arrayBuffer, start, length, dict) {
  31. super();
  32. this.bytes = arrayBuffer instanceof Uint8Array ? arrayBuffer : new Uint8Array(arrayBuffer);
  33. this.start = start || 0;
  34. this.pos = this.start;
  35. this.end = start + length || this.bytes.length;
  36. this.dict = dict;
  37. }
  38. get length() {
  39. return this.end - this.start;
  40. }
  41. get isEmpty() {
  42. return this.length === 0;
  43. }
  44. getByte() {
  45. if (this.pos >= this.end) {
  46. return -1;
  47. }
  48. return this.bytes[this.pos++];
  49. }
  50. getBytes(length) {
  51. const bytes = this.bytes;
  52. const pos = this.pos;
  53. const strEnd = this.end;
  54. if (!length) {
  55. return bytes.subarray(pos, strEnd);
  56. }
  57. let end = pos + length;
  58. if (end > strEnd) {
  59. end = strEnd;
  60. }
  61. this.pos = end;
  62. return bytes.subarray(pos, end);
  63. }
  64. getByteRange(begin, end) {
  65. if (begin < 0) {
  66. begin = 0;
  67. }
  68. if (end > this.end) {
  69. end = this.end;
  70. }
  71. return this.bytes.subarray(begin, end);
  72. }
  73. reset() {
  74. this.pos = this.start;
  75. }
  76. moveStart() {
  77. this.start = this.pos;
  78. }
  79. makeSubStream(start, length, dict = null) {
  80. return new Stream(this.bytes.buffer, start, length, dict);
  81. }
  82. }
  83. exports.Stream = Stream;
  84. class StringStream extends Stream {
  85. constructor(str) {
  86. super((0, _util.stringToBytes)(str));
  87. }
  88. }
  89. exports.StringStream = StringStream;
  90. class NullStream extends Stream {
  91. constructor() {
  92. super(new Uint8Array(0));
  93. }
  94. }
  95. exports.NullStream = NullStream;