< prev index next >

src/java.base/share/classes/java/io/BufferedInputStream.java

Print this page
rev 13993 : 8153334: Replace BufferedInputStreams use of AtomicReferenceFieldUpdater with Unsafe
Reviewed-by: TBD

@@ -22,11 +22,10 @@
  * or visit www.oracle.com if you need additional information or have any
  * questions.
  */
 
 package java.io;
-import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
 
 /**
  * A <code>BufferedInputStream</code> adds
  * functionality to another input stream-namely,
  * the ability to buffer the input and to

@@ -66,19 +65,26 @@
      * a different size.
      */
     protected volatile byte buf[];
 
     /**
-     * Atomic updater to provide compareAndSet for buf. This is
-     * necessary because closes can be asynchronous. We use nullness
+     * Get Unsafe and offset of buf to provide compareAndSet functionality.
+     * This is necessary because closes can be asynchronous. We use nullness
      * of buf[] as primary indicator that this stream is closed. (The
      * "in" field is also nulled out on close.)
      */
-    private static final
-        AtomicReferenceFieldUpdater<BufferedInputStream, byte[]> bufUpdater =
-        AtomicReferenceFieldUpdater.newUpdater
-        (BufferedInputStream.class,  byte[].class, "buf");
+    private static final jdk.internal.misc.Unsafe U =
+            jdk.internal.misc.Unsafe.getUnsafe();
+    private static final long BUF_OFFSET;
+    static {
+        try {
+            BUF_OFFSET = U.objectFieldOffset
+                (BufferedInputStream.class.getDeclaredField("buf"));
+        } catch (ReflectiveOperationException e) {
+            throw new Error(e);
+        }
+    }
 
     /**
      * The index one greater than the index of the last valid byte in
      * the buffer.
      * This value is always

@@ -230,11 +236,11 @@
                         pos * 2 : MAX_BUFFER_SIZE;
                 if (nsz > marklimit)
                     nsz = marklimit;
                 byte nbuf[] = new byte[nsz];
                 System.arraycopy(buffer, 0, nbuf, 0, pos);
-                if (!bufUpdater.compareAndSet(this, buffer, nbuf)) {
+                if (!U.compareAndSwapObject(this, BUF_OFFSET, buffer, nbuf)) {
                     // Can't replace buf if there was an async close.
                     // Note: This would need to be changed if fill()
                     // is ever made accessible to multiple threads.
                     // But for now, the only way CAS can fail is via close.
                     // assert buf == null;

@@ -474,11 +480,11 @@
      * @exception  IOException  if an I/O error occurs.
      */
     public void close() throws IOException {
         byte[] buffer;
         while ( (buffer = buf) != null) {
-            if (bufUpdater.compareAndSet(this, buffer, null)) {
+            if (U.compareAndSwapObject(this, BUF_OFFSET, buffer, null)) {
                 InputStream input = in;
                 in = null;
                 if (input != null)
                     input.close();
                 return;
< prev index next >