PageRenderTime 54ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/src/com/sun/jna/Platform.java

https://github.com/Boereck/jna
Java | 287 lines | 239 code | 10 blank | 38 comment | 82 complexity | c8b4c64181c72a5c5f55f5add29d0264 MD5 | raw file
Possible License(s): Apache-2.0
  1. /*
  2. * This library is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU Lesser General Public License as published by the
  4. * Free Software Foundation; either version 2.1 of the License, or (at
  5. * your option) any later version. This library is distributed in the
  6. * hope that it will be useful, but WITHOUT ANY WARRANTY; without even
  7. * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  8. * PURPOSE. See the GNU Lesser General Public License for more details.
  9. */
  10. package com.sun.jna;
  11. /** Provide simplified platform information. */
  12. public final class Platform {
  13. public static final int UNSPECIFIED = -1;
  14. public static final int MAC = 0;
  15. public static final int LINUX = 1;
  16. public static final int WINDOWS = 2;
  17. public static final int SOLARIS = 3;
  18. public static final int FREEBSD = 4;
  19. public static final int OPENBSD = 5;
  20. public static final int WINDOWSCE = 6;
  21. public static final int AIX = 7;
  22. public static final int ANDROID = 8;
  23. public static final int GNU = 9;
  24. public static final int KFREEBSD = 10;
  25. public static final int NETBSD = 11;
  26. /** Whether read-only (final) fields within Structures are supported. */
  27. public static final boolean RO_FIELDS;
  28. /** Whether this platform provides NIO Buffers. */
  29. public static final boolean HAS_BUFFERS;
  30. /** Whether this platform provides the AWT Component class; also false if
  31. * running headless.
  32. */
  33. public static final boolean HAS_AWT;
  34. /** Canonical name of this platform's math library. */
  35. public static final String MATH_LIBRARY_NAME;
  36. /** Canonical name of this platform's C runtime library. */
  37. public static final String C_LIBRARY_NAME;
  38. /** Whether in-DLL callbacks are supported. */
  39. public static final boolean HAS_DLL_CALLBACKS;
  40. /** Canonical resource prefix for the current platform. This value is
  41. * used to load bundled native libraries from the class path.
  42. */
  43. public static final String RESOURCE_PREFIX;
  44. private static final int osType;
  45. /** Current platform architecture. */
  46. public static final String ARCH;
  47. static {
  48. String osName = System.getProperty("os.name");
  49. if (osName.startsWith("Linux")) {
  50. if ("dalvik".equals(System.getProperty("java.vm.name").toLowerCase())) {
  51. osType = ANDROID;
  52. // Native libraries on android must be bundled with the APK
  53. System.setProperty("jna.nounpack", "true");
  54. }
  55. else {
  56. osType = LINUX;
  57. }
  58. }
  59. else if (osName.startsWith("AIX")) {
  60. osType = AIX;
  61. }
  62. else if (osName.startsWith("Mac") || osName.startsWith("Darwin")) {
  63. osType = MAC;
  64. }
  65. else if (osName.startsWith("Windows CE")) {
  66. osType = WINDOWSCE;
  67. }
  68. else if (osName.startsWith("Windows")) {
  69. osType = WINDOWS;
  70. }
  71. else if (osName.startsWith("Solaris") || osName.startsWith("SunOS")) {
  72. osType = SOLARIS;
  73. }
  74. else if (osName.startsWith("FreeBSD")) {
  75. osType = FREEBSD;
  76. }
  77. else if (osName.startsWith("OpenBSD")) {
  78. osType = OPENBSD;
  79. }
  80. else if (osName.equalsIgnoreCase("gnu")) {
  81. osType = GNU;
  82. }
  83. else if (osName.equalsIgnoreCase("gnu/kfreebsd")) {
  84. osType = KFREEBSD;
  85. }
  86. else if (osName.equalsIgnoreCase("netbsd")) {
  87. osType = NETBSD;
  88. }
  89. else {
  90. osType = UNSPECIFIED;
  91. }
  92. boolean hasBuffers = false;
  93. try {
  94. Class.forName("java.nio.Buffer");
  95. hasBuffers = true;
  96. }
  97. catch(ClassNotFoundException e) {
  98. }
  99. // NOTE: we used to do Class.forName("java.awt.Component"), but that
  100. // has the unintended side effect of actually loading AWT native libs,
  101. // which can be problematic
  102. HAS_AWT = osType != WINDOWSCE && osType != ANDROID && osType != AIX;
  103. HAS_BUFFERS = hasBuffers;
  104. RO_FIELDS = osType != WINDOWSCE;
  105. C_LIBRARY_NAME = osType == WINDOWS ? "msvcrt" : osType == WINDOWSCE ? "coredll" : "c";
  106. MATH_LIBRARY_NAME = osType == WINDOWS ? "msvcrt" : osType == WINDOWSCE ? "coredll" : "m";
  107. HAS_DLL_CALLBACKS = osType == WINDOWS;
  108. RESOURCE_PREFIX = getNativeLibraryResourcePrefix();
  109. ARCH = System.getProperty("os.arch").toLowerCase().trim();
  110. }
  111. private Platform() { }
  112. public static final int getOSType() {
  113. return osType;
  114. }
  115. public static final boolean isMac() {
  116. return osType == MAC;
  117. }
  118. public static final boolean isAndroid() {
  119. return osType == ANDROID;
  120. }
  121. public static final boolean isLinux() {
  122. return osType == LINUX;
  123. }
  124. public static final boolean isAIX() {
  125. return osType == AIX;
  126. }
  127. /** @deprecated */
  128. public static final boolean isAix() {
  129. return isAIX();
  130. }
  131. public static final boolean isWindowsCE() {
  132. return osType == WINDOWSCE;
  133. }
  134. /** Returns true for any windows variant. */
  135. public static final boolean isWindows() {
  136. return osType == WINDOWS || osType == WINDOWSCE;
  137. }
  138. public static final boolean isSolaris() {
  139. return osType == SOLARIS;
  140. }
  141. public static final boolean isFreeBSD() {
  142. return osType == FREEBSD;
  143. }
  144. public static final boolean isOpenBSD() {
  145. return osType == OPENBSD;
  146. }
  147. public static final boolean isNetBSD() {
  148. return osType == NETBSD;
  149. }
  150. public static final boolean isGNU() {
  151. return osType == GNU;
  152. }
  153. public static final boolean iskFreeBSD() {
  154. return osType == KFREEBSD;
  155. }
  156. public static final boolean isX11() {
  157. // TODO: check filesystem for /usr/X11 or some other X11-specific test
  158. return !Platform.isWindows() && !Platform.isMac();
  159. }
  160. public static final boolean hasRuntimeExec() {
  161. if (isWindowsCE() && "J9".equals(System.getProperty("java.vm.name")))
  162. return false;
  163. return true;
  164. }
  165. public static final boolean is64Bit() {
  166. String model = System.getProperty("sun.arch.data.model",
  167. System.getProperty("com.ibm.vm.bitmode"));
  168. if (model != null) {
  169. return "64".equals(model);
  170. }
  171. if ("x86_64".equals(ARCH)
  172. || "ia64".equals(ARCH)
  173. || "ppc64".equals(ARCH)
  174. || "sparcv9".equals(ARCH)
  175. || "amd64".equals(ARCH)) {
  176. return true;
  177. }
  178. return Native.POINTER_SIZE == 8;
  179. }
  180. public static final boolean isIntel() {
  181. if (ARCH.equals("i386")
  182. || ARCH.startsWith("i686")
  183. || ARCH.equals("x86")
  184. || ARCH.equals("x86_64")
  185. || ARCH.equals("amd64")) {
  186. return true;
  187. }
  188. return false;
  189. }
  190. public static final boolean isPPC() {
  191. if (ARCH.equals("ppc")
  192. || ARCH.equals("ppc64")
  193. || ARCH.equals("powerpc")
  194. || ARCH.equals("powerpc64")) {
  195. return true;
  196. }
  197. return false;
  198. }
  199. public static final boolean isARM() {
  200. return ARCH.startsWith("arm");
  201. }
  202. public static final boolean isSPARC() {
  203. return ARCH.startsWith("sparc");
  204. }
  205. /** Generate a canonical String prefix based on the current OS
  206. type/arch/name.
  207. */
  208. static String getNativeLibraryResourcePrefix() {
  209. return getNativeLibraryResourcePrefix(getOSType(), System.getProperty("os.arch"), System.getProperty("os.name"));
  210. }
  211. /** Generate a canonical String prefix based on the given OS
  212. type/arch/name.
  213. @param osType from {@link #getOSType()}
  214. @param arch from <code>os.arch</code> System property
  215. @param name from <code>os.name</code> System property
  216. */
  217. static String getNativeLibraryResourcePrefix(int osType, String arch, String name) {
  218. String osPrefix;
  219. arch = arch.toLowerCase().trim();
  220. if ("powerpc".equals(arch)) {
  221. arch = "ppc";
  222. }
  223. else if ("powerpc64".equals(arch)) {
  224. arch = "ppc64";
  225. }
  226. else if ("i386".equals(arch)) {
  227. arch = "x86";
  228. }
  229. else if ("x86_64".equals(arch) || "amd64".equals(arch)) {
  230. arch = "x86-64";
  231. }
  232. switch(osType) {
  233. case Platform.ANDROID:
  234. if (arch.startsWith("arm")) {
  235. arch = "arm";
  236. }
  237. osPrefix = "android-" + arch;
  238. break;
  239. case Platform.WINDOWS:
  240. osPrefix = "win32-" + arch;
  241. break;
  242. case Platform.WINDOWSCE:
  243. osPrefix = "w32ce-" + arch;
  244. break;
  245. case Platform.MAC:
  246. osPrefix = "darwin";
  247. break;
  248. case Platform.LINUX:
  249. osPrefix = "linux-" + arch;
  250. break;
  251. case Platform.SOLARIS:
  252. osPrefix = "sunos-" + arch;
  253. break;
  254. case Platform.FREEBSD:
  255. osPrefix = "freebsd-" + arch;
  256. break;
  257. case Platform.OPENBSD:
  258. osPrefix = "openbsd-" + arch;
  259. break;
  260. case Platform.NETBSD:
  261. osPrefix = "netbsd-" + arch;
  262. break;
  263. case Platform.KFREEBSD:
  264. osPrefix = "kfreebsd-" + arch;
  265. break;
  266. default:
  267. osPrefix = name.toLowerCase();
  268. int space = osPrefix.indexOf(" ");
  269. if (space != -1) {
  270. osPrefix = osPrefix.substring(0, space);
  271. }
  272. osPrefix += "-" + arch;
  273. break;
  274. }
  275. return osPrefix;
  276. }
  277. }