/** Create a native string as a NUL-terminated array of <code>wchar_t</code> * (if <code>wide</code> is true) or <code>char</code>.<p> * If not <code>wide</code>, the encoding is obtained from {@link * Native#getDefaultStringEncoding()}. * * @param string value to write to native memory * @param wide whether to store the String as <code>wchar_t</code> */ public NativeString(String string, boolean wide) { this(string, wide ? WIDE_STRING : Native.getDefaultStringEncoding()); }
/** Create a native string (NUL-terminated array of <code>char</code>).<p> * Uses the encoding returned by {@link Native#getDefaultStringEncoding()}. */ public NativeString(String string) { this(string, Native.getDefaultStringEncoding()); }
/** Create a native array of strings. */ public StringArray(String[] strings, boolean wide) { this((Object[])strings, wide ? NativeString.WIDE_STRING : Native.getDefaultStringEncoding()); } /** Create a native array of strings using the given encoding. */
/** * @param s The string * @return A NUL-terminated byte buffer equivalent to the given String, * using the encoding returned by {@link #getDefaultStringEncoding()}. * @see #toByteArray(String, String) */ public static byte[] toByteArray(String s) { return toByteArray(s, getDefaultStringEncoding()); }
/** * Copy native memory to a Java String. The encoding used is obtained * form {@link Native#getDefaultStringEncoding()}. * * @param offset byte offset from pointer to start reading bytes * @return the <code>String</code> value being pointed to */ public String getString(long offset) { return getString(offset, Native.getDefaultStringEncoding()); }
/** * Obtain a Java String from the given native byte array. If there is * no NUL terminator, the String will comprise the entire array. The * encoding is obtained from {@link #getDefaultStringEncoding()}. * * @param buf The buffer containing the encoded bytes * @see #toString(byte[], String) */ public static String toString(byte[] buf) { return toString(buf, getDefaultStringEncoding()); }
/** * @param s The input string * @return A byte array corresponding to the given String. The encoding * used is obtained from {@link #getDefaultStringEncoding()}. */ static byte[] getBytes(String s) { return getBytes(s, getDefaultStringEncoding()); }
/** <p>Returns an array of <code>String</code> based on a native array * of <code>char *</code>. The array length is determined by a * NULL-valued terminating element. * </p> * The strings are decoded using the encoding returned by {@link * Native#getDefaultStringEncoding()}. */ public String[] getStringArray(long offset) { return getStringArray(offset, -1, Native.getDefaultStringEncoding()); }
/** <p>Returns an array of <code>String</code> based on a native array * of <code>char *</code>, using the given array length. * </p> * The strings are decoded using the encoding returned by {@link * Native#getDefaultStringEncoding()}. */ public String[] getStringArray(long offset, int length) { return getStringArray(offset, length, Native.getDefaultStringEncoding()); }
/** * @param cls The native interface type * @return The preferred string encoding for the given native interface. * If there is no setting, defaults to the {@link #getDefaultStringEncoding()}. * @see com.sun.jna.Library#OPTION_STRING_ENCODING */ public static String getStringEncoding(Class<?> cls) { Map<String, ?> options = getLibraryOptions(cls); String encoding = (String) options.get(Library.OPTION_STRING_ENCODING); return encoding != null ? encoding : getDefaultStringEncoding(); }
/** * Copy bytes out of string <code>value</code> to the location being * pointed to, using the encoding indicated by {@link * Native#getDefaultStringEncoding()}. * * @param offset byte offset from pointer at which characters in * <code>value</code> must be set * @param value <code>java.lang.String</code> value to set */ public void setString(long offset, String value) { setString(offset, value, Native.getDefaultStringEncoding()); }
static String getString(Pointer pointer, long offset) { return getString(pointer, offset, getDefaultStringEncoding()); }
/** * Create a new <code>Function</code> that is linked with a native * function that follows the given calling convention. * * <p>The allocated instance represents a pointer to the given * function address, called with the given calling * convention. * * @param functionAddress * Address of the native function * @param callFlags * Function <a href="#callflags">call flags</a> * @param encoding * Encoding for conversion between Java and native strings. */ Function(Pointer functionAddress, int callFlags, String encoding) { checkCallingConvention(callFlags & MASK_CC); if (functionAddress == null || functionAddress.peer == 0) { throw new NullPointerException("Function address may not be null"); } this.functionName = functionAddress.toString(); this.callFlags = callFlags; this.peer = functionAddress.peer; this.options = Collections.EMPTY_MAP; this.encoding = encoding != null ? encoding : Native.getDefaultStringEncoding(); }
private NativeLibrary(String libraryName, String libraryPath, long handle, Map<String, ?> options) { this.libraryName = getLibraryName(libraryName); this.libraryPath = libraryPath; this.handle = handle; Object option = options.get(Library.OPTION_CALLING_CONVENTION); int callingConvention = option instanceof Number ? ((Number)option).intValue() : Function.C_CONVENTION; this.callFlags = callingConvention; this.options = options; this.encoding = (String)options.get(Library.OPTION_STRING_ENCODING); if (this.encoding == null) { this.encoding = Native.getDefaultStringEncoding(); } // Special workaround for w32 kernel32.GetLastError // Short-circuit the function to use built-in GetLastError access if (Platform.isWindows() && "kernel32".equals(this.libraryName.toLowerCase())) { synchronized(functions) { Function f = new Function(this, "GetLastError", Function.ALT_CONVENTION, encoding) { @Override Object invoke(Object[] args, Class<?> returnType, boolean b, int fixedArgs) { return Integer.valueOf(Native.getLastError()); } @Override Object invoke(Method invokingMethod, Class<?>[] paramTypes, Class<?> returnType, Object[] inArgs, Map<String, ?> options) { return Integer.valueOf(Native.getLastError()); } }; functions.put(functionKey("GetLastError", callFlags, encoding), f); } } }
this.callFlags = callFlags; this.options = library.options; this.encoding = encoding != null ? encoding : Native.getDefaultStringEncoding(); try { this.peer = library.getSymbolAddress(functionName);
/** * List extended attributes on file. * * @param fd file handle * @return collection of extended attributes' names * @throws IOException on any error except <code>ERANGE</code> which handled internally */ public static Collection<String> fListXAttr(int fd) throws IOException { return fListXAttr(fd, Native.getDefaultStringEncoding()); }
/** * Set or replace value of extended attribute. * * @param fd file handle * @param name extended attribute name * @param value value to set * @throws IOException on any error */ public static void fSetXAttr(int fd, String name, String value) throws IOException { fSetXAttr(fd, name, value, Native.getDefaultStringEncoding()); }
/** * Get extended attribute value. * * @param path file path * @param name extended attribute name * @return extended attribute value * @throws IOException on any error except <code>ERANGE</code> which handled internally */ public static String getXAttr(String path, String name) throws IOException { return getXAttr(path, name, Native.getDefaultStringEncoding()); }
/** Returns an array of <code>String</code> based on a native array * of <code>char*</code> or <code>wchar_t*</code> based on the * <code>wide</code> parameter, using the given array length. * * @deprecated use {@link #getStringArray(long,int,String)} or {@link * #getWideStringArray(long,int)} instead. */ @Deprecated public String[] getStringArray(long offset, int length, boolean wide) { return getStringArray(offset, length, wide ? NativeString.WIDE_STRING : Native.getDefaultStringEncoding()); }
/** <p>Returns an array of <code>String</code> based on a native array * of <code>char *</code>, using the given array length. * </p> * The strings are decoded using the encoding returned by {@link * Native#getDefaultStringEncoding()}. */ public String[] getStringArray(long offset, int length) { return getStringArray(offset, length, Native.getDefaultStringEncoding()); }