This repository has been archived by the owner on Sep 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
95 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
* LZ4 stream dictionary | ||
* NodeJS streams updates | ||
* NodeJS streams updates | ||
* dependent blocks compression |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,86 @@ | ||
var Transform = require('stream').Transform | ||
var inherits = require('util').inherits | ||
|
||
var encoder = require('./encoder') | ||
var LZ4Stream = require('./encoder').LZ4Stream | ||
var lz4_static = require('./static') | ||
|
||
var LZ4_compressChunk = encoder.LZ4_compressChunk | ||
var LZ4_compressHCChunk = encoder.LZ4_compressHCChunk | ||
var LZ4_compressBound = encoder.LZ4_compressBound | ||
|
||
/** | ||
Build up chunks and encode them one by one as they are assembled | ||
Build up blocks and encode them one by one as they are assembled | ||
*/ | ||
function Encoder (options) { | ||
if ( !(this instanceof Encoder) ) | ||
return new Encoder(options) | ||
|
||
Transform.call(this, options) | ||
// Options | ||
options = options || {} | ||
this.chunkSize = options.chunkSize || lz4_static.DEFAULT_CHUNKSIZE | ||
this.compress = options.hc ? LZ4_compressHCChunk : LZ4_compressChunk | ||
LZ4Stream.call(this, options) | ||
|
||
// Data being processed | ||
this.buffer = [] | ||
this.length = 0 | ||
|
||
this.first = true | ||
this.chunkBound = LZ4_compressBound(this.chunkSize) + 4 | ||
this.fast = !this.options.streamSize | ||
} | ||
inherits(Encoder, Transform) | ||
Object.keys(LZ4Stream.prototype).forEach(function(method) { | ||
if (!Encoder.prototype[method]) | ||
Encoder.prototype[method] = LZ4Stream.prototype[method]; | ||
}); | ||
|
||
Encoder.prototype._transform = function (data, encoding, done) { | ||
// Buffer the incoming data | ||
this.buffer.push(data) | ||
this.length += data.length | ||
|
||
if (this.first) { | ||
var buf = new Buffer(4) | ||
buf.writeUInt32LE(lz4_static.ARCHIVE_MAGICNUMBER, 0, false) | ||
this.push(buf) | ||
this.first = false | ||
} | ||
var blockMaxSize = this.options.blockMaxSize | ||
|
||
if ( this.length < this.chunkSize ) return done() | ||
|
||
this._compressChunk(this.chunkSize, done) | ||
} | ||
// Not enough data for a block | ||
if ( this.length < blockMaxSize ) return done() | ||
|
||
Encoder.prototype._flush = function (done) { | ||
if (this.length === 0) return done() | ||
// Build the data to be compressed | ||
var buf = Buffer.concat(this.buffer, this.length) | ||
|
||
while ( this.length > this.chunkSize ) { | ||
this._compressChunk(this.chunkSize) | ||
} | ||
// Compress the block | ||
this.add( buf.slice(0, blockMaxSize) ) | ||
|
||
this._compressChunk(this.length, done) | ||
} | ||
// Set the remaining data | ||
if (buf.length > blockMaxSize) { | ||
this.buffer = [] | ||
this.length = 0 | ||
} else { | ||
this.buffer = [ buf.slice(blockMaxSize) ] | ||
this.length = buf.length - blockMaxSize | ||
} | ||
|
||
Encoder.prototype._compressChunk = function (size, done) { | ||
var buf = new Buffer(this.chunkBound) | ||
var input = Buffer.concat(this.buffer, this.length) | ||
var res = this.compress( input.slice(0, size), buf.slice(4) ) | ||
if (res === 0) | ||
return done( null, new Error('Compression error') ) | ||
if (this.fast) { | ||
if (this.first) { | ||
this.push( this.header() ) | ||
this.first = false | ||
} | ||
this.push( this.shiftBlock() ) | ||
} | ||
|
||
buf.writeUInt32LE(res, 0, false) | ||
this.push( buf.slice(0, res + 4) ) | ||
done() | ||
} | ||
|
||
this.length = input.length - size | ||
this.buffer = this.length > 0 ? [ input.slice(size) ] : [] | ||
Encoder.prototype._flush = function (done) { | ||
if (this.length > 0) | ||
this.add( Buffer.concat(this.buffer, this.length) ) | ||
|
||
if (this.fast) { | ||
if (this.first) { | ||
this.push( this.header() ) | ||
this.first = false | ||
} | ||
var block | ||
while ( block = this.shiftBlock() ) this.push(block) | ||
this.push( this.tail() ) | ||
} else { | ||
this.push( this.done() ) | ||
} | ||
|
||
if (done) done() | ||
done() | ||
} | ||
|
||
module.exports = Encoder |