@@ -141,7 +141,7 @@ public void onResult(final Integer result, final Throwable t) {
141141 private void checkAndFetchResults (final int amountRead , final ByteBuffer dst , final SingleResultCallback <Integer > callback ) {
142142 if (currentPosition == fileInfo .getLength () || dst .remaining () == 0 ) {
143143 callback .onResult (amountRead , null );
144- } else if (! resultsQueue . isEmpty ()) {
144+ } else if (hasResultsToProcess ()) {
145145 processResults (amountRead , dst , callback );
146146 } else if (cursor == null ) {
147147 chunksCollection .find (new Document ("files_id" , fileInfo .getId ())
@@ -181,23 +181,27 @@ public void onResult(final List<Document> result, final Throwable t) {
181181 private void processResults (final int previousAmountRead , final ByteBuffer dst , final SingleResultCallback <Integer > callback ) {
182182 try {
183183 int amountRead = previousAmountRead ;
184- while (currentPosition < fileInfo .getLength () && dst .remaining () > 0 && !resultsQueue .isEmpty ()) {
185- if (buffer == null || bufferOffset == buffer .length ) {
184+ int amountToCopy = dst .remaining ();
185+ while (currentPosition < fileInfo .getLength () && amountToCopy > 0 ) {
186+
187+ if (getBufferFromResultsQueue ()) {
186188 buffer = getBufferFromChunk (resultsQueue .poll (), chunkIndex );
187189 bufferOffset = 0 ;
188190 chunkIndex += 1 ;
189191 }
190192
191- int amountToCopy = dst .remaining ();
192193 if (amountToCopy > buffer .length - bufferOffset ) {
193194 amountToCopy = buffer .length - bufferOffset ;
194195 }
195- dst .put (buffer , bufferOffset , amountToCopy );
196- bufferOffset += amountToCopy ;
197- currentPosition += amountToCopy ;
198- amountRead += amountToCopy ;
199- }
200196
197+ if (amountToCopy > 0 ) {
198+ dst .put (buffer , bufferOffset , amountToCopy );
199+ bufferOffset += amountToCopy ;
200+ currentPosition += amountToCopy ;
201+ amountRead += amountToCopy ;
202+ amountToCopy = dst .remaining ();
203+ }
204+ }
201205 checkAndFetchResults (amountRead , dst , callback );
202206 } catch (MongoGridFSException e ) {
203207 callback .onResult (null , e );
@@ -259,6 +263,14 @@ private byte[] getBufferFromChunk(final Document chunk, final int expectedChunkI
259263 return data ;
260264 }
261265
266+ private boolean getBufferFromResultsQueue () {
267+ return !resultsQueue .isEmpty () && (buffer == null || bufferOffset == buffer .length );
268+ }
269+
270+ private boolean hasResultsToProcess () {
271+ return !resultsQueue .isEmpty () || (buffer != null && bufferOffset < buffer .length );
272+ }
273+
262274 private <A > boolean tryGetReadingLock (final SingleResultCallback <A > callback ) {
263275 if (checkClosed ()) {
264276 callbackClosedException (callback );
0 commit comments