diff --git a/lib/deflate.js b/lib/deflate.js index aef8d83..fee24fe 100644 --- a/lib/deflate.js +++ b/lib/deflate.js @@ -32,7 +32,6 @@ var Deflate = function(options) { } this.strm = new zstream(); - this.strm.next_out = utils.arrayCreate(opt.chunkSize); var status = zlib_deflate.deflateInit2( this.strm, @@ -53,11 +52,13 @@ var Deflate = function(options) { */ Deflate.prototype.push = function(data_in) { var strm = this.strm; - var out; + var chunkSize = this.options.chunkSize; + var sliced; strm.next_in = data_in; strm.next_in_index = 0; strm.avail_in = strm.next_in.length; + strm.next_out = utils.arrayCreate(chunkSize); /* run deflate() on input until output buffer not full, finish compression if all of source has been read in */ @@ -71,9 +72,17 @@ Deflate.prototype.push = function(data_in) { // this.onEnd(ret); //} if(strm.next_out_index) { - out = utils.arrayCreate(strm.next_out_index); - utils.arraySet(out, strm.next_out, 0, strm.next_out_index, 0); - this.onData(out); + if (strm.next_out_index === chunkSize) { + // out buffer completely filled, push it as is + this.onData(strm.next_out); + } else { + // out buffer partially filled, slice it first + sliced = utils.arrayCreate(strm.next_out_index); + utils.arraySet(sliced, strm.next_out, 0, strm.next_out_index, 0); + this.onData(sliced); + } + // Allocate buffer for next chunk + strm.next_out = utils.arrayCreate(this.options.chunkSize); } } while (strm.avail_in > 0 || strm.avail_out === 0); };