[foreign-abi] RFR: 8241148: need a way to create MemorySegment with contents from java String and a way to read a C char* as java String

Athijegannathan Sundararajan sundar at openjdk.java.net
Thu Mar 19 04:07:58 UTC 2020


On Wed, 18 Mar 2020 19:03:41 GMT, Henry Jen <henryjen at openjdk.org> wrote:

>> Adding utility methods to ForeignUnsafe class.
>
> src/jdk.incubator.foreign/share/classes/jdk/incubator/foreign/unsafe/ForeignUnsafe.java line 119:
> 
>> 118:     public static String toJavaString(MemoryAddress addr) {
>> 119:         StringBuilder buf = new StringBuilder();
>> 120:         try (MemorySegment seg = ofNativeUnchecked(addr, Long.MAX_VALUE)) {
> 
> There are use cases to take a C string and make it a byte array in nio, and we have to retain the encoding of byte
> array, some pointed out to me that toJavaString(addr).getBytes() may not be proper if encoding is not properly
> specified(not my experience though).  Thus I have couple functions like following,
> https://github.com/slowhog/panama-foreign/blob/eb79b8c14cfcdeae6a869d6f434929f44ab73dd5/src/java.base/unix/classes/sun/nio/FFIUtils.java#L176

Will add Charset accepting variant as well.

-------------

PR: https://git.openjdk.java.net/panama-foreign/pull/55


More information about the panama-dev mailing list