[JAVA-1074] InputStream read() not behaving as expected by Interface Javadoc Created: 11/Jan/14  Updated: 12/Jan/14  Resolved: 12/Jan/14

Status: Closed
Project: Java Driver
Component/s: GridFS
Affects Version/s: None
Fix Version/s: None

Type: Improvement Priority: Major - P3
Reporter: Steve Hummingbird Assignee: Unassigned
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Backwards Compatibility: Fully Compatible

 Description   

The read(final byte[] b, final int off, final int len) method is supposed to try to retrieve the number of bytes specified in 'len'. The current implementation however only retrieves one chunk and reads that chunk. In most cases this leads to less bytes being retrieved than requested. This usually puts the burden on the application to loop over that method until the number of requested bytes could be retrieved. It should be considered if this work can be handled by the driver - at least for the 3.0 update. Unless someone expects the current behaviour, which differs from the javadoc for the Inputstream (which is the only documentation I could find), this change should not break backwards compatibility.



 Comments   
Comment by Jeffrey Yemin [ 12/Jan/14 ]

Thanks for your understanding. I'm going to close this issue, as the current behavior is consistent with the documentation of InputStream.read, and with the actual behavior of other implementations of this method, in particular SocketInputStream.

Comment by Steve Hummingbird [ 11/Jan/14 ]

Thanks for the comment. I can now see that this leaves quite some room for interpretation. Probably that's some sort of nit-picking, but currently the attempt is made to read up to one chunk size, in case 'len' is greater than the chunk size - instead of attempting to read 'len' bytes.
The issue I am seeing is that the caller can not know about the reason for less bytes being retrieved than requested. If the implementation of the stream would really attempt to read up to 'len' bytes, this would lead to less round trips in some cases.
As GridFS is designed to handle large documents, I would expect it to be able to fetch larger amounts of data without requiring manual assembly of the chunks. But I am aware of that this might be seen differently, so please close this issue if that's the case.

Comment by Jeffrey Yemin [ 11/Jan/14 ]

It's not clear to me how you came to this conclusion. The Javadoc for this method states:

Reads up to len bytes of data from the input stream into an array of bytes. An attempt is made to read as many as len bytes, but a smaller number may be read. The number of bytes actually read is returned as an integer.

Can you explain your reasoning?

Generated at Thu Feb 08 08:53:45 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.