dB. | Moscow - Geneva - Seattle - New York
dblock.org - @dblockdotorg
Suppose you have a function:
// Returns the number of characters written to the buffer
int getString(char* buffer, int bufsize);
The native code is expecting a fixed-size buffer, which it will fill in with the requested data. A Java String
is not appropriate here, since Strings are immutable. Nor is a Java StringBuffer
, since the native code only fills the buffer and does not change its size. The appropriate argument type would be either byte[]
, Memory
, or an NIO Buffer, with the size of the object passed as the second argument. The method Native.toString(byte[])
may then be used to convert the array of byte into a Java String.