PageRenderTime 67ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/src/com/sun/jna/Native.java

http://github.com/twall/jna
Java | 2414 lines | 1410 code | 203 blank | 801 comment | 337 complexity | 8c7944b63e7c7813d66fcf2b4782f8df MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0

Large files files are truncated, but you can click here to view the full file

  1. /* Copyright (c) 2007-2015 Timothy Wall, All Rights Reserved
  2. *
  3. * The contents of this file is dual-licensed under 2
  4. * alternative Open Source/Free licenses: LGPL 2.1 or later and
  5. * Apache License 2.0. (starting with JNA version 4.0.0).
  6. *
  7. * You can freely decide which license you want to apply to
  8. * the project.
  9. *
  10. * You may obtain a copy of the LGPL License at:
  11. *
  12. * http://www.gnu.org/licenses/licenses.html
  13. *
  14. * A copy is also included in the downloadable source code package
  15. * containing JNA, in file "LGPL2.1".
  16. *
  17. * You may obtain a copy of the Apache License at:
  18. *
  19. * http://www.apache.org/licenses/
  20. *
  21. * A copy is also included in the downloadable source code package
  22. * containing JNA, in file "AL2.0".
  23. */
  24. package com.sun.jna;
  25. import java.awt.Component;
  26. import java.awt.GraphicsEnvironment;
  27. import java.awt.HeadlessException;
  28. import java.awt.Window;
  29. import java.io.File;
  30. import java.io.FileOutputStream;
  31. import java.io.FilenameFilter;
  32. import java.io.IOException;
  33. import java.io.InputStream;
  34. import java.io.UnsupportedEncodingException;
  35. import java.lang.ref.Reference;
  36. import java.lang.ref.WeakReference;
  37. import java.lang.reflect.Array;
  38. import java.lang.reflect.Field;
  39. import java.lang.reflect.InvocationHandler;
  40. import java.lang.reflect.Method;
  41. import java.lang.reflect.Modifier;
  42. import java.lang.reflect.Proxy;
  43. import java.net.URI;
  44. import java.net.URISyntaxException;
  45. import java.net.URL;
  46. import java.net.URLClassLoader;
  47. import java.nio.Buffer;
  48. import java.nio.ByteBuffer;
  49. import java.nio.charset.Charset;
  50. import java.nio.charset.IllegalCharsetNameException;
  51. import java.nio.charset.UnsupportedCharsetException;
  52. import java.security.AccessController;
  53. import java.security.PrivilegedAction;
  54. import java.util.ArrayList;
  55. import java.util.Arrays;
  56. import java.util.Collections;
  57. import java.util.HashMap;
  58. import java.util.List;
  59. import java.util.Map;
  60. import java.util.StringTokenizer;
  61. import java.util.WeakHashMap;
  62. import com.sun.jna.Callback.UncaughtExceptionHandler;
  63. import com.sun.jna.Structure.FFIType;
  64. import java.util.logging.Level;
  65. import java.util.logging.Logger;
  66. /** Provides generation of invocation plumbing for a defined native
  67. * library interface. Also provides various utilities for native operations.
  68. * <p>
  69. * {@link #getTypeMapper} and {@link #getStructureAlignment} are provided
  70. * to avoid having to explicitly pass these parameters to {@link Structure}s,
  71. * which would require every {@link Structure} which requires custom mapping
  72. * or alignment to define a constructor and pass parameters to the superclass.
  73. * To avoid lots of boilerplate, the base {@link Structure} constructor
  74. * figures out these properties based on its enclosing interface.<p>
  75. * <a name=library_loading></a>
  76. * <h2>Library Loading</h2>
  77. * <p>When JNA classes are loaded, the native shared library (jnidispatch) is
  78. * loaded as well. An attempt is made to load it from the any paths defined
  79. * in <code>jna.boot.library.path</code> (if defined), then the system library
  80. * path using {@link System#loadLibrary}, unless <code>jna.nosys=true</code>.
  81. * If not found, the appropriate library will be extracted from the class path
  82. * (into a temporary directory if found within a jar file) and loaded from
  83. * there, unless <code>jna.noclasspath=true</code>. If your system has
  84. * additional security constraints regarding execution or load of files
  85. * (SELinux, for example), you should probably install the native library in
  86. * an accessible location and configure your system accordingly, rather than
  87. * relying on JNA to extract the library from its own jar file.</p>
  88. * <p>To avoid the automatic unpacking (in situations where you want to force a
  89. * failure if the JNA native library is not properly installed on the system),
  90. * set the system property <code>jna.nounpack=true</code>.
  91. * </p>
  92. * <p>While this class and its corresponding native library are loaded, the
  93. * system property <code>jna.loaded</code> will be set. The property will be
  94. * cleared when native support has been unloaded (i.e. the Native class and
  95. * its underlying native support has been GC'd).</p>
  96. * <p>NOTE: all native functions are provided within this class to ensure that
  97. * all other JNA-provided classes and objects are GC'd and/or
  98. * finalized/disposed before this class is disposed and/or removed from
  99. * memory (most notably Memory and any other class which by default frees its
  100. * resources in a finalizer).</p>
  101. * <a name=native_library_loading></a>
  102. * <h2>Native Library Loading</h2>
  103. * Native libraries loaded via {@link #load(Class)} may be found in
  104. * <a href="NativeLibrary.html#library_search_paths">several locations</a>.
  105. * @see Library
  106. * @author Todd Fast, todd.fast@sun.com
  107. * @author twall@users.sf.net
  108. */
  109. public final class Native implements Version {
  110. private static final Logger LOG = Logger.getLogger(Native.class.getName());
  111. public static final Charset DEFAULT_CHARSET;
  112. public static final String DEFAULT_ENCODING;
  113. static {
  114. // JNA used the defaultCharset to determine which encoding to use when
  115. // converting strings to native char*. The defaultCharset is set from
  116. // the system property file.encoding. Up to JDK 17 its value defaulted
  117. // to the system default encoding. From JDK 18 onwards its default value
  118. // changed to UTF-8.
  119. // JDK 18+ exposes the native encoding as the new system property
  120. // native.encoding, prior versions don't have that property and will
  121. // report NULL for it.
  122. // The algorithm is simple: If native.encoding is set, it will be used
  123. // else the original implementation of Charset#defaultCharset is used
  124. String nativeEncoding = System.getProperty("native.encoding");
  125. Charset nativeCharset = null;
  126. if (nativeEncoding != null) {
  127. try {
  128. nativeCharset = Charset.forName(nativeEncoding);
  129. } catch (Exception ex) {
  130. LOG.log(Level.WARNING, "Failed to get charset for native.encoding value : '" + nativeEncoding + "'", ex);
  131. }
  132. }
  133. if (nativeCharset == null) {
  134. nativeCharset = Charset.defaultCharset();
  135. }
  136. DEFAULT_CHARSET = nativeCharset;
  137. DEFAULT_ENCODING = nativeCharset.name();
  138. }
  139. public static final boolean DEBUG_LOAD = Boolean.getBoolean("jna.debug_load");
  140. public static final boolean DEBUG_JNA_LOAD = Boolean.getBoolean("jna.debug_load.jna");
  141. private final static Level DEBUG_JNA_LOAD_LEVEL = DEBUG_JNA_LOAD ? Level.INFO : Level.FINE;
  142. // Used by tests, do not remove
  143. static String jnidispatchPath = null;
  144. private static final Map<Class<?>, Map<String, Object>> typeOptions = Collections.synchronizedMap(new WeakHashMap<Class<?>, Map<String, Object>>());
  145. private static final Map<Class<?>, Reference<?>> libraries = Collections.synchronizedMap(new WeakHashMap<Class<?>, Reference<?>>());
  146. private static final String _OPTION_ENCLOSING_LIBRARY = "enclosing-library";
  147. private static final UncaughtExceptionHandler DEFAULT_HANDLER =
  148. new UncaughtExceptionHandler() {
  149. @Override
  150. public void uncaughtException(Callback c, Throwable e) {
  151. LOG.log(Level.WARNING, "JNA: Callback " + c + " threw the following exception", e);
  152. }
  153. };
  154. private static UncaughtExceptionHandler callbackExceptionHandler = DEFAULT_HANDLER;
  155. /** The size of a native pointer (<code>void*</code>) on the current
  156. * platform, in bytes.
  157. */
  158. public static final int POINTER_SIZE;
  159. /** Size of a native <code>long</code> type, in bytes. */
  160. public static final int LONG_SIZE;
  161. /** Size of a native <code>wchar_t</code> type, in bytes. */
  162. public static final int WCHAR_SIZE;
  163. /** Size of a native <code>size_t</code> type, in bytes. */
  164. public static final int SIZE_T_SIZE;
  165. /** Size of a native <code>bool</code> type (C99 and later), in bytes. */
  166. public static final int BOOL_SIZE;
  167. /** Size of a native <code>long double</code> type (C99 and later), in bytes. */
  168. public static final int LONG_DOUBLE_SIZE;
  169. private static final int TYPE_VOIDP = 0;
  170. private static final int TYPE_LONG = 1;
  171. private static final int TYPE_WCHAR_T = 2;
  172. private static final int TYPE_SIZE_T = 3;
  173. private static final int TYPE_BOOL = 4;
  174. private static final int TYPE_LONG_DOUBLE = 5;
  175. static final int MAX_ALIGNMENT;
  176. static final int MAX_PADDING;
  177. /**
  178. * Version string must have the structure <major>.<minor>.<revision>
  179. * a bugfix change in the native code increments revision, the minor is
  180. * incremented for backwards compatible changes and the major version
  181. * is changed for backwards incompatbile changes.
  182. *
  183. * @param expectedVersion
  184. * @param nativeVersion
  185. * @return true if nativeVersion describes a version compatible to expectedVersion
  186. */
  187. static boolean isCompatibleVersion(String expectedVersion, String nativeVersion) {
  188. String[] expectedVersionParts = expectedVersion.split("\\.");
  189. String[] nativeVersionParts = nativeVersion.split("\\.");
  190. if(expectedVersionParts.length < 3 || nativeVersionParts.length < 3) {
  191. return false;
  192. }
  193. int expectedMajor = Integer.parseInt(expectedVersionParts[0]);
  194. int nativeMajor = Integer.parseInt(nativeVersionParts[0]);
  195. int expectedMinor = Integer.parseInt(expectedVersionParts[1]);
  196. int nativeMinor = Integer.parseInt(nativeVersionParts[1]);
  197. if(expectedMajor != nativeMajor) {
  198. return false;
  199. }
  200. if(expectedMinor > nativeMinor) {
  201. return false;
  202. }
  203. return true;
  204. }
  205. static {
  206. loadNativeDispatchLibrary();
  207. if (! isCompatibleVersion(VERSION_NATIVE, getNativeVersion())) {
  208. String LS = System.getProperty("line.separator");
  209. throw new Error(LS + LS
  210. + "There is an incompatible JNA native library installed on this system" + LS
  211. + "Expected: " + VERSION_NATIVE + LS
  212. + "Found: " + getNativeVersion() + LS
  213. + (jnidispatchPath != null
  214. ? "(at " + jnidispatchPath + ")" : System.getProperty("java.library.path"))
  215. + "." + LS
  216. + "To resolve this issue you may do one of the following:" + LS
  217. + " - remove or uninstall the offending library" + LS
  218. + " - set the system property jna.nosys=true" + LS
  219. + " - set jna.boot.library.path to include the path to the version of the " + LS
  220. + " jnidispatch library included with the JNA jar file you are using" + LS);
  221. }
  222. POINTER_SIZE = sizeof(TYPE_VOIDP);
  223. LONG_SIZE = sizeof(TYPE_LONG);
  224. WCHAR_SIZE = sizeof(TYPE_WCHAR_T);
  225. SIZE_T_SIZE = sizeof(TYPE_SIZE_T);
  226. BOOL_SIZE = sizeof(TYPE_BOOL);
  227. LONG_DOUBLE_SIZE = sizeof(TYPE_LONG_DOUBLE);
  228. // Perform initialization of other JNA classes until *after*
  229. // initializing the above final fields
  230. initIDs();
  231. if (Boolean.getBoolean("jna.protected")) {
  232. setProtected(true);
  233. }
  234. MAX_ALIGNMENT = Platform.isSPARC() || Platform.isWindows()
  235. || (Platform.isLinux() && (Platform.isARM() || Platform.isPPC() || Platform.isMIPS()))
  236. || Platform.isAIX()
  237. || (Platform.isAndroid() && !Platform.isIntel())
  238. ? 8 : LONG_SIZE;
  239. MAX_PADDING = (Platform.isMac() && Platform.isPPC()) ? 8 : MAX_ALIGNMENT;
  240. System.setProperty("jna.loaded", "true");
  241. }
  242. /** Force a dispose when the Native class is GC'd. */
  243. private static final Object finalizer = new Object() {
  244. @Override
  245. protected void finalize() throws Throwable {
  246. dispose();
  247. super.finalize();
  248. }
  249. };
  250. /** Properly dispose of JNA functionality.
  251. Called when this class is finalized and also from JNI when
  252. JNA's native shared library is unloaded.
  253. */
  254. private static void dispose() {
  255. CallbackReference.disposeAll();
  256. Memory.disposeAll();
  257. NativeLibrary.disposeAll();
  258. unregisterAll();
  259. jnidispatchPath = null;
  260. System.setProperty("jna.loaded", "false");
  261. }
  262. /** Remove any automatically unpacked native library.
  263. This will fail on windows, which disallows removal of any file that is
  264. still in use, so an alternative is required in that case. Mark
  265. the file that could not be deleted, and attempt to delete any
  266. temporaries on next startup.
  267. Do NOT force the class loader to unload the native library, since
  268. that introduces issues with cleaning up any extant JNA bits
  269. (e.g. Memory) which may still need use of the library before shutdown.
  270. */
  271. static boolean deleteLibrary(File lib) {
  272. if (lib.delete()) {
  273. return true;
  274. }
  275. // Couldn't delete it, mark for later deletion
  276. markTemporaryFile(lib);
  277. return false;
  278. }
  279. private Native() { }
  280. private static native void initIDs();
  281. /** Set whether native memory accesses are protected from invalid
  282. * accesses. This should only be set true when testing or debugging,
  283. * and should not be considered reliable or robust for applications
  284. * where JNA native calls are occurring on multiple threads.
  285. * Protected mode will be automatically set if the
  286. * system property <code>jna.protected</code> has a value of "true"
  287. * when the JNA library is first loaded.<p>
  288. * If not supported by the underlying platform, this setting will
  289. * have no effect.<p>
  290. * NOTE: On platforms which support signals (non-Windows), JNA uses
  291. * signals to trap errors. This may interfere with the JVM's own use of
  292. * signals. When protected mode is enabled, you should make use of the
  293. * jsig library, if available (see <a href="http://download.oracle.com/javase/6/docs/technotes/guides/vm/signal-chaining.html">Signal Chaining</a>).
  294. * In short, set the environment variable <code>LD_PRELOAD</code> to the
  295. * path to <code>libjsig.so</code> in your JRE lib directory
  296. * (usually ${java.home}/lib/${os.arch}/libjsig.so) before launching your
  297. * Java application.
  298. */
  299. public static synchronized native void setProtected(boolean enable);
  300. /** Returns whether protection is enabled. Check the result of this method
  301. * after calling {@link #setProtected setProtected(true)} to determine
  302. * if this platform supports protecting memory accesses.
  303. */
  304. public static synchronized native boolean isProtected();
  305. /** Utility method to get the native window ID for a Java {@link Window}
  306. * as a <code>long</code> value.
  307. * This method is primarily for X11-based systems, which use an opaque
  308. * <code>XID</code> (usually <code>long int</code>) to identify windows.
  309. * @throws HeadlessException if the current VM is running headless
  310. */
  311. public static long getWindowID(Window w) throws HeadlessException {
  312. return AWT.getWindowID(w);
  313. }
  314. /** Utility method to get the native window ID for a heavyweight Java
  315. * {@link Component} as a <code>long</code> value.
  316. * This method is primarily for X11-based systems, which use an opaque
  317. * <code>XID</code> (usually <code>long int</code>) to identify windows.
  318. * @throws HeadlessException if the current VM is running headless
  319. */
  320. public static long getComponentID(Component c) throws HeadlessException {
  321. return AWT.getComponentID(c);
  322. }
  323. /** Utility method to get the native window pointer for a Java
  324. * {@link Window} as a {@link Pointer} value. This method is primarily for
  325. * w32, which uses the <code>HANDLE</code> type (actually
  326. * <code>void *</code>) to identify windows.
  327. * @throws HeadlessException if the current VM is running headless
  328. */
  329. public static Pointer getWindowPointer(Window w) throws HeadlessException {
  330. return new Pointer(AWT.getWindowID(w));
  331. }
  332. /** Utility method to get the native window pointer for a heavyweight Java
  333. * {@link Component} as a {@link Pointer} value. This method is primarily
  334. * for w32, which uses the <code>HWND</code> type (actually
  335. * <code>void *</code>) to identify windows.
  336. * @throws HeadlessException if the current VM is running headless
  337. */
  338. public static Pointer getComponentPointer(Component c) throws HeadlessException {
  339. return new Pointer(AWT.getComponentID(c));
  340. }
  341. static native long getWindowHandle0(Component c);
  342. /** Convert a direct {@link Buffer} into a {@link Pointer}.
  343. * @throws IllegalArgumentException if the buffer is not direct.
  344. */
  345. public static Pointer getDirectBufferPointer(Buffer b) {
  346. long peer = _getDirectBufferPointer(b);
  347. return peer == 0 ? null : new Pointer(peer);
  348. }
  349. private static native long _getDirectBufferPointer(Buffer b);
  350. /**
  351. * Gets the charset belonging to the given {@code encoding}.
  352. * @param encoding The encoding - if {@code null} then the default platform
  353. * encoding is used.
  354. * @return The charset belonging to the given {@code encoding} or the platform default.
  355. * Never {@code null}.
  356. */
  357. private static Charset getCharset(String encoding) {
  358. Charset charset = null;
  359. if (encoding != null) {
  360. try {
  361. charset = Charset.forName(encoding);
  362. }
  363. catch(IllegalCharsetNameException e) {
  364. LOG.log(Level.WARNING, "JNA Warning: Encoding ''{0}'' is unsupported ({1})",
  365. new Object[]{encoding, e.getMessage()});
  366. }
  367. catch(UnsupportedCharsetException e) {
  368. LOG.log(Level.WARNING, "JNA Warning: Encoding ''{0}'' is unsupported ({1})",
  369. new Object[]{encoding, e.getMessage()});
  370. }
  371. }
  372. if (charset == null) {
  373. LOG.log(Level.WARNING, "JNA Warning: Using fallback encoding {0}", Native.DEFAULT_CHARSET);
  374. charset = Native.DEFAULT_CHARSET;
  375. }
  376. return charset;
  377. }
  378. /**
  379. * Obtain a Java String from the given native byte array. If there is
  380. * no NUL terminator, the String will comprise the entire array. The
  381. * encoding is obtained from {@link #getDefaultStringEncoding()}.
  382. *
  383. * @param buf The buffer containing the encoded bytes
  384. * @see #toString(byte[], String)
  385. */
  386. public static String toString(byte[] buf) {
  387. return toString(buf, getDefaultStringEncoding());
  388. }
  389. /**
  390. * Obtain a Java String from the given native byte array, using the given
  391. * encoding. If there is no NUL terminator, the String will comprise the
  392. * entire array.
  393. *
  394. * <p><strong>Usage note</strong>: This function assumes, that {@code buf}
  395. * holds a {@code char} array. This means only single-byte encodings are
  396. * supported.</p>
  397. *
  398. * @param buf The buffer containing the encoded bytes. Must not be {@code null}.
  399. * @param encoding The encoding name - if {@code null} then the platform
  400. * default encoding will be used
  401. */
  402. public static String toString(byte[] buf, String encoding) {
  403. return Native.toString(buf, Native.getCharset(encoding));
  404. }
  405. /**
  406. * Obtain a Java String from the given native byte array, using the given
  407. * encoding. If there is no NUL terminator, the String will comprise the
  408. * entire array.
  409. *
  410. * <p><strong>Usage note</strong>: This function assumes, that {@code buf}
  411. * holds a {@code char} array. This means only single-byte encodings are
  412. * supported.</p>
  413. *
  414. * @param buf The buffer containing the encoded bytes. Must not be {@code null}.
  415. * @param charset The charset to decode {@code buf}. Must not be {@code null}.
  416. */
  417. public static String toString(byte[] buf, Charset charset) {
  418. int len = buf.length;
  419. // find out the effective length
  420. for (int index = 0; index < len; index++) {
  421. if (buf[index] == 0) {
  422. len = index;
  423. break;
  424. }
  425. }
  426. if (len == 0) {
  427. return "";
  428. }
  429. return new String(buf, 0, len, charset);
  430. }
  431. /**
  432. * Obtain a Java String from the given native wchar_t array. If there is
  433. * no NUL terminator, the String will comprise the entire array.
  434. *
  435. * @param buf The buffer containing the characters
  436. */
  437. public static String toString(char[] buf) {
  438. int len = buf.length;
  439. for (int index = 0; index < len; index++) {
  440. if (buf[index] == '\0') {
  441. len = index;
  442. break;
  443. }
  444. }
  445. if (len == 0) {
  446. return "";
  447. } else {
  448. return new String(buf, 0, len);
  449. }
  450. }
  451. /**
  452. * Converts a &quot;list&quot; of strings each null terminated
  453. * into a {@link List} of {@link String} values. The end of the
  454. * list is signaled by an extra NULL value at the end or by the
  455. * end of the buffer.
  456. * @param buf The buffer containing the strings
  457. * @return A {@link List} of all the strings in the buffer
  458. * @see #toStringList(char[], int, int)
  459. */
  460. public static List<String> toStringList(char[] buf) {
  461. return toStringList(buf, 0, buf.length);
  462. }
  463. /**
  464. * Converts a &quot;list&quot; of strings each null terminated
  465. * into a {@link List} of {@link String} values. The end of the
  466. * list is signaled by an extra NULL value at the end or by the
  467. * end of the data.
  468. * @param buf The buffer containing the strings
  469. * @param offset Offset to start parsing
  470. * @param len The total characters to parse
  471. * @return A {@link List} of all the strings in the buffer
  472. */
  473. public static List<String> toStringList(char[] buf, int offset, int len) {
  474. List<String> list = new ArrayList<String>();
  475. int lastPos = offset;
  476. int maxPos = offset + len;
  477. for (int curPos = offset; curPos < maxPos; curPos++) {
  478. if (buf[curPos] != '\0') {
  479. continue;
  480. }
  481. // check if found the extra null terminator
  482. if (lastPos == curPos) {
  483. return list;
  484. }
  485. String value = new String(buf, lastPos, curPos - lastPos);
  486. list.add(value);
  487. lastPos = curPos + 1; // skip the '\0'
  488. }
  489. // This point is reached if there is no double null terminator
  490. if (lastPos < maxPos) {
  491. String value = new String(buf, lastPos, maxPos - lastPos);
  492. list.add(value);
  493. }
  494. return list;
  495. }
  496. /** Map a library interface to the current process, providing
  497. * the explicit interface class.
  498. * Native libraries loaded via this method may be found in
  499. * <a href="NativeLibrary.html#library_search_paths">several locations</a>.
  500. * @param <T> Type of expected wrapper
  501. * @param interfaceClass The implementation wrapper interface
  502. * @return an instance of the requested interface, mapped to the current
  503. * process.
  504. * @throws UnsatisfiedLinkError if the library cannot be found or
  505. * dependent libraries are missing.
  506. */
  507. public static <T extends Library> T load(Class<T> interfaceClass) {
  508. return load(null, interfaceClass);
  509. }
  510. /** Map a library interface to the current process, providing
  511. * the explicit interface class. Any options provided for the library are
  512. * cached and associated with the library and any of its defined
  513. * structures and/or functions.
  514. * Native libraries loaded via this method may be found in
  515. * <a href="NativeLibrary.html#library_search_paths">several locations</a>.
  516. * @param <T> Type of expected wrapper
  517. * @param interfaceClass The implementation wrapper interface
  518. * @param options Map of library options
  519. * @return an instance of the requested interface, mapped to the current
  520. * process.
  521. * @throws UnsatisfiedLinkError if the library cannot be found or
  522. * dependent libraries are missing.
  523. * @see #load(String, Class, Map)
  524. */
  525. public static <T extends Library> T load(Class<T> interfaceClass, Map<String, ?> options) {
  526. return load(null, interfaceClass, options);
  527. }
  528. /** Map a library interface to the given shared library, providing
  529. * the explicit interface class.
  530. * If <code>name</code> is null, attempts to map onto the current process.
  531. * Native libraries loaded via this method may be found in
  532. * <a href="NativeLibrary.html#library_search_paths">several locations</a>.
  533. * @param <T> Type of expected wrapper
  534. * @param name Library base name
  535. * @param interfaceClass The implementation wrapper interface
  536. * @return an instance of the requested interface, mapped to the indicated
  537. * native library.
  538. * @throws UnsatisfiedLinkError if the library cannot be found or
  539. * dependent libraries are missing.
  540. * @see #load(String, Class, Map)
  541. */
  542. public static <T extends Library> T load(String name, Class<T> interfaceClass) {
  543. return load(name, interfaceClass, Collections.<String, Object>emptyMap());
  544. }
  545. /** Load a library interface from the given shared library, providing
  546. * the explicit interface class and a map of options for the library.
  547. * If no library options are detected the map is interpreted as a map
  548. * of Java method names to native function names.<p>
  549. * If <code>name</code> is null, attempts to map onto the current process.
  550. * Native libraries loaded via this method may be found in
  551. * <a href="NativeLibrary.html#library_search_paths">several locations</a>.
  552. * @param <T> Type of expected wrapper
  553. * @param name Library base name
  554. * @param interfaceClass The implementation wrapper interface
  555. * @param options Map of library options
  556. * @return an instance of the requested interface, mapped to the indicated
  557. * native library.
  558. * @throws UnsatisfiedLinkError if the library cannot be found or
  559. * dependent libraries are missing.
  560. */
  561. public static <T extends Library> T load(String name, Class<T> interfaceClass, Map<String, ?> options) {
  562. if (!Library.class.isAssignableFrom(interfaceClass)) {
  563. // Maybe still possible if the caller is not using generics?
  564. throw new IllegalArgumentException("Interface (" + interfaceClass.getSimpleName() + ")"
  565. + " of library=" + name + " does not extend " + Library.class.getSimpleName());
  566. }
  567. Library.Handler handler = new Library.Handler(name, interfaceClass, options);
  568. ClassLoader loader = interfaceClass.getClassLoader();
  569. Object proxy = Proxy.newProxyInstance(loader, new Class[] {interfaceClass}, handler);
  570. cacheOptions(interfaceClass, options, proxy);
  571. return interfaceClass.cast(proxy);
  572. }
  573. /**
  574. * Provided for improved compatibility between JNA 4.X and 5.X
  575. *
  576. * @see Native#load(java.lang.Class)
  577. */
  578. @Deprecated
  579. public static <T> T loadLibrary(Class<T> interfaceClass) {
  580. return loadLibrary(null, interfaceClass);
  581. }
  582. /**
  583. * Provided for improved compatibility between JNA 4.X and 5.X
  584. *
  585. * @see Native#load(java.lang.Class, java.util.Map)
  586. */
  587. @Deprecated
  588. public static <T> T loadLibrary(Class<T> interfaceClass, Map<String, ?> options) {
  589. return loadLibrary(null, interfaceClass, options);
  590. }
  591. /**
  592. * Provided for improved compatibility between JNA 4.X and 5.X
  593. *
  594. * @see Native#load(java.lang.String, java.lang.Class)
  595. */
  596. @Deprecated
  597. public static <T> T loadLibrary(String name, Class<T> interfaceClass) {
  598. return loadLibrary(name, interfaceClass, Collections.<String, Object>emptyMap());
  599. }
  600. /**
  601. * Provided for improved compatibility between JNA 4.X and 5.X
  602. *
  603. * @see Native#load(java.lang.String, java.lang.Class, java.util.Map)
  604. */
  605. @Deprecated
  606. public static <T> T loadLibrary(String name, Class<T> interfaceClass, Map<String, ?> options) {
  607. if (!Library.class.isAssignableFrom(interfaceClass)) {
  608. // Maybe still possible if the caller is not using generics?
  609. throw new IllegalArgumentException("Interface (" + interfaceClass.getSimpleName() + ")"
  610. + " of library=" + name + " does not extend " + Library.class.getSimpleName());
  611. }
  612. Library.Handler handler = new Library.Handler(name, interfaceClass, options);
  613. ClassLoader loader = interfaceClass.getClassLoader();
  614. Object proxy = Proxy.newProxyInstance(loader, new Class[] {interfaceClass}, handler);
  615. cacheOptions(interfaceClass, options, proxy);
  616. return interfaceClass.cast(proxy);
  617. }
  618. /** Attempts to force initialization of an instance of the library interface
  619. * by loading a public static field of the requisite type.
  620. * Returns whether an instance variable was instantiated.
  621. * Expects that lock on libraries is already held
  622. */
  623. private static void loadLibraryInstance(Class<?> cls) {
  624. if (cls != null && !libraries.containsKey(cls)) {
  625. try {
  626. Field[] fields = cls.getFields();
  627. for (int i=0;i < fields.length;i++) {
  628. Field field = fields[i];
  629. if (field.getType() == cls
  630. && Modifier.isStatic(field.getModifiers())) {
  631. // Ensure the field gets initialized by reading it
  632. field.setAccessible(true); // interface might be private
  633. libraries.put(cls, new WeakReference<Object>(field.get(null)));
  634. break;
  635. }
  636. }
  637. }
  638. catch (Exception e) {
  639. throw new IllegalArgumentException("Could not access instance of "
  640. + cls + " (" + e + ")");
  641. }
  642. }
  643. }
  644. /**
  645. * Find the library interface corresponding to the given class. Checks
  646. * all ancestor classes and interfaces for a declaring class which
  647. * implements {@link Library}.
  648. * @param cls The given class
  649. * @return The enclosing class
  650. */
  651. static Class<?> findEnclosingLibraryClass(Class<?> cls) {
  652. if (cls == null) {
  653. return null;
  654. }
  655. // Check for direct-mapped libraries, which won't necessarily
  656. // implement com.sun.jna.Library.
  657. Map<String, ?> libOptions = typeOptions.get(cls);
  658. if (libOptions != null) {
  659. Class<?> enclosingClass = (Class<?>)libOptions.get(_OPTION_ENCLOSING_LIBRARY);
  660. if (enclosingClass != null) {
  661. return enclosingClass;
  662. }
  663. return cls;
  664. }
  665. if (Library.class.isAssignableFrom(cls)) {
  666. return cls;
  667. }
  668. if (Callback.class.isAssignableFrom(cls)) {
  669. cls = CallbackReference.findCallbackClass(cls);
  670. }
  671. Class<?> declaring = cls.getDeclaringClass();
  672. Class<?> fromDeclaring = findEnclosingLibraryClass(declaring);
  673. if (fromDeclaring != null) {
  674. return fromDeclaring;
  675. }
  676. return findEnclosingLibraryClass(cls.getSuperclass());
  677. }
  678. /** Return the preferred native library configuration options for the given
  679. * class. First attempts to load any field of the interface type within
  680. * the interface mapping, then checks the cache for any specified library
  681. * options. If none found, a set of library options will be generated
  682. * from the fields (by order of precedence) <code>OPTIONS</code> (a {@link
  683. * Map}), <code>TYPE_MAPPER</code> (a {@link TypeMapper}),
  684. * <code>STRUCTURE_ALIGNMENT</code> (an {@link Integer}), and
  685. * <code>STRING_ENCODING</code> (a {@link String}).
  686. *
  687. * @param type The type class
  688. * @return The options map
  689. */
  690. public static Map<String, Object> getLibraryOptions(Class<?> type) {
  691. Map<String, Object> libraryOptions;
  692. // cached already ?
  693. libraryOptions = typeOptions.get(type);
  694. if (libraryOptions != null) {
  695. return libraryOptions;
  696. }
  697. Class<?> mappingClass = findEnclosingLibraryClass(type);
  698. if (mappingClass != null) {
  699. loadLibraryInstance(mappingClass);
  700. } else {
  701. mappingClass = type;
  702. }
  703. libraryOptions = typeOptions.get(mappingClass);
  704. if (libraryOptions != null) {
  705. typeOptions.put(type, libraryOptions); // cache for next time
  706. return libraryOptions;
  707. }
  708. try {
  709. Field field = mappingClass.getField("OPTIONS");
  710. field.setAccessible(true);
  711. libraryOptions = (Map<String, Object>) field.get(null);
  712. if (libraryOptions == null) {
  713. throw new IllegalStateException("Null options field");
  714. }
  715. } catch (NoSuchFieldException e) {
  716. libraryOptions = Collections.<String, Object>emptyMap();
  717. } catch (Exception e) {
  718. throw new IllegalArgumentException("OPTIONS must be a public field of type java.util.Map (" + e + "): " + mappingClass);
  719. }
  720. // Make a clone of the original options
  721. libraryOptions = new HashMap<String, Object>(libraryOptions);
  722. if (!libraryOptions.containsKey(Library.OPTION_TYPE_MAPPER)) {
  723. libraryOptions.put(Library.OPTION_TYPE_MAPPER, lookupField(mappingClass, "TYPE_MAPPER", TypeMapper.class));
  724. }
  725. if (!libraryOptions.containsKey(Library.OPTION_STRUCTURE_ALIGNMENT)) {
  726. libraryOptions.put(Library.OPTION_STRUCTURE_ALIGNMENT, lookupField(mappingClass, "STRUCTURE_ALIGNMENT", Integer.class));
  727. }
  728. if (!libraryOptions.containsKey(Library.OPTION_STRING_ENCODING)) {
  729. libraryOptions.put(Library.OPTION_STRING_ENCODING, lookupField(mappingClass, "STRING_ENCODING", String.class));
  730. }
  731. libraryOptions = cacheOptions(mappingClass, libraryOptions, null);
  732. // Store the original lookup class, if different from the mapping class
  733. if (type != mappingClass) {
  734. typeOptions.put(type, libraryOptions);
  735. }
  736. return libraryOptions;
  737. }
  738. private static Object lookupField(Class<?> mappingClass, String fieldName, Class<?> resultClass) {
  739. try {
  740. Field field = mappingClass.getField(fieldName);
  741. field.setAccessible(true);
  742. return field.get(null);
  743. }
  744. catch (NoSuchFieldException e) {
  745. return null;
  746. }
  747. catch (Exception e) {
  748. throw new IllegalArgumentException(fieldName + " must be a public field of type "
  749. + resultClass.getName() + " ("
  750. + e + "): " + mappingClass);
  751. }
  752. }
  753. /** Return the preferred {@link TypeMapper} for the given native interface.
  754. * See {@link com.sun.jna.Library#OPTION_TYPE_MAPPER}.
  755. */
  756. public static TypeMapper getTypeMapper(Class<?> cls) {
  757. Map<String, ?> options = getLibraryOptions(cls);
  758. return (TypeMapper) options.get(Library.OPTION_TYPE_MAPPER);
  759. }
  760. /**
  761. * @param cls The native interface type
  762. * @return The preferred string encoding for the given native interface.
  763. * If there is no setting, defaults to the {@link #getDefaultStringEncoding()}.
  764. * @see com.sun.jna.Library#OPTION_STRING_ENCODING
  765. */
  766. public static String getStringEncoding(Class<?> cls) {
  767. Map<String, ?> options = getLibraryOptions(cls);
  768. String encoding = (String) options.get(Library.OPTION_STRING_ENCODING);
  769. return encoding != null ? encoding : getDefaultStringEncoding();
  770. }
  771. /**
  772. * @return The default string encoding. Returns the value of the system
  773. * property <code>jna.encoding</code> or {@link Native#DEFAULT_ENCODING}.
  774. */
  775. public static String getDefaultStringEncoding() {
  776. return System.getProperty("jna.encoding", DEFAULT_ENCODING);
  777. }
  778. /**
  779. * @param cls The native interface type
  780. * @return The preferred structure alignment for the given native interface.
  781. * @see com.sun.jna.Library#OPTION_STRUCTURE_ALIGNMENT
  782. */
  783. public static int getStructureAlignment(Class<?> cls) {
  784. Integer alignment = (Integer)getLibraryOptions(cls).get(Library.OPTION_STRUCTURE_ALIGNMENT);
  785. return alignment == null ? Structure.ALIGN_DEFAULT : alignment;
  786. }
  787. /**
  788. * @param s The input string
  789. * @return A byte array corresponding to the given String. The encoding
  790. * used is obtained from {@link #getDefaultStringEncoding()}.
  791. */
  792. static byte[] getBytes(String s) {
  793. return getBytes(s, getDefaultStringEncoding());
  794. }
  795. /**
  796. * @param s The string. Must not be {@code null}.
  797. * @param encoding The encoding - if {@code null} then the default platform
  798. * encoding is used
  799. * @return A byte array corresponding to the given String, using the given
  800. * encoding. If the encoding is not found default to the platform native
  801. * encoding.
  802. */
  803. static byte[] getBytes(String s, String encoding) {
  804. return Native.getBytes(s, Native.getCharset(encoding));
  805. }
  806. /**
  807. * @param s The string. Must not be {@code null}.
  808. * @param charset The charset used to encode {@code s}. Must not be {@code null}.
  809. * @return A byte array corresponding to the given String, using the given
  810. * charset.
  811. */
  812. static byte[] getBytes(String s, Charset charset) {
  813. return s.getBytes(charset);
  814. }
  815. /**
  816. * @param s The string
  817. * @return A NUL-terminated byte buffer equivalent to the given String,
  818. * using the encoding returned by {@link #getDefaultStringEncoding()}.
  819. * @see #toByteArray(String, String)
  820. */
  821. public static byte[] toByteArray(String s) {
  822. return toByteArray(s, getDefaultStringEncoding());
  823. }
  824. /**
  825. * @param s The string. Must not be {@code null}.
  826. * @param encoding The encoding - if {@code null} then the default platform
  827. * encoding is used
  828. * @return A NUL-terminated byte buffer equivalent to the given String,
  829. * using the given encoding.
  830. * @see #getBytes(String, String)
  831. */
  832. public static byte[] toByteArray(String s, String encoding) {
  833. return Native.toByteArray(s, Native.getCharset(encoding));
  834. }
  835. /**
  836. * @param s The string. Must not be {@code null}.
  837. * @param charset The charset used to encode {@code s}. Must not be {@code null}.
  838. * @return A NUL-terminated byte buffer equivalent to the given String,
  839. * using the given charset.
  840. * @see #getBytes(String, String)
  841. */
  842. public static byte[] toByteArray(String s, Charset charset) {
  843. byte[] bytes = Native.getBytes(s, charset);
  844. byte[] buf = new byte[bytes.length+1];
  845. System.arraycopy(bytes, 0, buf, 0, bytes.length);
  846. return buf;
  847. }
  848. /**
  849. * @param s The string
  850. * @return A NUL-terminated wide character buffer equivalent to the given string.
  851. */
  852. public static char[] toCharArray(String s) {
  853. char[] chars = s.toCharArray();
  854. char[] buf = new char[chars.length+1];
  855. System.arraycopy(chars, 0, buf, 0, chars.length);
  856. return buf;
  857. }
  858. /**
  859. * Loads the JNA stub library.
  860. * First tries jna.boot.library.path, then the system path, then from the
  861. * jar file.
  862. */
  863. private static void loadNativeDispatchLibrary() {
  864. if (!Boolean.getBoolean("jna.nounpack")) {
  865. try {
  866. removeTemporaryFiles();
  867. }
  868. catch(IOException e) {
  869. LOG.log(Level.WARNING, "JNA Warning: IOException removing temporary files", e);
  870. }
  871. }
  872. String libName = System.getProperty("jna.boot.library.name", "jnidispatch");
  873. String bootPath = System.getProperty("jna.boot.library.path");
  874. if (bootPath != null) {
  875. // String.split not available in 1.4
  876. StringTokenizer dirs = new StringTokenizer(bootPath, File.pathSeparator);
  877. while (dirs.hasMoreTokens()) {
  878. String dir = dirs.nextToken();
  879. File file = new File(new File(dir), System.mapLibraryName(libName).replace(".dylib", ".jnilib"));
  880. String path = file.getAbsolutePath();
  881. LOG.log(DEBUG_JNA_LOAD_LEVEL, "Looking in {0}", path);
  882. if (file.exists()) {
  883. try {
  884. LOG.log(DEBUG_JNA_LOAD_LEVEL, "Trying {0}", path);
  885. System.setProperty("jnidispatch.path", path);
  886. System.load(path);
  887. jnidispatchPath = path;
  888. LOG.log(DEBUG_JNA_LOAD_LEVEL, "Found jnidispatch at {0}", path);
  889. return;
  890. } catch (UnsatisfiedLinkError ex) {
  891. // Not a problem if already loaded in anoteher class loader
  892. // Unfortunately we can't distinguish the difference...
  893. //System.out.println("File found at " + file + " but not loadable: " + ex.getMessage());
  894. }
  895. }
  896. if (Platform.isMac()) {
  897. String orig, ext;
  898. if (path.endsWith("dylib")) {
  899. orig = "dylib";
  900. ext = "jnilib";
  901. } else {
  902. orig = "jnilib";
  903. ext = "dylib";
  904. }
  905. path = path.substring(0, path.lastIndexOf(orig)) + ext;
  906. LOG.log(DEBUG_JNA_LOAD_LEVEL, "Looking in {0}", path);
  907. if (new File(path).exists()) {
  908. try {
  909. LOG.log(DEBUG_JNA_LOAD_LEVEL, "Trying {0}", path);
  910. System.setProperty("jnidispatch.path", path);
  911. System.load(path);
  912. jnidispatchPath = path;
  913. LOG.log(DEBUG_JNA_LOAD_LEVEL, "Found jnidispatch at {0}", path);
  914. return;
  915. } catch (UnsatisfiedLinkError ex) {
  916. LOG.log(Level.WARNING, "File found at " + path + " but not loadable: " + ex.getMessage(), ex);
  917. }
  918. }
  919. }
  920. }
  921. }
  922. String jnaNosys = System.getProperty("jna.nosys", "true");
  923. if ((!Boolean.parseBoolean(jnaNosys)) || Platform.isAndroid()) {
  924. try {
  925. LOG.log(DEBUG_JNA_LOAD_LEVEL, "Trying (via loadLibrary) {0}", libName);
  926. System.loadLibrary(libName);
  927. LOG.log(DEBUG_JNA_LOAD_LEVEL, "Found jnidispatch on system path");
  928. return;
  929. }
  930. catch(UnsatisfiedLinkError e) {
  931. }
  932. }
  933. if (!Boolean.getBoolean("jna.noclasspath")) {
  934. loadNativeDispatchLibraryFromClasspath();
  935. }
  936. else {
  937. throw new UnsatisfiedLinkError("Unable to locate JNA native support library");
  938. }
  939. }
  940. static final String JNA_TMPLIB_PREFIX = "jna";
  941. /**
  942. * Attempts to load the native library resource from the filesystem,
  943. * extracting the JNA stub library from jna.jar if not already available.
  944. */
  945. private static void loadNativeDispatchLibraryFromClasspath() {
  946. try {
  947. String mappedName = System.mapLibraryName("jnidispatch").replace(".dylib", ".jnilib");
  948. if(Platform.isAIX()) {
  949. // OpenJDK is reported to map to .so -- this works around the
  950. // difference between J9 and OpenJDK
  951. mappedName = "libjnidispatch.a";
  952. }
  953. String libName = "/com/sun/jna/" + Platform.RESOURCE_PREFIX + "/" + mappedName;
  954. File lib = extractFromResourcePath(libName, Native.class.getClassLoader());
  955. if (lib == null) {
  956. if (lib == null) {
  957. throw new UnsatisfiedLinkError("Could not find JNA native support");
  958. }
  959. }
  960. LOG.log(DEBUG_JNA_LOAD_LEVEL, "Trying {0}", lib.getAbsolutePath());
  961. System.setProperty("jnidispatch.path", lib.getAbsolutePath());
  962. System.load(lib.getAbsolutePath());
  963. jnidispatchPath = lib.getAbsolutePath();
  964. LOG.log(DEBUG_JNA_LOAD_LEVEL, "Found jnidispatch at {0}", jnidispatchPath);
  965. // Attempt to delete immediately once jnidispatch is successfully
  966. // loaded. This avoids the complexity of trying to do so on "exit",
  967. // which point can vary under different circumstances (native
  968. // compilation, dynamically loaded modules, normal application, etc).
  969. if (isUnpacked(lib)
  970. && !Boolean.getBoolean("jnidispatch.preserve")) {
  971. deleteLibrary(lib);
  972. }
  973. }
  974. catch(IOException e) {
  975. throw new UnsatisfiedLinkError(e.getMessage());
  976. }
  977. }
  978. /** Identify temporary files unpacked from classpath jar files. */
  979. static boolean isUnpacked(File file) {
  980. return file.getName().startsWith(JNA_TMPLIB_PREFIX);
  981. }
  982. /** Attempt to extract a native library from the current resource path,
  983. * using the current thread context class loader.
  984. * @param name Base name of native library to extract. May also be an
  985. * absolute resource path (i.e. starts with "/"), in which case the
  986. * no transformations of the library name are performed. If only the base
  987. * name is given, the resource path is attempted both with and without
  988. * {@link Platform#RESOURCE_PREFIX}, after mapping the library name via
  989. * {@link NativeLibrary#mapSharedLibraryName(String)}.
  990. * @return File indicating extracted resource on disk
  991. * @throws IOException if resource not found
  992. */
  993. public static File extractFromResourcePath(String name) throws IOException {
  994. return extractFromResourcePath(name, null);
  995. }
  996. /** Attempt to extract a native library from the resource path using the
  997. * given class loader.
  998. * @param name Base name of native library to extract. May also be an
  999. * absolute resource path (i.e. starts with "/"), in which case the
  1000. * no transformations of the library name are performed. If only the base
  1001. * name is given, the resource path is attempted both with and without
  1002. * {@link Platform#RESOURCE_PREFIX}, after mapping the library name via
  1003. * {@link NativeLibrary#mapSharedLibraryName(String)}.
  1004. * @param loader Class loader to use to load resources
  1005. * @return File indicating extracted resource on disk
  1006. * @throws IOException if resource not found
  1007. */
  1008. public static File extractFromResourcePath(String name, ClassLoader loader) throws IOException {
  1009. final Level DEBUG = (DEBUG_LOAD
  1010. || (DEBUG_JNA_LOAD && name.contains("jnidispatch"))) ? Level.INFO : Level.FINE;
  1011. if (loader == null) {
  1012. loader = Thread.currentThread().getContextClassLoader();
  1013. // Context class loader is not guaranteed to be set
  1014. if (loader == null) {
  1015. loader = Native.class.getClassLoader();
  1016. }
  1017. }
  1018. LOG.log(DEBUG, "Looking in classpath from {0} for {1}", new Object[]{loader, name});
  1019. String libname = name.startsWith("/") ? name : NativeLibrary.mapSharedLibraryName(name);
  1020. String resourcePath = name.startsWith("/") ? name : Platform.RESOURCE_PREFIX + "/" + libname;
  1021. if (resourcePath.startsWith("/")) {
  1022. resourcePath = resourcePath.substring(1);
  1023. }
  1024. URL url = loader.getResource(resourcePath);
  1025. if (url == null) {
  1026. if (resourcePath.startsWith(Platform.RESOURCE_PREFIX)) {
  1027. // Fallback for legacy darwin behaviour: darwin was in the past
  1028. // special cased in that all architectures were mapped to the same
  1029. // prefix and it was expected, that a fat binary was present at that
  1030. // point, that contained all architectures.
  1031. if(Platform.RESOURCE_PREFIX.startsWith("darwin")) {
  1032. url = loader.getResource("darwin/" + resourcePath.substring(Platform.RESOURCE_PREFIX.length() + 1));
  1033. }
  1034. if (url == null) {
  1035. // If not found with the standard resource prefix, try without it
  1036. url = loader.getResource(libname);
  1037. }
  1038. } else if (resourcePath.startsWith("com/sun/jna/" + Platform.RESOURCE_PREFIX + "/")) {
  1039. // Fallback for legacy darwin behaviour: darwin was in the past
  1040. // special cased in that all architectures were mapped to the same
  1041. // prefix and it was expected, that a fat binary was present at that
  1042. // point, that contained all architectures.
  1043. if(Platform.RESOURCE_PREFIX.startsWith("com/sun/jna/darwin")) {
  1044. url = loader.getResource("com/sun/jna/darwin" + resourcePath.substring(("com/sun/jna/" + Platform.RESOURCE_PREFIX).length() + 1));
  1045. }
  1046. if (url == null) {
  1047. // If not found with the standard resource prefix, try without it
  1048. url = loader.getResource(libname);
  1049. }
  1050. }
  1051. }
  1052. if (url == null) {
  1053. String path = System.getProperty("java.class.path");
  1054. if (loader instanceof URLClass

Large files files are truncated, but you can click here to view the full file