Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 25.2k
Fix support for blobs larger than 64 KB on Android#31789
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Uh oh!
There was an error while loading. Please reload this page.
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
1b92132
Register `BlobProvider` as a ContentProvider
tomekzaw 2116f50
Add blob object URL as Image component source example in RNTester app
tomekzaw 2ffbd5a
Fix `BlobProvider.openFile` for blobs larger than pipe capacity
tomekzaw 57e3173
Declare `data` and `writeSide` final
tomekzaw 005d4a6
Fix Flow errors
tomekzaw 82d3284
Fix code style
tomekzaw 4b886e2
Conditionally create new thread
tomekzaw 58c8354
Fix code style
tomekzaw f8d3b85
Use single thread executor
tomekzaw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
42 changes: 35 additions & 7 deletions
42 ReactAndroid/src/main/java/com/facebook/react/modules/blob/BlobProvider.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -20,9 +20,15 @@ | ||
| import java.io.FileNotFoundException; | ||
| import java.io.IOException; | ||
| import java.io.OutputStream; | ||
| import java.util.concurrent.ExecutorService; | ||
| import java.util.concurrent.Executors; | ||
| public final class BlobProvider extends ContentProvider { | ||
| private static final int PIPE_CAPACITY = 65536; | ||
| private ExecutorService executor = Executors.newSingleThreadExecutor(); | ||
| @Override | ||
| public boolean onCreate() { | ||
| return true; | ||
| @@ -72,7 +78,7 @@ public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundEx | ||
| throw new RuntimeException("No blob module associated with BlobProvider"); | ||
| } | ||
| byte[] data = blobModule.resolve(uri); | ||
| final byte[] data = blobModule.resolve(uri); | ||
| if (data == null) { | ||
| throw new FileNotFoundException("Cannot open " + uri.toString() + ", blob not found."); | ||
| } | ||
| @@ -84,12 +90,34 @@ public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundEx | ||
| return null; | ||
| } | ||
| ParcelFileDescriptor readSide = pipe[0]; | ||
| ParcelFileDescriptor writeSide = pipe[1]; | ||
| try (OutputStream outputStream = new ParcelFileDescriptor.AutoCloseOutputStream(writeSide)) { | ||
| outputStream.write(data); | ||
| } catch (IOException exception) { | ||
| return null; | ||
| final ParcelFileDescriptor writeSide = pipe[1]; | ||
| if (data.length <= PIPE_CAPACITY) { | ||
| // If the blob length is less than or equal to pipe capacity (64 KB), | ||
| // we can write the data synchronously to the pipe buffer. | ||
| try (OutputStream outputStream = new ParcelFileDescriptor.AutoCloseOutputStream(writeSide)) { | ||
| outputStream.write(data); | ||
| } catch (IOException exception) { | ||
| return null; | ||
| } | ||
| } else { | ||
| // For blobs larger than 64 KB, a synchronous write would fill up the whole buffer | ||
| // and block forever, because there are no readers to empty the buffer. | ||
| // Writing from a separate thread allows us to return the read side descriptor | ||
| // immediately so that both writer and reader can work concurrently. | ||
| // Reading from the pipe empties the buffer and allows the next chunks to be written. | ||
tomekzaw marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| Runnable writer = | ||
| new Runnable() { | ||
| public void run() { | ||
| try (OutputStream outputStream = | ||
| new ParcelFileDescriptor.AutoCloseOutputStream(writeSide)) { | ||
| outputStream.write(data); | ||
| } catch (IOException exception) { | ||
| // no-op | ||
| } | ||
| } | ||
| }; | ||
| executor.submit(writer); | ||
| } | ||
| return readSide; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| <resources> | ||
| <string name="app_name">RNTester App</string> | ||
| <string name="blob_provider_authority">com.facebook.react.uiapp.blobs</string> | ||
| </resources> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.