{"action":"created","comment":{"url":"https://api.github.com/repos/rozza/mongo-java-driver/pulls/comments/61302251","id":61302251,"diff_hunk":"@@ -122,61 +122,87 @@ public void onResult(final GridFSFile result, final Throwable t) {\n             return;\n         } else if (currentPosition == fileInfo.getLength()) {\n             releaseReadingLock();\n-            callback.onResult(-1, null);\n+            errorHandlingCallback.onResult(-1, null);\n             return;\n         }\n-\n-        int amountToRead = dst.remaining();\n-        if (fileInfo.getLength() < amountToRead) {\n-            amountToRead = (int) fileInfo.getLength();\n-        }\n-        read(amountToRead, dst, errorHandlingCallback);\n+        checkAndFetchResults(0, dst, new SingleResultCallback<Integer>() {\n+            @Override\n+            public void onResult(final Integer result, final Throwable t) {\n+                releaseReadingLock();\n+                errorHandlingCallback.onResult(result, t);\n+            }\n+        });\n     }\n \n-    private void read(final int amountRead, final ByteBuffer dst, final SingleResultCallback<Integer> callback) {\n+    private void checkAndFetchResults(final int amountRead, final ByteBuffer dst, final SingleResultCallback<Integer> callback) {\n         if (currentPosition == fileInfo.getLength() || dst.remaining() == 0) {\n-            releaseReadingLock();\n             callback.onResult(amountRead, null);\n-            return;\n-        }\n-\n-        boolean fetchBuffer = false;\n-        if (buffer == null) {\n-            fetchBuffer = true;\n-        } else if (bufferOffset == buffer.length) {\n-            chunkIndex += 1;\n-            bufferOffset = 0;\n-            fetchBuffer = true;\n-        }\n-\n-        final RecursiveReadCallback recursiveCallback = new RecursiveReadCallback(amountRead, dst, callback);\n-        if (fetchBuffer) {\n-            getBuffer(chunkIndex, new SingleResultCallback<byte[]>() {\n+        } else if (!resultsQueue.isEmpty()) {\n+            processResults(amountRead, dst, callback);\n+        } else if (cursor == null) {\n+            chunksCollection.find(new Document(\"files_id\", fileInfo.getId())\n+                    .append(\"n\", new Document(\"$gte\", chunkIndex)))\n+                    .batchSize(batchSize).sort(new Document(\"n\", 1))\n+                    .batchCursor(new SingleResultCallback<AsyncBatchCursor<Document>>() {\n+                        @Override\n+                        public void onResult(final AsyncBatchCursor<Document> result, final Throwable t) {\n+                            if (t != null) {\n+                                callback.onResult(null, t);\n+                            } else if (result == null) {\n+                                chunkNotFound(chunkIndex, callback);\n+                            } else {\n+                                cursor = result;\n+                                checkAndFetchResults(amountRead, dst, callback);\n+                            }\n+                        }\n+                    });\n+        } else {\n+            cursor.next(new SingleResultCallback<List<Document>>() {\n                 @Override\n-                public void onResult(final byte[] result, final Throwable t) {\n+                public void onResult(final List<Document> result, final Throwable t) {\n                     if (t != null) {\n-                        releaseReadingLock();\n                         callback.onResult(null, t);\n+                    } else if (result == null || result.isEmpty()) {\n+                        chunkNotFound(chunkIndex, callback);","path":"driver-async/src/main/com/mongodb/async/client/gridfs/GridFSDownloadStreamImpl.java","position":null,"original_position":71,"commit_id":"b68642bb7d1e2974839367e4e9b02913058b0e11","original_commit_id":"dc0b14ef3af94604ab7675a88af71c508125af8e","user":{"login":"rozza","id":420,"avatar_url":"https://avatars.githubusercontent.com/u/420?v=3","gravatar_id":"","url":"https://api.github.com/users/rozza","html_url":"https://github.com/rozza","followers_url":"https://api.github.com/users/rozza/followers","following_url":"https://api.github.com/users/rozza/following{/other_user}","gists_url":"https://api.github.com/users/rozza/gists{/gist_id}","starred_url":"https://api.github.com/users/rozza/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rozza/subscriptions","organizations_url":"https://api.github.com/users/rozza/orgs","repos_url":"https://api.github.com/users/rozza/repos","events_url":"https://api.github.com/users/rozza/events{/privacy}","received_events_url":"https://api.github.com/users/rozza/received_events","type":"User","site_admin":false},"body":"Added a test case for this scenario","created_at":"2016-04-27T17:36:22Z","updated_at":"2016-04-27T17:36:22Z","html_url":"https://github.com/rozza/mongo-java-driver/pull/152#discussion_r61302251","pull_request_url":"https://api.github.com/repos/rozza/mongo-java-driver/pulls/152","_links":{"self":{"href":"https://api.github.com/repos/rozza/mongo-java-driver/pulls/comments/61302251"},"html":{"href":"https://github.com/rozza/mongo-java-driver/pull/152#discussion_r61302251"},"pull_request":{"href":"https://api.github.com/repos/rozza/mongo-java-driver/pulls/152"}}},"pull_request":{"url":"https://api.github.com/repos/rozza/mongo-java-driver/pulls/152","id":67923671,"html_url":"https://github.com/rozza/mongo-java-driver/pull/152","diff_url":"https://github.com/rozza/mongo-java-driver/pull/152.diff","patch_url":"https://github.com/rozza/mongo-java-driver/pull/152.patch","issue_url":"https://api.github.com/repos/rozza/mongo-java-driver/issues/152","number":152,"state":"open","locked":false,"title":"Improved how small chunksizes are handled in Async GridFS","user":{"login":"rozza","id":420,"avatar_url":"https://avatars.githubusercontent.com/u/420?v=3","gravatar_id":"","url":"https://api.github.com/users/rozza","html_url":"https://github.com/rozza","followers_url":"https://api.github.com/users/rozza/followers","following_url":"https://api.github.com/users/rozza/following{/other_user}","gists_url":"https://api.github.com/users/rozza/gists{/gist_id}","starred_url":"https://api.github.com/users/rozza/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rozza/subscriptions","organizations_url":"https://api.github.com/users/rozza/orgs","repos_url":"https://api.github.com/users/rozza/repos","events_url":"https://api.github.com/users/rozza/events{/privacy}","received_events_url":"https://api.github.com/users/rozza/received_events","type":"User","site_admin":false},"body":"Added minimum bufferSize when downloading to AsyncOutputStream\r\nReduced recursiveness of GridFSDownloadStreamImpl when consuming chunks.\r\n\r\nJAVA-1282","created_at":"2016-04-26T17:13:58Z","updated_at":"2016-04-27T17:36:22Z","closed_at":null,"merged_at":null,"merge_commit_sha":"f3ae6f8ed0cd3af4b772001cfb4239383d83375c","assignee":{"login":"jyemin","id":1111546,"avatar_url":"https://avatars.githubusercontent.com/u/1111546?v=3","gravatar_id":"","url":"https://api.github.com/users/jyemin","html_url":"https://github.com/jyemin","followers_url":"https://api.github.com/users/jyemin/followers","following_url":"https://api.github.com/users/jyemin/following{/other_user}","gists_url":"https://api.github.com/users/jyemin/gists{/gist_id}","starred_url":"https://api.github.com/users/jyemin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jyemin/subscriptions","organizations_url":"https://api.github.com/users/jyemin/orgs","repos_url":"https://api.github.com/users/jyemin/repos","events_url":"https://api.github.com/users/jyemin/events{/privacy}","received_events_url":"https://api.github.com/users/jyemin/received_events","type":"User","site_admin":false},"milestone":null,"commits_url":"https://api.github.com/repos/rozza/mongo-java-driver/pulls/152/commits","review_comments_url":"https://api.github.com/repos/rozza/mongo-java-driver/pulls/152/comments","review_comment_url":"https://api.github.com/repos/rozza/mongo-java-driver/pulls/comments{/number}","comments_url":"https://api.github.com/repos/rozza/mongo-java-driver/issues/152/comments","statuses_url":"https://api.github.com/repos/rozza/mongo-java-driver/statuses/b68642bb7d1e2974839367e4e9b02913058b0e11","head":{"label":"rozza:j1282","ref":"j1282","sha":"b68642bb7d1e2974839367e4e9b02913058b0e11","user":{"login":"rozza","id":420,"avatar_url":"https://avatars.githubusercontent.com/u/420?v=3","gravatar_id":"","url":"https://api.github.com/users/rozza","html_url":"https://github.com/rozza","followers_url":"https://api.github.com/users/rozza/followers","following_url":"https://api.github.com/users/rozza/following{/other_user}","gists_url":"https://api.github.com/users/rozza/gists{/gist_id}","starred_url":"https://api.github.com/users/rozza/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rozza/subscriptions","organizations_url":"https://api.github.com/users/rozza/orgs","repos_url":"https://api.github.com/users/rozza/repos","events_url":"https://api.github.com/users/rozza/events{/privacy}","received_events_url":"https://api.github.com/users/rozza/received_events","type":"User","site_admin":false},"repo":{"id":10246360,"name":"mongo-java-driver","full_name":"rozza/mongo-java-driver","owner":{"login":"rozza","id":420,"avatar_url":"https://avatars.githubusercontent.com/u/420?v=3","gravatar_id":"","url":"https://api.github.com/users/rozza","html_url":"https://github.com/rozza","followers_url":"https://api.github.com/users/rozza/followers","following_url":"https://api.github.com/users/rozza/following{/other_user}","gists_url":"https://api.github.com/users/rozza/gists{/gist_id}","starred_url":"https://api.github.com/users/rozza/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rozza/subscriptions","organizations_url":"https://api.github.com/users/rozza/orgs","repos_url":"https://api.github.com/users/rozza/repos","events_url":"https://api.github.com/users/rozza/events{/privacy}","received_events_url":"https://api.github.com/users/rozza/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/rozza/mongo-java-driver","description":"java driver for mongo","fork":true,"url":"https://api.github.com/repos/rozza/mongo-java-driver","forks_url":"https://api.github.com/repos/rozza/mongo-java-driver/forks","keys_url":"https://api.github.com/repos/rozza/mongo-java-driver/keys{/key_id}","collaborators_url":"https://api.github.com/repos/rozza/mongo-java-driver/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/rozza/mongo-java-driver/teams","hooks_url":"https://api.github.com/repos/rozza/mongo-java-driver/hooks","issue_events_url":"https://api.github.com/repos/rozza/mongo-java-driver/issues/events{/number}","events_url":"https://api.github.com/repos/rozza/mongo-java-driver/events","assignees_url":"https://api.github.com/repos/rozza/mongo-java-driver/assignees{/user}","branches_url":"https://api.github.com/repos/rozza/mongo-java-driver/branches{/branch}","tags_url":"https://api.github.com/repos/rozza/mongo-java-driver/tags","blobs_url":"https://api.github.com/repos/rozza/mongo-java-driver/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/rozza/mongo-java-driver/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/rozza/mongo-java-driver/git/refs{/sha}","trees_url":"https://api.github.com/repos/rozza/mongo-java-driver/git/trees{/sha}","statuses_url":"https://api.github.com/repos/rozza/mongo-java-driver/statuses/{sha}","languages_url":"https://api.github.com/repos/rozza/mongo-java-driver/languages","stargazers_url":"https://api.github.com/repos/rozza/mongo-java-driver/stargazers","contributors_url":"https://api.github.com/repos/rozza/mongo-java-driver/contributors","subscribers_url":"https://api.github.com/repos/rozza/mongo-java-driver/subscribers","subscription_url":"https://api.github.com/repos/rozza/mongo-java-driver/subscription","commits_url":"https://api.github.com/repos/rozza/mongo-java-driver/commits{/sha}","git_commits_url":"https://api.github.com/repos/rozza/mongo-java-driver/git/commits{/sha}","comments_url":"https://api.github.com/repos/rozza/mongo-java-driver/comments{/number}","issue_comment_url":"https://api.github.com/repos/rozza/mongo-java-driver/issues/comments{/number}","contents_url":"https://api.github.com/repos/rozza/mongo-java-driver/contents/{+path}","compare_url":"https://api.github.com/repos/rozza/mongo-java-driver/compare/{base}...{head}","merges_url":"https://api.github.com/repos/rozza/mongo-java-driver/merges","archive_url":"https://api.github.com/repos/rozza/mongo-java-driver/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/rozza/mongo-java-driver/downloads","issues_url":"https://api.github.com/repos/rozza/mongo-java-driver/issues{/number}","pulls_url":"https://api.github.com/repos/rozza/mongo-java-driver/pulls{/number}","milestones_url":"https://api.github.com/repos/rozza/mongo-java-driver/milestones{/number}","notifications_url":"https://api.github.com/repos/rozza/mongo-java-driver/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/rozza/mongo-java-driver/labels{/name}","releases_url":"https://api.github.com/repos/rozza/mongo-java-driver/releases{/id}","deployments_url":"https://api.github.com/repos/rozza/mongo-java-driver/deployments","created_at":"2013-05-23T15:06:03Z","updated_at":"2015-09-01T13:30:57Z","pushed_at":"2016-04-27T17:36:12Z","git_url":"git://github.com/rozza/mongo-java-driver.git","ssh_url":"git@github.com:rozza/mongo-java-driver.git","clone_url":"https://github.com/rozza/mongo-java-driver.git","svn_url":"https://github.com/rozza/mongo-java-driver","homepage":"","size":39852,"stargazers_count":1,"watchers_count":1,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":0,"mirror_url":null,"open_issues_count":2,"forks":0,"open_issues":2,"watchers":1,"default_branch":"master"}},"base":{"label":"rozza:master","ref":"master","sha":"7b8af2f68110cbfbe3363d471c7f7c8aa1540a17","user":{"login":"rozza","id":420,"avatar_url":"https://avatars.githubusercontent.com/u/420?v=3","gravatar_id":"","url":"https://api.github.com/users/rozza","html_url":"https://github.com/rozza","followers_url":"https://api.github.com/users/rozza/followers","following_url":"https://api.github.com/users/rozza/following{/other_user}","gists_url":"https://api.github.com/users/rozza/gists{/gist_id}","starred_url":"https://api.github.com/users/rozza/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rozza/subscriptions","organizations_url":"https://api.github.com/users/rozza/orgs","repos_url":"https://api.github.com/users/rozza/repos","events_url":"https://api.github.com/users/rozza/events{/privacy}","received_events_url":"https://api.github.com/users/rozza/received_events","type":"User","site_admin":false},"repo":{"id":10246360,"name":"mongo-java-driver","full_name":"rozza/mongo-java-driver","owner":{"login":"rozza","id":420,"avatar_url":"https://avatars.githubusercontent.com/u/420?v=3","gravatar_id":"","url":"https://api.github.com/users/rozza","html_url":"https://github.com/rozza","followers_url":"https://api.github.com/users/rozza/followers","following_url":"https://api.github.com/users/rozza/following{/other_user}","gists_url":"https://api.github.com/users/rozza/gists{/gist_id}","starred_url":"https://api.github.com/users/rozza/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rozza/subscriptions","organizations_url":"https://api.github.com/users/rozza/orgs","repos_url":"https://api.github.com/users/rozza/repos","events_url":"https://api.github.com/users/rozza/events{/privacy}","received_events_url":"https://api.github.com/users/rozza/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/rozza/mongo-java-driver","description":"java driver for mongo","fork":true,"url":"https://api.github.com/repos/rozza/mongo-java-driver","forks_url":"https://api.github.com/repos/rozza/mongo-java-driver/forks","keys_url":"https://api.github.com/repos/rozza/mongo-java-driver/keys{/key_id}","collaborators_url":"https://api.github.com/repos/rozza/mongo-java-driver/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/rozza/mongo-java-driver/teams","hooks_url":"https://api.github.com/repos/rozza/mongo-java-driver/hooks","issue_events_url":"https://api.github.com/repos/rozza/mongo-java-driver/issues/events{/number}","events_url":"https://api.github.com/repos/rozza/mongo-java-driver/events","assignees_url":"https://api.github.com/repos/rozza/mongo-java-driver/assignees{/user}","branches_url":"https://api.github.com/repos/rozza/mongo-java-driver/branches{/branch}","tags_url":"https://api.github.com/repos/rozza/mongo-java-driver/tags","blobs_url":"https://api.github.com/repos/rozza/mongo-java-driver/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/rozza/mongo-java-driver/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/rozza/mongo-java-driver/git/refs{/sha}","trees_url":"https://api.github.com/repos/rozza/mongo-java-driver/git/trees{/sha}","statuses_url":"https://api.github.com/repos/rozza/mongo-java-driver/statuses/{sha}","languages_url":"https://api.github.com/repos/rozza/mongo-java-driver/languages","stargazers_url":"https://api.github.com/repos/rozza/mongo-java-driver/stargazers","contributors_url":"https://api.github.com/repos/rozza/mongo-java-driver/contributors","subscribers_url":"https://api.github.com/repos/rozza/mongo-java-driver/subscribers","subscription_url":"https://api.github.com/repos/rozza/mongo-java-driver/subscription","commits_url":"https://api.github.com/repos/rozza/mongo-java-driver/commits{/sha}","git_commits_url":"https://api.github.com/repos/rozza/mongo-java-driver/git/commits{/sha}","comments_url":"https://api.github.com/repos/rozza/mongo-java-driver/comments{/number}","issue_comment_url":"https://api.github.com/repos/rozza/mongo-java-driver/issues/comments{/number}","contents_url":"https://api.github.com/repos/rozza/mongo-java-driver/contents/{+path}","compare_url":"https://api.github.com/repos/rozza/mongo-java-driver/compare/{base}...{head}","merges_url":"https://api.github.com/repos/rozza/mongo-java-driver/merges","archive_url":"https://api.github.com/repos/rozza/mongo-java-driver/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/rozza/mongo-java-driver/downloads","issues_url":"https://api.github.com/repos/rozza/mongo-java-driver/issues{/number}","pulls_url":"https://api.github.com/repos/rozza/mongo-java-driver/pulls{/number}","milestones_url":"https://api.github.com/repos/rozza/mongo-java-driver/milestones{/number}","notifications_url":"https://api.github.com/repos/rozza/mongo-java-driver/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/rozza/mongo-java-driver/labels{/name}","releases_url":"https://api.github.com/repos/rozza/mongo-java-driver/releases{/id}","deployments_url":"https://api.github.com/repos/rozza/mongo-java-driver/deployments","created_at":"2013-05-23T15:06:03Z","updated_at":"2015-09-01T13:30:57Z","pushed_at":"2016-04-27T17:36:12Z","git_url":"git://github.com/rozza/mongo-java-driver.git","ssh_url":"git@github.com:rozza/mongo-java-driver.git","clone_url":"https://github.com/rozza/mongo-java-driver.git","svn_url":"https://github.com/rozza/mongo-java-driver","homepage":"","size":39852,"stargazers_count":1,"watchers_count":1,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":0,"mirror_url":null,"open_issues_count":2,"forks":0,"open_issues":2,"watchers":1,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/rozza/mongo-java-driver/pulls/152"},"html":{"href":"https://github.com/rozza/mongo-java-driver/pull/152"},"issue":{"href":"https://api.github.com/repos/rozza/mongo-java-driver/issues/152"},"comments":{"href":"https://api.github.com/repos/rozza/mongo-java-driver/issues/152/comments"},"review_comments":{"href":"https://api.github.com/repos/rozza/mongo-java-driver/pulls/152/comments"},"review_comment":{"href":"https://api.github.com/repos/rozza/mongo-java-driver/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/rozza/mongo-java-driver/pulls/152/commits"},"statuses":{"href":"https://api.github.com/repos/rozza/mongo-java-driver/statuses/b68642bb7d1e2974839367e4e9b02913058b0e11"}}}}