RequestBodyUtil.create uses inputStream.available() as contentLength
See original GitHub issueIs this a bug report?
Yes
Have you read the Contributing Guidelines?
Yes
Environment
n/a
Bug
RequestBodyUtil.create(MediaType mediaType, InputStream inputStream)
uses inputStream.available()
to set the contentLength
of the RequestBody which is wrong.
https://developer.android.com/reference/java/io/InputStream.html#available()
Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream. The next invocation might be the same thread or another thread. A single read or skip of this many bytes will not block, but may read or skip fewer bytes.
Note that while some implementations of InputStream will return the total number of bytes in the stream, many will not. It is never correct to use the return value of this method to allocate a buffer intended to hold all data in this stream.
Issue Analytics
- State:
- Created 6 years ago
- Comments:8 (4 by maintainers)
Top GitHub Comments
FileInputStream.available
is still not supposed to be used to figure out the file size.RequestBodyUtil.create(MediaType, InputStream)
should just be deprecated and replaced. The caller should be passing a size along with the media type and body. The callers upstream of this helper function have much better access to the file size.If a
FileInputStream
is being passed to RBU.create then somewhere close upstream it was created from aFile
instance that can just have.size()
called on it.If the
InputStream
comes from the content resolver like ingetFileInputStream
, that same code has the ability to ask Android’s document provider for the size column of thefileContentUri
(which may be unknown so more work will need to be done, but often is not giving you the size you need).👋 hey everyone. Thank you for raising this issue and the discussion of potential solutions here. Given the age of this issue and time since the last activity, I’m going to go ahead and close this issue for now.
However; should you still like this to remain open please let me know and I’ll re-open - I would suggest though tackling this via a PR instead (if someone would like to volunteer that’d be great ❤️) and having the discussion there on the various ways of resolving this issue.
Thank you