Convert src/core/decode_stream.js
to use standard classes
This commit is contained in:
parent
8ce2cae4a7
commit
b11f012e52
@ -16,16 +16,15 @@
|
|||||||
import { Stream } from "./stream.js";
|
import { Stream } from "./stream.js";
|
||||||
import { unreachable } from "../shared/util.js";
|
import { unreachable } from "../shared/util.js";
|
||||||
|
|
||||||
// super class for the decoding streams
|
// Lots of DecodeStreams are created whose buffers are never used. For these
|
||||||
const DecodeStream = (function DecodeStreamClosure() {
|
// we share a single empty buffer. This is (a) space-efficient and (b) avoids
|
||||||
// Lots of DecodeStreams are created whose buffers are never used. For these
|
// having special cases that would be required if we used |null| for an empty
|
||||||
// we share a single empty buffer. This is (a) space-efficient and (b) avoids
|
// buffer.
|
||||||
// having special cases that would be required if we used |null| for an empty
|
const emptyBuffer = new Uint8Array(0);
|
||||||
// buffer.
|
|
||||||
const emptyBuffer = new Uint8Array(0);
|
|
||||||
|
|
||||||
// eslint-disable-next-line no-shadow
|
// Super class for the decoding streams.
|
||||||
function DecodeStream(maybeMinBufferLength) {
|
class DecodeStream {
|
||||||
|
constructor(maybeMinBufferLength) {
|
||||||
this._rawMinBufferLength = maybeMinBufferLength || 0;
|
this._rawMinBufferLength = maybeMinBufferLength || 0;
|
||||||
|
|
||||||
this.pos = 0;
|
this.pos = 0;
|
||||||
@ -41,155 +40,156 @@ const DecodeStream = (function DecodeStreamClosure() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
DecodeStream.prototype = {
|
// eslint-disable-next-line getter-return
|
||||||
// eslint-disable-next-line getter-return
|
get length() {
|
||||||
get length() {
|
unreachable("Should not access DecodeStream.length");
|
||||||
unreachable("Should not access DecodeStream.length");
|
|
||||||
},
|
|
||||||
|
|
||||||
get isEmpty() {
|
|
||||||
while (!this.eof && this.bufferLength === 0) {
|
|
||||||
this.readBlock();
|
|
||||||
}
|
|
||||||
return this.bufferLength === 0;
|
|
||||||
},
|
|
||||||
ensureBuffer: function DecodeStream_ensureBuffer(requested) {
|
|
||||||
const buffer = this.buffer;
|
|
||||||
if (requested <= buffer.byteLength) {
|
|
||||||
return buffer;
|
|
||||||
}
|
|
||||||
let size = this.minBufferLength;
|
|
||||||
while (size < requested) {
|
|
||||||
size *= 2;
|
|
||||||
}
|
|
||||||
const buffer2 = new Uint8Array(size);
|
|
||||||
buffer2.set(buffer);
|
|
||||||
return (this.buffer = buffer2);
|
|
||||||
},
|
|
||||||
getByte: function DecodeStream_getByte() {
|
|
||||||
const pos = this.pos;
|
|
||||||
while (this.bufferLength <= pos) {
|
|
||||||
if (this.eof) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
this.readBlock();
|
|
||||||
}
|
|
||||||
return this.buffer[this.pos++];
|
|
||||||
},
|
|
||||||
getUint16: function DecodeStream_getUint16() {
|
|
||||||
const b0 = this.getByte();
|
|
||||||
const b1 = this.getByte();
|
|
||||||
if (b0 === -1 || b1 === -1) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
return (b0 << 8) + b1;
|
|
||||||
},
|
|
||||||
getInt32: function DecodeStream_getInt32() {
|
|
||||||
const b0 = this.getByte();
|
|
||||||
const b1 = this.getByte();
|
|
||||||
const b2 = this.getByte();
|
|
||||||
const b3 = this.getByte();
|
|
||||||
return (b0 << 24) + (b1 << 16) + (b2 << 8) + b3;
|
|
||||||
},
|
|
||||||
getBytes(length, forceClamped = false) {
|
|
||||||
const pos = this.pos;
|
|
||||||
let end;
|
|
||||||
|
|
||||||
if (length) {
|
|
||||||
this.ensureBuffer(pos + length);
|
|
||||||
end = pos + length;
|
|
||||||
|
|
||||||
while (!this.eof && this.bufferLength < end) {
|
|
||||||
this.readBlock();
|
|
||||||
}
|
|
||||||
const bufEnd = this.bufferLength;
|
|
||||||
if (end > bufEnd) {
|
|
||||||
end = bufEnd;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
while (!this.eof) {
|
|
||||||
this.readBlock();
|
|
||||||
}
|
|
||||||
end = this.bufferLength;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.pos = end;
|
|
||||||
const subarray = this.buffer.subarray(pos, end);
|
|
||||||
// `this.buffer` is either a `Uint8Array` or `Uint8ClampedArray` here.
|
|
||||||
return forceClamped && !(subarray instanceof Uint8ClampedArray)
|
|
||||||
? new Uint8ClampedArray(subarray)
|
|
||||||
: subarray;
|
|
||||||
},
|
|
||||||
peekByte: function DecodeStream_peekByte() {
|
|
||||||
const peekedByte = this.getByte();
|
|
||||||
if (peekedByte !== -1) {
|
|
||||||
this.pos--;
|
|
||||||
}
|
|
||||||
return peekedByte;
|
|
||||||
},
|
|
||||||
peekBytes(length, forceClamped = false) {
|
|
||||||
const bytes = this.getBytes(length, forceClamped);
|
|
||||||
this.pos -= bytes.length;
|
|
||||||
return bytes;
|
|
||||||
},
|
|
||||||
makeSubStream: function DecodeStream_makeSubStream(start, length, dict) {
|
|
||||||
if (length === undefined) {
|
|
||||||
while (!this.eof) {
|
|
||||||
this.readBlock();
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
const end = start + length;
|
|
||||||
while (this.bufferLength <= end && !this.eof) {
|
|
||||||
this.readBlock();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return new Stream(this.buffer, start, length, dict);
|
|
||||||
},
|
|
||||||
|
|
||||||
getByteRange(begin, end) {
|
|
||||||
unreachable("Should not call DecodeStream.getByteRange");
|
|
||||||
},
|
|
||||||
|
|
||||||
skip: function DecodeStream_skip(n) {
|
|
||||||
if (!n) {
|
|
||||||
n = 1;
|
|
||||||
}
|
|
||||||
this.pos += n;
|
|
||||||
},
|
|
||||||
reset: function DecodeStream_reset() {
|
|
||||||
this.pos = 0;
|
|
||||||
},
|
|
||||||
getBaseStreams: function DecodeStream_getBaseStreams() {
|
|
||||||
if (this.str && this.str.getBaseStreams) {
|
|
||||||
return this.str.getBaseStreams();
|
|
||||||
}
|
|
||||||
return [];
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
return DecodeStream;
|
|
||||||
})();
|
|
||||||
|
|
||||||
const StreamsSequenceStream = (function StreamsSequenceStreamClosure() {
|
|
||||||
// eslint-disable-next-line no-shadow
|
|
||||||
function StreamsSequenceStream(streams) {
|
|
||||||
this.streams = streams;
|
|
||||||
|
|
||||||
let maybeLength = 0;
|
|
||||||
for (let i = 0, ii = streams.length; i < ii; i++) {
|
|
||||||
const stream = streams[i];
|
|
||||||
if (stream instanceof DecodeStream) {
|
|
||||||
maybeLength += stream._rawMinBufferLength;
|
|
||||||
} else {
|
|
||||||
maybeLength += stream.length;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
DecodeStream.call(this, maybeLength);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
StreamsSequenceStream.prototype = Object.create(DecodeStream.prototype);
|
get isEmpty() {
|
||||||
|
while (!this.eof && this.bufferLength === 0) {
|
||||||
|
this.readBlock();
|
||||||
|
}
|
||||||
|
return this.bufferLength === 0;
|
||||||
|
}
|
||||||
|
|
||||||
StreamsSequenceStream.prototype.readBlock = function streamSequenceStreamReadBlock() {
|
ensureBuffer(requested) {
|
||||||
|
const buffer = this.buffer;
|
||||||
|
if (requested <= buffer.byteLength) {
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
let size = this.minBufferLength;
|
||||||
|
while (size < requested) {
|
||||||
|
size *= 2;
|
||||||
|
}
|
||||||
|
const buffer2 = new Uint8Array(size);
|
||||||
|
buffer2.set(buffer);
|
||||||
|
return (this.buffer = buffer2);
|
||||||
|
}
|
||||||
|
|
||||||
|
getByte() {
|
||||||
|
const pos = this.pos;
|
||||||
|
while (this.bufferLength <= pos) {
|
||||||
|
if (this.eof) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
this.readBlock();
|
||||||
|
}
|
||||||
|
return this.buffer[this.pos++];
|
||||||
|
}
|
||||||
|
|
||||||
|
getUint16() {
|
||||||
|
const b0 = this.getByte();
|
||||||
|
const b1 = this.getByte();
|
||||||
|
if (b0 === -1 || b1 === -1) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
return (b0 << 8) + b1;
|
||||||
|
}
|
||||||
|
|
||||||
|
getInt32() {
|
||||||
|
const b0 = this.getByte();
|
||||||
|
const b1 = this.getByte();
|
||||||
|
const b2 = this.getByte();
|
||||||
|
const b3 = this.getByte();
|
||||||
|
return (b0 << 24) + (b1 << 16) + (b2 << 8) + b3;
|
||||||
|
}
|
||||||
|
|
||||||
|
getBytes(length, forceClamped = false) {
|
||||||
|
const pos = this.pos;
|
||||||
|
let end;
|
||||||
|
|
||||||
|
if (length) {
|
||||||
|
this.ensureBuffer(pos + length);
|
||||||
|
end = pos + length;
|
||||||
|
|
||||||
|
while (!this.eof && this.bufferLength < end) {
|
||||||
|
this.readBlock();
|
||||||
|
}
|
||||||
|
const bufEnd = this.bufferLength;
|
||||||
|
if (end > bufEnd) {
|
||||||
|
end = bufEnd;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
while (!this.eof) {
|
||||||
|
this.readBlock();
|
||||||
|
}
|
||||||
|
end = this.bufferLength;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.pos = end;
|
||||||
|
const subarray = this.buffer.subarray(pos, end);
|
||||||
|
// `this.buffer` is either a `Uint8Array` or `Uint8ClampedArray` here.
|
||||||
|
return forceClamped && !(subarray instanceof Uint8ClampedArray)
|
||||||
|
? new Uint8ClampedArray(subarray)
|
||||||
|
: subarray;
|
||||||
|
}
|
||||||
|
|
||||||
|
peekByte() {
|
||||||
|
const peekedByte = this.getByte();
|
||||||
|
if (peekedByte !== -1) {
|
||||||
|
this.pos--;
|
||||||
|
}
|
||||||
|
return peekedByte;
|
||||||
|
}
|
||||||
|
|
||||||
|
peekBytes(length, forceClamped = false) {
|
||||||
|
const bytes = this.getBytes(length, forceClamped);
|
||||||
|
this.pos -= bytes.length;
|
||||||
|
return bytes;
|
||||||
|
}
|
||||||
|
|
||||||
|
makeSubStream(start, length, dict = null) {
|
||||||
|
if (length === undefined) {
|
||||||
|
while (!this.eof) {
|
||||||
|
this.readBlock();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
const end = start + length;
|
||||||
|
while (this.bufferLength <= end && !this.eof) {
|
||||||
|
this.readBlock();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new Stream(this.buffer, start, length, dict);
|
||||||
|
}
|
||||||
|
|
||||||
|
getByteRange(begin, end) {
|
||||||
|
unreachable("Should not call DecodeStream.getByteRange");
|
||||||
|
}
|
||||||
|
|
||||||
|
skip(n) {
|
||||||
|
if (!n) {
|
||||||
|
n = 1;
|
||||||
|
}
|
||||||
|
this.pos += n;
|
||||||
|
}
|
||||||
|
|
||||||
|
reset() {
|
||||||
|
this.pos = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
getBaseStreams() {
|
||||||
|
if (this.str && this.str.getBaseStreams) {
|
||||||
|
return this.str.getBaseStreams();
|
||||||
|
}
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class StreamsSequenceStream extends DecodeStream {
|
||||||
|
constructor(streams) {
|
||||||
|
let maybeLength = 0;
|
||||||
|
for (const stream of streams) {
|
||||||
|
maybeLength +=
|
||||||
|
stream instanceof DecodeStream
|
||||||
|
? stream._rawMinBufferLength
|
||||||
|
: stream.length;
|
||||||
|
}
|
||||||
|
super(maybeLength);
|
||||||
|
|
||||||
|
this.streams = streams;
|
||||||
|
}
|
||||||
|
|
||||||
|
readBlock() {
|
||||||
const streams = this.streams;
|
const streams = this.streams;
|
||||||
if (streams.length === 0) {
|
if (streams.length === 0) {
|
||||||
this.eof = true;
|
this.eof = true;
|
||||||
@ -202,20 +202,17 @@ const StreamsSequenceStream = (function StreamsSequenceStreamClosure() {
|
|||||||
const buffer = this.ensureBuffer(newLength);
|
const buffer = this.ensureBuffer(newLength);
|
||||||
buffer.set(chunk, bufferLength);
|
buffer.set(chunk, bufferLength);
|
||||||
this.bufferLength = newLength;
|
this.bufferLength = newLength;
|
||||||
};
|
}
|
||||||
|
|
||||||
StreamsSequenceStream.prototype.getBaseStreams = function StreamsSequenceStream_getBaseStreams() {
|
getBaseStreams() {
|
||||||
const baseStreams = [];
|
const baseStreams = [];
|
||||||
for (let i = 0, ii = this.streams.length; i < ii; i++) {
|
for (const stream of this.streams) {
|
||||||
const stream = this.streams[i];
|
|
||||||
if (stream.getBaseStreams) {
|
if (stream.getBaseStreams) {
|
||||||
baseStreams.push(...stream.getBaseStreams());
|
baseStreams.push(...stream.getBaseStreams());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return baseStreams;
|
return baseStreams;
|
||||||
};
|
}
|
||||||
|
}
|
||||||
return StreamsSequenceStream;
|
|
||||||
})();
|
|
||||||
|
|
||||||
export { DecodeStream, StreamsSequenceStream };
|
export { DecodeStream, StreamsSequenceStream };
|
||||||
|
Loading…
x
Reference in New Issue
Block a user