All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sun.jna.NativeString Maven / Gradle / Ivy

/* This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 

* This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. */ package com.sun.jna; /** Provides a temporary allocation of an immutable C string * (const char* or const wchar_t*) for use when * converting a Java String into a native memory function argument. * * @author Todd Fast, [email protected] * @author [email protected] */ class NativeString implements CharSequence, Comparable { private String value; private Pointer pointer; /** 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 charstring. * 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) { this.value = string; 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()). if (wide) { int len = (string.length() + 1 ) * Native.WCHAR_SIZE; pointer = new Memory(len); pointer.setString(0, string, true); } else { byte[] data = Native.getBytes(string); pointer = new Memory(data.length + 1); pointer.setString(0, string); } } public int hashCode() { return value.hashCode(); } public boolean equals(Object other) { if (other instanceof CharSequence) { return compareTo(other) == 0; } return false; } public String toString() { return value; } public Pointer getPointer() { return pointer; } public char charAt(int index) { return value.charAt(index); } public int length() { return value.length(); } public CharSequence subSequence(int start, int end) { return value.subSequence(start, end); } public int compareTo(Object other) { if (other == null) return 1; return value.compareTo(other.toString()); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy