stream.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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, forceClamped = false) {
  51. const bytes = this.bytes;
  52. const pos = this.pos;
  53. const strEnd = this.end;
  54. if (!length) {
  55. const subarray = bytes.subarray(pos, strEnd);
  56. return forceClamped ? new Uint8ClampedArray(subarray) : subarray;
  57. }
  58. let end = pos + length;
  59. if (end > strEnd) {
  60. end = strEnd;
  61. }
  62. this.pos = end;
  63. const subarray = bytes.subarray(pos, end);
  64. return forceClamped ? new Uint8ClampedArray(subarray) : subarray;
  65. }
  66. getByteRange(begin, end) {
  67. if (begin < 0) {
  68. begin = 0;
  69. }
  70. if (end > this.end) {
  71. end = this.end;
  72. }
  73. return this.bytes.subarray(begin, end);
  74. }
  75. reset() {
  76. this.pos = this.start;
  77. }
  78. moveStart() {
  79. this.start = this.pos;
  80. }
  81. makeSubStream(start, length, dict = null) {
  82. return new Stream(this.bytes.buffer, start, length, dict);
  83. }
  84. }
  85. exports.Stream = Stream;
  86. class StringStream extends Stream {
  87. constructor(str) {
  88. super((0, _util.stringToBytes)(str));
  89. }
  90. }
  91. exports.StringStream = StringStream;
  92. class NullStream extends Stream {
  93. constructor() {
  94. super(new Uint8Array(0));
  95. }
  96. }
  97. exports.NullStream = NullStream;