PageRenderTime 42ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/src/java/com/jogamp/common/os/Platform.java

https://github.com/rothwell/gluegen
Java | 170 lines | 92 code | 24 blank | 54 comment | 31 complexity | 1d88521a82c08a50d717ae3601800fba MD5 | raw file
  1. /*
  2. * Copyright (c) 2010, Michael Bien, Sven Gothel
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of Michael Bien nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  17. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL Michael Bien BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. /*
  28. * Created on Sunday, March 28 2010 14:43
  29. */
  30. package com.jogamp.common.os;
  31. import com.jogamp.common.nio.Buffers;
  32. import java.nio.ByteBuffer;
  33. import java.nio.IntBuffer;
  34. import java.nio.ShortBuffer;
  35. import java.security.*;
  36. /**
  37. * Utility class for querying platform specific properties.
  38. * @author Michael Bien, Sven Gothel
  39. */
  40. public class Platform {
  41. public static final boolean JAVA_SE;
  42. public static final boolean LITTLE_ENDIAN;
  43. private final static boolean is32Bit;
  44. private final static int pointerSizeInBits;
  45. private final static String os, arch;
  46. static {
  47. NativeLibrary.ensureNativeLibLoaded();
  48. // We don't seem to need an AccessController.doPrivileged() block
  49. // here as these system properties are visible even to unsigned
  50. // applets
  51. os = System.getProperty("os.name");
  52. arch = System.getProperty("os.arch");
  53. pointerSizeInBits = getPointerSizeInBitsImpl();
  54. // Try to use Sun's sun.arch.data.model first ..
  55. if ( 32 == pointerSizeInBits || 64 == pointerSizeInBits ) {
  56. is32Bit = ( 32 == pointerSizeInBits );
  57. }else {
  58. String os_lc = os.toLowerCase();
  59. String arch_lc = arch.toLowerCase();
  60. if ((os_lc.startsWith("windows") && arch_lc.equals("x86")) ||
  61. (os_lc.startsWith("windows") && arch_lc.equals("arm")) ||
  62. (os_lc.startsWith("linux") && arch_lc.equals("i386")) ||
  63. (os_lc.startsWith("linux") && arch_lc.equals("x86")) ||
  64. (os_lc.startsWith("mac os_lc") && arch_lc.equals("ppc")) ||
  65. (os_lc.startsWith("mac os_lc") && arch_lc.equals("i386")) ||
  66. (os_lc.startsWith("darwin") && arch_lc.equals("ppc")) ||
  67. (os_lc.startsWith("darwin") && arch_lc.equals("i386")) ||
  68. (os_lc.startsWith("sunos_lc") && arch_lc.equals("sparc")) ||
  69. (os_lc.startsWith("sunos_lc") && arch_lc.equals("x86")) ||
  70. (os_lc.startsWith("freebsd") && arch_lc.equals("i386")) ||
  71. (os_lc.startsWith("hp-ux") && arch_lc.equals("pa_risc2.0"))) {
  72. is32Bit = true;
  73. } else if ((os_lc.startsWith("windows") && arch_lc.equals("amd64")) ||
  74. (os_lc.startsWith("linux") && arch_lc.equals("amd64")) ||
  75. (os_lc.startsWith("linux") && arch_lc.equals("x86_64")) ||
  76. (os_lc.startsWith("linux") && arch_lc.equals("ia64")) ||
  77. (os_lc.startsWith("mac os_lc") && arch_lc.equals("x86_64")) ||
  78. (os_lc.startsWith("darwin") && arch_lc.equals("x86_64")) ||
  79. (os_lc.startsWith("sunos_lc") && arch_lc.equals("sparcv9")) ||
  80. (os_lc.startsWith("sunos_lc") && arch_lc.equals("amd64"))) {
  81. is32Bit = false;
  82. }else{
  83. throw new RuntimeException("Please port CPU detection (32/64 bit) to your platform (" + os_lc + "/" + arch_lc + ")");
  84. }
  85. }
  86. boolean se;
  87. try{
  88. Class.forName("java.nio.LongBuffer");
  89. Class.forName("java.nio.DoubleBuffer");
  90. se = true;
  91. }catch(ClassNotFoundException ex) {
  92. se = false;
  93. }
  94. if(!se) {
  95. // no more the fast path, due to access controller ..
  96. String java_runtime_name = (String) AccessController.doPrivileged(new PrivilegedAction() {
  97. public Object run() {
  98. return System.getProperty("java.runtime.name");
  99. }
  100. });
  101. se = java_runtime_name.indexOf("Java SE") != -1;
  102. }
  103. JAVA_SE = se;
  104. // byte order
  105. ByteBuffer tst_b = Buffers.newDirectByteBuffer(Buffers.SIZEOF_INT); // 32bit in native order
  106. IntBuffer tst_i = tst_b.asIntBuffer();
  107. ShortBuffer tst_s = tst_b.asShortBuffer();
  108. tst_i.put(0, 0x0A0B0C0D);
  109. LITTLE_ENDIAN = 0x0C0D == tst_s.get(0);
  110. }
  111. private Platform() {}
  112. private static native int getPointerSizeInBitsImpl();
  113. /**
  114. * Returns true only if this program is running on the Java Standard Edition.
  115. */
  116. public static boolean isJavaSE() {
  117. return JAVA_SE;
  118. }
  119. /**
  120. * Returns true only if this system uses little endian byte ordering.
  121. */
  122. public static boolean isLittleEndian() {
  123. return LITTLE_ENDIAN;
  124. }
  125. /**
  126. * Returns the OS name.
  127. */
  128. public static String getOS() {
  129. return os;
  130. }
  131. /**
  132. * Returns the CPU architecture String.
  133. */
  134. public static String getArch() {
  135. return arch;
  136. }
  137. /**
  138. * Returns true if this JVM is a 32bit JVM.
  139. */
  140. public static boolean is32Bit() {
  141. return is32Bit;
  142. }
  143. public static int getPointerSizeInBits() {
  144. return pointerSizeInBits;
  145. }
  146. }