PageRenderTime 53ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/neosam/gluegen
Java | 163 lines | 85 code | 24 blank | 54 comment | 31 complexity | 79e51b647e03adaa3b79a2911065ee64 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. /**
  36. * Utility class for querying platform specific properties.
  37. * @author Michael Bien, Sven Gothel
  38. */
  39. public class Platform {
  40. public static final boolean JAVA_SE;
  41. public static final boolean LITTLE_ENDIAN;
  42. private final static boolean is32Bit;
  43. private final static int pointerSizeInBits;
  44. private final static String os, arch;
  45. static {
  46. NativeLibrary.ensureNativeLibLoaded();
  47. // We don't seem to need an AccessController.doPrivileged() block
  48. // here as these system properties are visible even to unsigned
  49. // applets
  50. os = System.getProperty("os.name");
  51. arch = System.getProperty("os.arch");
  52. pointerSizeInBits = getPointerSizeInBitsImpl();
  53. // Try to use Sun's sun.arch.data.model first ..
  54. if ( 32 == pointerSizeInBits || 64 == pointerSizeInBits ) {
  55. is32Bit = ( 32 == pointerSizeInBits );
  56. }else {
  57. String os_lc = os.toLowerCase();
  58. String arch_lc = arch.toLowerCase();
  59. if ((os_lc.startsWith("windows") && arch_lc.equals("x86")) ||
  60. (os_lc.startsWith("windows") && arch_lc.equals("arm")) ||
  61. (os_lc.startsWith("linux") && arch_lc.equals("i386")) ||
  62. (os_lc.startsWith("linux") && arch_lc.equals("x86")) ||
  63. (os_lc.startsWith("mac os_lc") && arch_lc.equals("ppc")) ||
  64. (os_lc.startsWith("mac os_lc") && arch_lc.equals("i386")) ||
  65. (os_lc.startsWith("darwin") && arch_lc.equals("ppc")) ||
  66. (os_lc.startsWith("darwin") && arch_lc.equals("i386")) ||
  67. (os_lc.startsWith("sunos_lc") && arch_lc.equals("sparc")) ||
  68. (os_lc.startsWith("sunos_lc") && arch_lc.equals("x86")) ||
  69. (os_lc.startsWith("freebsd") && arch_lc.equals("i386")) ||
  70. (os_lc.startsWith("hp-ux") && arch_lc.equals("pa_risc2.0"))) {
  71. is32Bit = true;
  72. } else if ((os_lc.startsWith("windows") && arch_lc.equals("amd64")) ||
  73. (os_lc.startsWith("linux") && arch_lc.equals("amd64")) ||
  74. (os_lc.startsWith("linux") && arch_lc.equals("x86_64")) ||
  75. (os_lc.startsWith("linux") && arch_lc.equals("ia64")) ||
  76. (os_lc.startsWith("mac os_lc") && arch_lc.equals("x86_64")) ||
  77. (os_lc.startsWith("darwin") && arch_lc.equals("x86_64")) ||
  78. (os_lc.startsWith("sunos_lc") && arch_lc.equals("sparcv9")) ||
  79. (os_lc.startsWith("sunos_lc") && arch_lc.equals("amd64"))) {
  80. is32Bit = false;
  81. }else{
  82. throw new RuntimeException("Please port CPU detection (32/64 bit) to your platform (" + os_lc + "/" + arch_lc + ")");
  83. }
  84. }
  85. // fast path
  86. boolean se = System.getProperty("java.runtime.name").indexOf("Java SE") != -1;
  87. if(!se) {
  88. try{
  89. Class.forName("java.nio.LongBuffer");
  90. Class.forName("java.nio.DoubleBuffer");
  91. se = true;
  92. }catch(ClassNotFoundException ex) {
  93. se = false;
  94. }
  95. }
  96. JAVA_SE = se;
  97. // byte order
  98. ByteBuffer tst_b = Buffers.newDirectByteBuffer(Buffers.SIZEOF_INT); // 32bit in native order
  99. IntBuffer tst_i = tst_b.asIntBuffer();
  100. ShortBuffer tst_s = tst_b.asShortBuffer();
  101. tst_i.put(0, 0x0A0B0C0D);
  102. LITTLE_ENDIAN = 0x0C0D == tst_s.get(0);
  103. }
  104. private Platform() {}
  105. private static native int getPointerSizeInBitsImpl();
  106. /**
  107. * Returns true only if this program is running on the Java Standard Edition.
  108. */
  109. public static boolean isJavaSE() {
  110. return JAVA_SE;
  111. }
  112. /**
  113. * Returns true only if this system uses little endian byte ordering.
  114. */
  115. public static boolean isLittleEndian() {
  116. return LITTLE_ENDIAN;
  117. }
  118. /**
  119. * Returns the OS name.
  120. */
  121. public static String getOS() {
  122. return os;
  123. }
  124. /**
  125. * Returns the CPU architecture String.
  126. */
  127. public static String getArch() {
  128. return arch;
  129. }
  130. /**
  131. * Returns true if this JVM is a 32bit JVM.
  132. */
  133. public static boolean is32Bit() {
  134. return is32Bit;
  135. }
  136. public static int getPointerSizeInBits() {
  137. return pointerSizeInBits;
  138. }
  139. }