[foreign-memaccess+abi] RFR: 8268230: Foreign Linker API & Windows user32/kernel32: String conversion seems broken [v3]
Jorn Vernee
jvernee at openjdk.java.net
Fri Jul 9 11:11:19 UTC 2021
On Fri, 9 Jul 2021 07:42:14 GMT, Sebastian Stenzel <github.com+1204330+overheadhunter at openjdk.org> wrote:
> If I call `CLinker.toCString(new String(bytes, UTF-16BE))`, it'll still be converted to UTF-8 internally.
Note that the char set that is passed to the `String` constructor only indicates the encoding of the `bytes` that are being passed. These bytes are then converted to one of the internal encodings used by Java strings to produce the new `String`, and then `toCString` converts from that `String` back to a UTF-8 encoded memory region.
---
For encoding to a native representation, here is an example that assumes the native representation of the string should be terminated with 2 null bytes (the exact format the native string should have depends ultimately on the domain, and what the library expects):
String input = foo(); // get a Java string from somewhere
// Using a standard null terminated format here, but the library could also expect a different format
byte[] bytes = (input + '\0').getBytes(UTF_16BE);
MemorySegment text = MemorySegment.allocateNative(bytes.length);
text.copyFrom(MemorySegment.ofArray(bytes));
// use 'text'
For converting from a native string to a Java string a way to determine the length of a string is needed. Examples are `strlen` and `wcslen`, but it depends on the encoding the string is in, and again the domain (e.g. the library might expose a function to determine the length of the strings it returns).
Then something like this could be used:
MemoryAddress input = foo(); // get a native string from somewhere
int length = utf16be_string_length(input);
byte[] bytes = new byte[length];
MemorySegment inputSegment = input.asSegment(length, ResourceScope.newImplicitScope());
MemorySegment.ofArray(bytes).copyFrom(inputSegment);
String text = new String(bytes, UTF_16BE);
// use 'text'
-------------
PR: https://git.openjdk.java.net/panama-foreign/pull/554
More information about the panama-dev
mailing list