facebookincubator / fbjni

A library designed to simplify the usage of the Java Native Interface
Apache License 2.0
266 stars 47 forks source link

feat: Add `bigEndian()` and `littleEndian()` fields to `JByteOrder` #93

Closed mrousavy closed 9 months ago

mrousavy commented 9 months ago

Motivation

JByteBuffers could only be initialized with nativeOrder endianness. With this PR, both littleEndian() and bigEndian() fields are exposed in JByteOrder and can now be set through JByteBuffer::order(...).

Summary

Get static fields LITTLE_ENDIAN and BIG_ENDIAN through JByteOrder.

Test Plan

Test code:

Default:

auto buffer = JByteBuffer::allocateDirect(5);
uint8_t* data = buffer->getDirectData();
data[0] = 255;
data[1] = 1;
__android_log_print(ANDROID_LOG_INFO, "Test", "First item: %i", data[0]);

With custom Endian:

auto buffer = JByteBuffer::allocateDirect(5);
buffer->order(JByteOrder::bigEndian()); // <-- or littleEndian()
uint8_t* data = buffer->getDirectData();
data[0] = 255;
data[1] = 1;
__android_log_print(ANDROID_LOG_INFO, "Test", "First item: %i", data[0]);
facebook-github-bot commented 9 months ago

@cortinico has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

mrousavy commented 9 months ago

What's the linter complaining about? Probably the long line in littleEndian()? Can I run this locally?

cortinico commented 9 months ago

What's the linter complaining about? Probably the long line in littleEndian()? Can I run this locally?

It's a clang formatting issue. I've reformatted it for you and scheduled a land.

Will need also a new release of fbjni if you need to use this. If you could do the bump to NDK 26.1 similarly to how I did on https://github.com/facebook/react-native/pull/42656 would be extremely helpful 👍 so we can get a new release out soon

mrousavy commented 9 months ago

Thanks! & sure, I'll do that tmrw :)

facebook-github-bot commented 9 months ago

@cortinico merged this pull request in facebookincubator/fbjni@0454714cf0692c4efdb49a94d29d5d8f11667bbc.

mrousavy commented 9 months ago

Done: https://github.com/facebookincubator/fbjni/pull/94