decode_stream.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2021 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, forceClamped = false) {
  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. const subarray = this.buffer.subarray(pos, end);
  95. return forceClamped && !(subarray instanceof Uint8ClampedArray) ? new Uint8ClampedArray(subarray) : subarray;
  96. }
  97. reset() {
  98. this.pos = 0;
  99. }
  100. makeSubStream(start, length, dict = null) {
  101. if (length === undefined) {
  102. while (!this.eof) {
  103. this.readBlock();
  104. }
  105. } else {
  106. const end = start + length;
  107. while (this.bufferLength <= end && !this.eof) {
  108. this.readBlock();
  109. }
  110. }
  111. return new _stream.Stream(this.buffer, start, length, dict);
  112. }
  113. getBaseStreams() {
  114. return this.str ? this.str.getBaseStreams() : null;
  115. }
  116. }
  117. exports.DecodeStream = DecodeStream;
  118. class StreamsSequenceStream extends DecodeStream {
  119. constructor(streams) {
  120. let maybeLength = 0;
  121. for (const stream of streams) {
  122. maybeLength += stream instanceof DecodeStream ? stream._rawMinBufferLength : stream.length;
  123. }
  124. super(maybeLength);
  125. this.streams = streams;
  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. const chunk = stream.getBytes();
  135. const bufferLength = this.bufferLength;
  136. const newLength = bufferLength + chunk.length;
  137. const buffer = this.ensureBuffer(newLength);
  138. buffer.set(chunk, bufferLength);
  139. this.bufferLength = newLength;
  140. }
  141. getBaseStreams() {
  142. const baseStreamsBuf = [];
  143. for (const stream of this.streams) {
  144. const baseStreams = stream.getBaseStreams();
  145. if (baseStreams) {
  146. baseStreamsBuf.push(...baseStreams);
  147. }
  148. }
  149. return baseStreamsBuf.length > 0 ? baseStreamsBuf : null;
  150. }
  151. }
  152. exports.StreamsSequenceStream = StreamsSequenceStream;