From d36d3ab8930bf3ad1d9f3b7f53f7626f1364b2c7 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Thu, 4 Jun 2026 13:10:24 +0200 Subject: [PATCH] Shorten the `getBytes` method in the `Stream`/`ChunkedStream` classes This is very old code and there's currently a bit of unneeded duplication in these methods, especially in the `ChunkedStream` class. --- src/core/chunked_stream.js | 24 +++++------------------- src/core/stream.js | 15 +++------------ 2 files changed, 8 insertions(+), 31 deletions(-) diff --git a/src/core/chunked_stream.js b/src/core/chunked_stream.js index 5fd65d054..277e51ce0 100644 --- a/src/core/chunked_stream.js +++ b/src/core/chunked_stream.js @@ -182,28 +182,14 @@ class ChunkedStream extends Stream { } getBytes(length) { - const bytes = this.bytes; const pos = this.pos; - const strEnd = this.end; + const endPos = !length ? this.end : Math.min(pos + length, this.end); - if (!length) { - if (strEnd > this.progressiveDataLength) { - this.ensureRange(pos, strEnd); - } - this.pos = strEnd; - return bytes.subarray(pos, strEnd); + if (endPos > this.progressiveDataLength) { + this.ensureRange(pos, endPos); } - - let end = pos + length; - if (end > strEnd) { - end = strEnd; - } - if (end > this.progressiveDataLength) { - this.ensureRange(pos, end); - } - - this.pos = end; - return bytes.subarray(pos, end); + this.pos = endPos; + return this.bytes.subarray(pos, endPos); } getByteRange(begin, end) { diff --git a/src/core/stream.js b/src/core/stream.js index ddb269772..9b9b43055 100644 --- a/src/core/stream.js +++ b/src/core/stream.js @@ -46,20 +46,11 @@ class Stream extends BaseStream { } getBytes(length) { - const bytes = this.bytes; const pos = this.pos; - const strEnd = this.end; + const endPos = !length ? this.end : Math.min(pos + length, this.end); - if (!length) { - this.pos = strEnd; - return bytes.subarray(pos, strEnd); - } - let end = pos + length; - if (end > strEnd) { - end = strEnd; - } - this.pos = end; - return bytes.subarray(pos, end); + this.pos = endPos; + return this.bytes.subarray(pos, endPos); } getByteRange(begin, end) {