question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

RequestBodyUtil.create uses inputStream.available() as contentLength

See original GitHub issue

Is this a bug report?

Yes

Have you read the Contributing Guidelines?

Yes

Environment

n/a

Bug

See: https://github.com/facebook/react-native/blob/master/ReactAndroid/src/main/java/com/facebook/react/modules/network/RequestBodyUtil.java#L132

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:closed
  • Created 6 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
dantmancommented, Jan 14, 2018

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 a File instance that can just have .size() called on it.

If the InputStream comes from the content resolver like in getFileInputStream, that same code has the ability to ask Android’s document provider for the size column of the fileContentUri (which may be unknown so more work will need to be done, but often is not giving you the size you need).

0reactions
Salakarcommented, Apr 10, 2019

👋 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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Android how to post InputStream by chunks using okhttp
class RequestBodyUtil { companion object{ fun create(inputStream: ... fun contentLength(): Long { return try { inputStream.available().
Read more >
Example usage for com.squareup.okhttp MultipartBuilder type
public static void presignedPostPolicy_test() throws Exception ... RequestBodyUtil.create(MediaType.parse("image/jpg"), inputStream); // verify body ...
Read more >
Upload from android gallery to Server - CommonsWare
Create a file in getFilesDir(); Try uploading that file using more conventional RequestBody options. If that fails, then my guess is that there ......
Read more >
ContentLengthInputStream (HttpClient 3.1 API)
ContentLengthInputStream. public ContentLengthInputStream(InputStream in, long contentLength). Creates a new length limited stream. Parameters: in - ...
Read more >
Unchecked S3 object metadata content length
Failure to specify a content length causes the contents of the input stream to buffer locally in memory in order to calculate its...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found