Hello.
I am confused by this passage in the JNA Javadoc (emphasis mine):
When converting Java unicode characters into an array of char, the
default platform encoding is used, unless the system property
jna.encoding is set to a valid encoding. This property may be set to
"UTF8", for example, to ensure all native strings use that encoding.
http://java-native-access.github.io/jna/4.2.1/overview-summary.html#stringsHowever in what appears to be the relevant section of the code we have:
public static final String DEFAULT_ENCODING = "utf8";
...
/**
* @return The default string encoding. Returns the value of the system
* property <code>jna.encoding</code> or {@link Native#DEFAULT_ENCODING}.
*/
public static String getDefaultStringEncoding() {
return System.getProperty("jna.encoding", DEFAULT_ENCODING);
}
https://github.com/java-native-access/jna/blob/master/src/com/sun/jna/Native.javaIt seems that the documentation and code do not agree. If the documentation were correct then I'd expect DEFAULT_ENCODING to be set from e.g. Charset.defaultCharset().name(). So which is correct?
This mismatch appears to be the cause of an issue that's been reported with an application I maintain, in which on Windows a file path passed to a native API is misinterpreted when it contains non-ASCII characters because JNA is providing UTF-8 while the native code is expecting Cp1252. The issue is mitigated by setting jna.encoding to Cp1252, but it's not clear that that's safe for all of the libraries we are using that use JNA under the hood.
Thanks,
Aaron