{
private Pointer pointer;
private boolean wide;
/** Create a native string (NUL-terminated array of char
).
* If the system property jna.encoding
is set, its value will
* be used to encode the native string. If not set or if the encoding
* is unavailable, the default platform encoding will be used.
*/
public NativeString(String string) {
this(string, false);
}
/** Create a native string as a NUL-terminated array of wchar_t
* (if wide
is true) or char
.
* If the system property jna.encoding
is set, its value will
* be used to encode the native char
string.
* If not set or if the encoding is unavailable, the default platform
* encoding will be used.
*
* @param string value to write to native memory
* @param wide whether to store the String as wchar_t
*/
public NativeString(String string, boolean wide) {
if (string == null) {
throw new NullPointerException("String must not be null");
}
// Allocate the memory to hold the string. Note, we have to
// make this 1 element longer in order to accommodate the terminating
// NUL (which is generated in Pointer.setString()).
this.wide = wide;
if (wide) {
int len = (string.length() + 1 ) * Native.WCHAR_SIZE;
pointer = new Memory(len);
pointer.setString(0, string);
}
else {
byte[] data = getBytes(string);
pointer = new Memory(data.length + 1);
pointer.write(0, data, 0, data.length);
pointer.setByte(data.length, (byte)0);
}
}
static byte[] getBytes(String s) {
try {
return getBytes(s, System.getProperty("jna.encoding"));
}
catch (UnsupportedEncodingException e) {
return s.getBytes();
}
}
/** Return a byte array corresponding to the given String, using the given
encoding.
*/
static byte[] getBytes(String s, String encoding) throws UnsupportedEncodingException {
if (encoding != null) {
return s.getBytes(encoding);
}
return s.getBytes();
}
public int hashCode() {
return toString().hashCode();
}
public boolean equals(Object other) {
if (other instanceof CharSequence) {
return compareTo(other) == 0;
}
return false;
}
public String toString() {
String s = wide ? "const wchar_t*" : "const char*";
s += "(" + pointer.getString(0) + ")";
return s;
}
public Pointer getPointer() {
return pointer;
}
public char charAt(int index) {
return toString().charAt(index);
}
public int length() {
return toString().length();
}
public CharSequence subSequence(int start, int end) {
return CharBuffer.wrap(toString()).subSequence(start, end);
}
public int compareTo(Object other) {
if (other == null)
return 1;
return toString().compareTo(other.toString());
}
}