@@ -36,7 +36,6 @@ function LZ4Stream (options) {
36
36
this . options = o
37
37
38
38
this . compress = o . highCompression ? lz4_binding . compressHCLimited : lz4_binding . compressLimited
39
- this . chunkBound = lz4_binding . compressBound ( o . blockMaxSize )
40
39
41
40
// Build the stream descriptor from the options
42
41
// flags
@@ -66,18 +65,23 @@ function LZ4Stream (options) {
66
65
}
67
66
68
67
// Add data to the stream, splitting blocks according to blockMaxSize
69
- LZ4Stream . prototype . push = function ( data ) {
68
+ LZ4Stream . prototype . add = function ( data ) {
70
69
if ( ! data ) return
71
70
72
71
for ( var size , i = 0 , n = data . length ; i < n ; i += size ) {
73
72
size = Math . min ( n - i , this . options . blockMaxSize )
74
- this . _push ( data . slice ( i , i + size ) )
73
+ this . _add ( data . slice ( i , i + size ) )
75
74
}
76
75
}
77
76
77
+ // Shift a block
78
+ LZ4Stream . prototype . shiftBlock = function ( ) {
79
+ return this . blocks . shift ( )
80
+ }
81
+
78
82
// Compress and add a data block to the stream
79
83
// The block is uncompressed if it is bigger than blockMaxSize
80
- LZ4Stream . prototype . _push = function ( data ) {
84
+ LZ4Stream . prototype . _add = function ( data ) {
81
85
if ( ! data ) return
82
86
83
87
var compressed = new Buffer ( data . length )
@@ -114,10 +118,14 @@ LZ4Stream.prototype._push = function (data) {
114
118
this . size += data . length
115
119
}
116
120
117
- LZ4Stream . prototype . flush = function ( ) {
118
- var res = [ lz4_static . MAGICNUMBER_BUFFER ]
119
- // Allocate maximum descriptor size...
120
- var descriptor = new Buffer ( 15 )
121
+ LZ4Stream . prototype . header = function ( ) {
122
+ // Allocate magic number + maximum descriptor size
123
+ var magicSize = 4
124
+ var res = new Buffer ( magicSize + 15 )
125
+
126
+ res . writeUInt32LE ( lz4_static . MAGICNUMBER , 0 , false )
127
+
128
+ var descriptor = res . slice ( magicSize )
121
129
var descriptorLength = 3
122
130
123
131
// Update the stream descriptor
@@ -143,22 +151,37 @@ LZ4Stream.prototype.flush = function () {
143
151
, descriptorLength - 1 , false
144
152
)
145
153
146
- // ...then slice it accordingly
154
+ // Adjust size according to descriptor length
147
155
if ( descriptorLength < descriptor . length )
148
- descriptor = descriptor . slice ( 0 , descriptorLength )
149
-
150
- res . push ( descriptor )
156
+ res = res . slice ( 0 , magicSize + descriptorLength )
151
157
152
- // Add compressed blocks
153
- res . push . apply ( res , this . blocks )
158
+ return res
159
+ }
154
160
155
- res . push ( lz4_static . EOS_BUFFER )
161
+ LZ4Stream . prototype . tail = function ( ) {
162
+ var eosSize = 4
156
163
157
164
if ( this . options . streamChecksum ) {
158
- var checksum = new Buffer ( 4 )
159
- checksum . writeUInt32LE ( utils . streamChecksum ( null , this . checksum ) , 0 , false )
160
- res . push ( checksum )
165
+ var res = new Buffer ( eosSize + 4 )
166
+ res . writeUInt32LE ( utils . streamChecksum ( null , this . checksum ) , eosSize , false )
167
+ } else {
168
+ var res = new Buffer ( eosSize )
161
169
}
170
+
171
+ res . writeUInt32LE ( lz4_static . EOS , 0 , false )
172
+
173
+ return res
174
+ }
175
+
176
+ LZ4Stream . prototype . done = function ( ) {
177
+ var res = [ ]
178
+
179
+ res . push ( this . header ( ) )
180
+
181
+ // Add compressed blocks
182
+ res . push . apply ( res , this . blocks )
183
+
184
+ res . push ( this . tail ( ) )
162
185
163
186
return Buffer . concat ( res )
164
187
}
@@ -172,6 +195,8 @@ function integerBytesLength (i) {
172
195
173
196
exports . LZ4_compress = function ( input , options ) {
174
197
var LZ4S = new LZ4Stream ( options )
175
- LZ4S . push ( input )
176
- return LZ4S . flush ( )
198
+ LZ4S . add ( input )
199
+ return LZ4S . done ( )
177
200
}
201
+
202
+ exports . LZ4Stream = LZ4Stream
0 commit comments