1
0
mirror of https://github.com/regular/unbzip2-stream.git synced 2025-06-02 12:00:57 +00:00

Allow multiple streams in input

bzip2 can handle inputs that consist of multiple concatenated bzip2 streams.
To support this functionality, the decoder needs to continue after seeing an
EOS marker because it may be followed by another bzip2 stream.
This commit is contained in:
Stefan Friesel
2018-09-15 21:28:39 +02:00
parent fb948b73c9
commit b0a8c32bba
3 changed files with 25 additions and 7 deletions

@ -29,8 +29,8 @@ function unbzip2Stream() {
streamCRC = bz2.decompress(bitReader, f, buf, bufsize, streamCRC);
if (streamCRC === null) {
push(null);
//console.error('done');
// reset for next bzip2 header
blockSize = 0;
return false;
}else{
//console.error('decompressed', chunk.length,'bytes');
@ -71,16 +71,20 @@ function unbzip2Stream() {
return bufferQueue.shift();
});
}
while (hasBytes - bitReader.bytesRead + 1 >= ((25000 + 100000 * blockSize) || 4)){
while (!broken && hasBytes - bitReader.bytesRead + 1 >= ((25000 + 100000 * blockSize) || 4)){
//console.error('decompressing with', hasBytes - bitReader.bytesRead + 1, 'bytes in buffer');
if (!done) done = !decompressAndQueue(this);
if (done) break;
decompressAndQueue(this);
}
},
function end(x) {
//console.error(x,'last compressing with', hasBytes, 'bytes in buffer');
if (!done) {
while(decompressAndQueue(this));
while (!broken && hasBytes > bitReader.bytesRead){
decompressAndQueue(this);
}
if (!broken) {
if (streamCRC !== null)
stream.emit('error', new Error("input stream ended prematurely"));
this.queue(null);
}
}
);

BIN
test/fixtures/concatenated.bz2 vendored Normal file

Binary file not shown.

@ -17,6 +17,20 @@ test('one chunk of compressed data piped into unbzip2-stream results in original
});
test('concatenated bz2 streams piped into unbzip2-stream results in original file content', function(t) {
t.plan(1);
var compressed = fs.readFileSync('test/fixtures/concatenated.bz2');
var unbz2 = unbzip2Stream();
unbz2.pipe( concat(function(data) {
var expected = "ab\n";
t.equal(data.toString('utf-8'), expected);
}));
unbz2.write(compressed);
unbz2.end();
});
test('should emit error when stream is broken', function(t) {
t.plan(1);
var compressed = fs.readFileSync('test/fixtures/broken');