Skip to content
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

jnigen should offer efficient methods to create and extract data from ByteBuffers #563

Closed
brianquinlan opened this issue Sep 7, 2023 · 2 comments
Assignees

Comments

@brianquinlan
Copy link
Contributor

brianquinlan commented Sep 7, 2023

For example:

// Get the data out of a byte buffer without having to index into it's array 1,000,000 times.
final b = java.ByteBuffer.allocateDirect(1000000);
UInt8List d = b.toUInt8List();

// Populate a ByteBuffer without having to index into an array 1,000,000 times
final d = UInt8List(1000000);
java.ByteBuffer b = d.toJByteBuffer();

The JNI functions that can help with this are:

@brianquinlan
Copy link
Contributor Author

I'm creating bindings for Cronet and have two use cases:

I need to convert a complete java.nio.ByteBuffer into a List<int>. The code currently looks like:

      onReadCompleted: (urlRequest, responseInfo, byteBuffer) {
        byteBuffer.flip();

        final remaining = byteBuffer.remaining();
        final data = Uint8List(remaining);
        // TODO: Use a more efficient approach when
        // https://github.com/dart-lang/native/issues/563 is fixed.
        for (var i = 0; i < remaining; ++i) {
          data[i] = byteBuffer.get1(i);
        }
        responseStream!.add(data);
        byteBuffer.clear();
        cronetRequest.read(byteBuffer);
      },

I have a List<int> and need to convert it to a java.nio.ByteBuffer (or byte[]). The code currently looks like:

    if (body.isNotEmpty) {
      // TODO: Use a more efficient approach when
      // https://github.com/dart-lang/native/issues/563 is fixed.
      final bodyBytes = JArray(jbyte.type, body.length);
      for (var i = 0; i < body.length; ++i) {
        bodyBytes[i] = body[i];
      }
      builder.setUploadDataProvider(
          // This also accepts a `java.nio.ByteBuffer`
          jb.UploadDataProviders.create4(bodyBytes), _executor);

Eventually I'll want to implement by own UploadDataProvider, which will mean copying the data from a List<int> to an offset in a java.nio.ByteBuffer

@HosseinYousefi HosseinYousefi self-assigned this Sep 12, 2023
@HosseinYousefi
Copy link
Member

Fixed by dart-lang/jnigen#390.

@HosseinYousefi HosseinYousefi transferred this issue from dart-lang/jnigen Nov 17, 2023
parlough pushed a commit to parlough/native that referenced this issue Apr 8, 2024
Bumps [actions/checkout](https://github.com/actions/checkout) from 3.5.0 to 3.5.2.
- [Release notes](https://github.com/actions/checkout/releases)
- [Changelog](https://github.com/actions/checkout/blob/main/CHANGELOG.md)
- [Commits](actions/checkout@8f4b7f8...8e5e7e5)

---
updated-dependencies:
- dependency-name: actions/checkout
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

2 participants