decode_stream.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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.StreamsSequenceStream = exports.DecodeStream = void 0;
  27. var _base_stream = require("./base_stream.js");
  28. var _stream = require("./stream.js");
  29. const emptyBuffer = new Uint8Array(0);
  30. class DecodeStream extends _base_stream.BaseStream {
  31. constructor(maybeMinBufferLength) {
  32. super();
  33. this._rawMinBufferLength = maybeMinBufferLength || 0;
  34. this.pos = 0;
  35. this.bufferLength = 0;
  36. this.eof = false;
  37. this.buffer = emptyBuffer;
  38. this.minBufferLength = 512;
  39. if (maybeMinBufferLength) {
  40. while (this.minBufferLength < maybeMinBufferLength) {
  41. this.minBufferLength *= 2;
  42. }
  43. }
  44. }
  45. get isEmpty() {
  46. while (!this.eof && this.bufferLength === 0) {
  47. this.readBlock();
  48. }
  49. return this.bufferLength === 0;
  50. }
  51. ensureBuffer(requested) {
  52. const buffer = this.buffer;
  53. if (requested <= buffer.byteLength) {
  54. return buffer;
  55. }
  56. let size = this.minBufferLength;
  57. while (size < requested) {
  58. size *= 2;
  59. }
  60. const buffer2 = new Uint8Array(size);
  61. buffer2.set(buffer);
  62. return this.buffer = buffer2;
  63. }
  64. getByte() {
  65. const pos = this.pos;
  66. while (this.bufferLength <= pos) {
  67. if (this.eof) {
  68. return -1;
  69. }
  70. this.readBlock();
  71. }
  72. return this.buffer[this.pos++];
  73. }
  74. getBytes(length) {
  75. const pos = this.pos;
  76. let end;
  77. if (length) {
  78. this.ensureBuffer(pos + length);
  79. end = pos + length;
  80. while (!this.eof && this.bufferLength < end) {
  81. this.readBlock();
  82. }
  83. const bufEnd = this.bufferLength;
  84. if (end > bufEnd) {
  85. end = bufEnd;
  86. }
  87. } else {
  88. while (!this.eof) {
  89. this.readBlock();
  90. }
  91. end = this.bufferLength;
  92. }
  93. this.pos = end;
  94. return this.buffer.subarray(pos, end);
  95. }
  96. reset() {
  97. this.pos = 0;
  98. }
  99. makeSubStream(start, length, dict = null) {
  100. if (length === undefined) {
  101. while (!this.eof) {
  102. this.readBlock();
  103. }
  104. } else {
  105. const end = start + length;
  106. while (this.bufferLength <= end && !this.eof) {
  107. this.readBlock();
  108. }
  109. }
  110. return new _stream.Stream(this.buffer, start, length, dict);
  111. }
  112. getBaseStreams() {
  113. return this.str ? this.str.getBaseStreams() : null;
  114. }
  115. }
  116. exports.DecodeStream = DecodeStream;
  117. class StreamsSequenceStream extends DecodeStream {
  118. constructor(streams, onError = null) {
  119. let maybeLength = 0;
  120. for (const stream of streams) {
  121. maybeLength += stream instanceof DecodeStream ? stream._rawMinBufferLength : stream.length;
  122. }
  123. super(maybeLength);
  124. this.streams = streams;
  125. this._onError = onError;
  126. }
  127. readBlock() {
  128. const streams = this.streams;
  129. if (streams.length === 0) {
  130. this.eof = true;
  131. return;
  132. }
  133. const stream = streams.shift();
  134. let chunk;
  135. try {
  136. chunk = stream.getBytes();
  137. } catch (reason) {
  138. if (this._onError) {
  139. this._onError(reason, stream.dict && stream.dict.objId);
  140. return;
  141. }
  142. throw reason;
  143. }
  144. const bufferLength = this.bufferLength;
  145. const newLength = bufferLength + chunk.length;
  146. const buffer = this.ensureBuffer(newLength);
  147. buffer.set(chunk, bufferLength);
  148. this.bufferLength = newLength;
  149. }
  150. getBaseStreams() {
  151. const baseStreamsBuf = [];
  152. for (const stream of this.streams) {
  153. const baseStreams = stream.getBaseStreams();
  154. if (baseStreams) {
  155. baseStreamsBuf.push(...baseStreams);
  156. }
  157. }
  158. return baseStreamsBuf.length > 0 ? baseStreamsBuf : null;
  159. }
  160. }
  161. exports.StreamsSequenceStream = StreamsSequenceStream;