Skip to content

Commit

Permalink
Fix RequestBodyUtil for older version of Android (M or before) (#28399)
Browse files Browse the repository at this point in the history
Summary:
This is bugfix for following code in RequestBodyUtil.java.

```
stream.getChannel().transferFrom(channel, 0, Long.MAX_VALUE);
```

This throws IllegalArgumentException in Android M or before. It seems in old version of JVM it internally casts third argument to integer so when you pass value that is larger than Integer.MAX_VALUE app would crash.

See:
https://bugs.openjdk.java.net/browse/JDK-5105464
https://stackoverflow.com/questions/55696035/thrown-illegalargumentexception-when-using-java-nio

## Changelog

[Android] [Fixed] Fix issue downloading request body via remote URL

Pull Request resolved: #28399

Test Plan:
Run following code on Android M or before. It would crash w/o this PR but it won't with this PR.

```
const body = new FormData();
const image = { uri: "https://reactnative.dev/img/showcase/facebook.png", path: null };
body.append('user[img]', fileBody(image));
fetch("https://example.com", {
    method: 'POST',
    headers: {
    Accept: 'application/json',
    'Content-Type': 'multipart/form-data;',
    },
    body: body
});
```

Reviewed By: christophpurrer, cipolleschi

Differential Revision: D45057263

Pulled By: cortinico

fbshipit-source-id: e0306eb157a5aa92619ac51e67d106b8651a0ba7
  • Loading branch information
daisy1754 authored and facebook-github-bot committed Apr 18, 2023
1 parent f7dc24c commit 4b39f44
Showing 1 changed file with 7 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.util.Base64;
import androidx.annotation.Nullable;
import com.facebook.common.logging.FLog;
Expand Down Expand Up @@ -96,7 +97,12 @@ private static InputStream getDownloadFileInputStream(Context context, Uri uri)
try {
final FileOutputStream stream = new FileOutputStream(file);
try {
stream.getChannel().transferFrom(channel, 0, Long.MAX_VALUE);
long maxBytes = Long.MAX_VALUE;
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.M) {
// Old version of Android internally cast value to integer
maxBytes = (long) Integer.MAX_VALUE;
}
stream.getChannel().transferFrom(channel, 0, maxBytes);
return new FileInputStream(file);
} finally {
stream.close();
Expand Down

0 comments on commit 4b39f44

Please sign in to comment.