When using GridFSDownloadStream to download a file with multiple chunks, if you use a destination buffer whose size is not exactly equal to the file's chunk size then you will get the following exception:
com.mongodb.MongoGridFSException: Could not find file chunk for files_id: BsonObjectId{value=57c87e6afc76010ce8f43887} at chunk index 2. at com.mongodb.async.client.gridfs.GridFSDownloadStreamImpl.chunkNotFound(GridFSDownloadStreamImpl.java:235) at com.mongodb.async.client.gridfs.GridFSDownloadStreamImpl.access$600(GridFSDownloadStreamImpl.java:38) at com.mongodb.async.client.gridfs.GridFSDownloadStreamImpl$5.onResult(GridFSDownloadStreamImpl.java:168) at com.mongodb.async.client.gridfs.GridFSDownloadStreamImpl$5.onResult(GridFSDownloadStreamImpl.java:162) at com.mongodb.operation.AsyncQueryBatchCursor.next(AsyncQueryBatchCursor.java:109) at com.mongodb.async.client.gridfs.GridFSDownloadStreamImpl.checkAndFetchResults(GridFSDownloadStreamImpl.java:162) at com.mongodb.async.client.gridfs.GridFSDownloadStreamImpl.read(GridFSDownloadStreamImpl.java:132)
I have created a GIST for a JUnit test which demonstrates the issue – https://gist.github.com/sfitts/6ac8fc6efa19ef86600ae5354682c5ca
The issue seems to be that the code decides to do a read-ahead into a chunk that doesn't exist (it is one off the edge) and so doesn't get to use the data already sitting in "buffer" to fulfill the read.