src/java.base/share/classes/sun/nio/ch/Util.java
changeset 50719 106dc156ce6b
parent 47428 d72d7d55c765
child 57804 9b7b9f16dfd9
equal deleted inserted replaced
50715:46492a773912 50719:106dc156ce6b
    24  */
    24  */
    25 
    25 
    26 package sun.nio.ch;
    26 package sun.nio.ch;
    27 
    27 
    28 import java.io.FileDescriptor;
    28 import java.io.FileDescriptor;
       
    29 import java.io.IOException;
    29 import java.lang.reflect.Constructor;
    30 import java.lang.reflect.Constructor;
    30 import java.lang.reflect.InvocationTargetException;
    31 import java.lang.reflect.InvocationTargetException;
    31 import java.nio.ByteBuffer;
    32 import java.nio.ByteBuffer;
    32 import java.nio.MappedByteBuffer;
    33 import java.nio.MappedByteBuffer;
    33 import java.security.AccessController;
    34 import java.security.AccessController;
    34 import java.security.PrivilegedAction;
    35 import java.security.PrivilegedAction;
    35 import java.util.Collection;
    36 import java.util.Collection;
    36 import java.util.Iterator;
    37 import java.util.Iterator;
    37 import java.util.Set;
    38 import java.util.Set;
       
    39 
       
    40 import jdk.internal.misc.TerminatingThreadLocal;
    38 import jdk.internal.misc.Unsafe;
    41 import jdk.internal.misc.Unsafe;
    39 import sun.security.action.GetPropertyAction;
    42 import sun.security.action.GetPropertyAction;
    40 import java.io.IOException;
       
    41 
    43 
    42 public class Util {
    44 public class Util {
    43 
    45 
    44     // -- Caches --
    46     // -- Caches --
    45 
    47 
    48 
    50 
    49     // The max size allowed for a cached temp buffer, in bytes
    51     // The max size allowed for a cached temp buffer, in bytes
    50     private static final long MAX_CACHED_BUFFER_SIZE = getMaxCachedBufferSize();
    52     private static final long MAX_CACHED_BUFFER_SIZE = getMaxCachedBufferSize();
    51 
    53 
    52     // Per-thread cache of temporary direct buffers
    54     // Per-thread cache of temporary direct buffers
    53     private static ThreadLocal<BufferCache> bufferCache =
    55     private static ThreadLocal<BufferCache> bufferCache = new TerminatingThreadLocal<>() {
    54         new ThreadLocal<BufferCache>()
       
    55     {
       
    56         @Override
    56         @Override
    57         protected BufferCache initialValue() {
    57         protected BufferCache initialValue() {
    58             return new BufferCache();
    58             return new BufferCache();
       
    59         }
       
    60         @Override
       
    61         protected void threadTerminated(BufferCache cache) { // will never be null
       
    62             while (!cache.isEmpty()) {
       
    63                 ByteBuffer bb = cache.removeFirst();
       
    64                 free(bb);
       
    65             }
    59         }
    66         }
    60     };
    67     };
    61 
    68 
    62     /**
    69     /**
    63      * Returns the max size allowed for a cached temp buffers, in
    70      * Returns the max size allowed for a cached temp buffers, in