@@ -49,6 +49,10 @@ function checkPosition(pos, name) {
4949 }
5050}
5151
52+ function roundUpToMultipleOf8 ( n ) {
53+ return ( n + 7 ) & ~ 7 ; // Align to 8 byte boundary.
54+ }
55+
5256function ReadStream ( path , options ) {
5357 if ( ! ( this instanceof ReadStream ) )
5458 return new ReadStream ( path , options ) ;
@@ -170,10 +174,18 @@ ReadStream.prototype._read = function(n) {
170174 // Now that we know how much data we have actually read, re-wind the
171175 // 'used' field if we can, and otherwise allow the remainder of our
172176 // reservation to be used as a new pool later.
173- if ( start + toRead === thisPool . used && thisPool === pool )
174- thisPool . used += bytesRead - toRead ;
175- else if ( toRead - bytesRead > kMinPoolSpace )
176- poolFragments . push ( thisPool . slice ( start + bytesRead , start + toRead ) ) ;
177+ if ( start + toRead === thisPool . used && thisPool === pool ) {
178+ const newUsed = thisPool . used + bytesRead - toRead ;
179+ thisPool . used = roundUpToMultipleOf8 ( newUsed ) ;
180+ } else {
181+ // Round down to the next lowest multiple of 8 to ensure the new pool
182+ // fragment start and end positions are aligned to an 8 byte boundary.
183+ const alignedEnd = ( start + toRead ) & ~ 7 ;
184+ const alignedStart = roundUpToMultipleOf8 ( start + bytesRead ) ;
185+ if ( alignedEnd - alignedStart >= kMinPoolSpace ) {
186+ poolFragments . push ( thisPool . slice ( alignedStart , alignedEnd ) ) ;
187+ }
188+ }
177189
178190 if ( bytesRead > 0 ) {
179191 this . bytesRead += bytesRead ;
@@ -187,7 +199,8 @@ ReadStream.prototype._read = function(n) {
187199 // Move the pool positions, and internal position for reading.
188200 if ( this . pos !== undefined )
189201 this . pos += toRead ;
190- pool . used += toRead ;
202+
203+ pool . used = roundUpToMultipleOf8 ( pool . used + toRead ) ;
191204} ;
192205
193206ReadStream . prototype . _destroy = function ( err , cb ) {
0 commit comments