8201494: Avoid early initialization of java.nio.Bits
authorredestad
Thu, 12 Apr 2018 17:23:32 +0200
changeset 49581 bd45ce23b1ac
parent 49580 62b908c9f0e6
child 49582 f29aeb8bb44f
child 49583 47861fafb279
8201494: Avoid early initialization of java.nio.Bits Reviewed-by: rriggs, alanb
src/java.base/share/classes/java/nio/Bits.java
src/java.base/share/classes/java/nio/Buffer.java
src/java.base/share/classes/java/nio/ByteOrder.java
src/java.base/share/classes/java/nio/X-Buffer.java.template
--- a/src/java.base/share/classes/java/nio/Bits.java	Thu Apr 12 13:55:20 2018 +0100
+++ b/src/java.base/share/classes/java/nio/Bits.java	Thu Apr 12 17:23:32 2018 +0200
@@ -65,25 +65,13 @@
 
     private static final Unsafe UNSAFE = Unsafe.getUnsafe();
 
-    static Unsafe unsafe() {
-        return UNSAFE;
-    }
-
-
     // -- Processor and memory-system properties --
 
-    private static final ByteOrder BYTE_ORDER
-        = UNSAFE.isBigEndian() ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN;
-
-    static ByteOrder byteOrder() {
-        return BYTE_ORDER;
-    }
-
     private static int PAGE_SIZE = -1;
 
     static int pageSize() {
         if (PAGE_SIZE == -1)
-            PAGE_SIZE = unsafe().pageSize();
+            PAGE_SIZE = UNSAFE.pageSize();
         return PAGE_SIZE;
     }
 
--- a/src/java.base/share/classes/java/nio/Buffer.java	Thu Apr 12 13:55:20 2018 +0100
+++ b/src/java.base/share/classes/java/nio/Buffer.java	Thu Apr 12 17:23:32 2018 +0200
@@ -183,7 +183,7 @@
 
 public abstract class Buffer {
     // Cached unsafe-access object
-    static final Unsafe UNSAFE = Bits.unsafe();
+    static final Unsafe UNSAFE = Unsafe.getUnsafe();
 
     /**
      * The characteristics of Spliterators that traverse and split elements
--- a/src/java.base/share/classes/java/nio/ByteOrder.java	Thu Apr 12 13:55:20 2018 +0100
+++ b/src/java.base/share/classes/java/nio/ByteOrder.java	Thu Apr 12 17:23:32 2018 +0200
@@ -25,6 +25,7 @@
 
 package java.nio;
 
+import jdk.internal.misc.Unsafe;
 
 /**
  * A typesafe enumeration for byte orders.
@@ -57,6 +58,12 @@
     public static final ByteOrder LITTLE_ENDIAN
         = new ByteOrder("LITTLE_ENDIAN");
 
+    // Retrieve the native byte order. It's used early during bootstrap, and
+    // must be initialized after BIG_ENDIAN and LITTLE_ENDIAN.
+    private static final ByteOrder NATIVE_ORDER
+        = Unsafe.getUnsafe().isBigEndian()
+            ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN;
+
     /**
      * Retrieves the native byte order of the underlying platform.
      *
@@ -69,7 +76,7 @@
      *          virtual machine is running
      */
     public static ByteOrder nativeOrder() {
-        return Bits.byteOrder();
+        return NATIVE_ORDER;
     }
 
     /**
--- a/src/java.base/share/classes/java/nio/X-Buffer.java.template	Thu Apr 12 13:55:20 2018 +0100
+++ b/src/java.base/share/classes/java/nio/X-Buffer.java.template	Thu Apr 12 17:23:32 2018 +0200
@@ -1579,7 +1579,7 @@
     boolean bigEndian                                   // package-private
         = true;
     boolean nativeByteOrder                             // package-private
-        = (Bits.byteOrder() == ByteOrder.BIG_ENDIAN);
+        = (ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN);
 
     /**
      * Retrieves this buffer's byte order.
@@ -1608,7 +1608,7 @@
     public final $Type$Buffer order(ByteOrder bo) {
         bigEndian = (bo == ByteOrder.BIG_ENDIAN);
         nativeByteOrder =
-            (bigEndian == (Bits.byteOrder() == ByteOrder.BIG_ENDIAN));
+            (bigEndian == (ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN));
         return this;
     }