# HG changeset patch # User redestad # Date 1503314077 -7200 # Mon Aug 21 13:14:37 2017 +0200 # Node ID b8dc6078e91a7c229e80f8999a948e70086599ef # Parent 4469f450455806ceb5c40cdcc923690a878ff85a 8185362: Replace use of AtomicReferenceFieldUpdater from BufferedInputStream with Unsafe Reviewed-by: shade diff --git a/src/java.base/share/classes/java/io/BufferedInputStream.java b/src/java.base/share/classes/java/io/BufferedInputStream.java --- a/src/java.base/share/classes/java/io/BufferedInputStream.java +++ b/src/java.base/share/classes/java/io/BufferedInputStream.java @@ -24,7 +24,8 @@ */ package java.io; -import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; + +import jdk.internal.misc.Unsafe; /** * A BufferedInputStream adds @@ -60,23 +61,17 @@ */ private static int MAX_BUFFER_SIZE = Integer.MAX_VALUE - 8; + private static final Unsafe U = Unsafe.getUnsafe(); + + private static final long BUF_OFFSET + = U.objectFieldOffset(BufferedInputStream.class, "buf"); + /** * The internal buffer array where the data is stored. When necessary, * it may be replaced by another array of * 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 - * of buf[] as primary indicator that this stream is closed. (The - * "in" field is also nulled out on close.) - */ - private static final - AtomicReferenceFieldUpdater bufUpdater = - AtomicReferenceFieldUpdater.newUpdater - (BufferedInputStream.class, byte[].class, "buf"); + protected volatile byte[] buf; /** * The index one greater than the index of the last valid byte in @@ -232,7 +227,7 @@ nsz = marklimit; byte nbuf[] = new byte[nsz]; System.arraycopy(buffer, 0, nbuf, 0, pos); - if (!bufUpdater.compareAndSet(this, buffer, nbuf)) { + if (!U.compareAndSetObject(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. @@ -476,7 +471,7 @@ public void close() throws IOException { byte[] buffer; while ( (buffer = buf) != null) { - if (bufUpdater.compareAndSet(this, buffer, null)) { + if (U.compareAndSetObject(this, BUF_OFFSET, buffer, null)) { InputStream input = in; in = null; if (input != null)