9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package sun.nio.ch; 27 28 import java.io.FileDescriptor; 29 import java.lang.reflect.Constructor; 30 import java.lang.reflect.InvocationTargetException; 31 import java.nio.ByteBuffer; 32 import java.nio.MappedByteBuffer; 33 import java.security.AccessController; 34 import java.security.PrivilegedAction; 35 import java.util.Collection; 36 import java.util.Iterator; 37 import java.util.Set; 38 import jdk.internal.misc.Unsafe; 39 import sun.security.action.GetPropertyAction; 40 import java.io.IOException; 41 42 public class Util { 43 44 // -- Caches -- 45 46 // The number of temp buffers in our pool 47 private static final int TEMP_BUF_POOL_SIZE = IOUtil.IOV_MAX; 48 49 // The max size allowed for a cached temp buffer, in bytes 50 private static final long MAX_CACHED_BUFFER_SIZE = getMaxCachedBufferSize(); 51 52 // Per-thread cache of temporary direct buffers 53 private static ThreadLocal<BufferCache> bufferCache = 54 new ThreadLocal<BufferCache>() 55 { 56 @Override 57 protected BufferCache initialValue() { 58 return new BufferCache(); 59 } 60 }; 61 62 /** 63 * Returns the max size allowed for a cached temp buffers, in 64 * bytes. It defaults to Long.MAX_VALUE. It can be set with the 65 * jdk.nio.maxCachedBufferSize property. Even though 66 * ByteBuffer.capacity() returns an int, we're using a long here 67 * for potential future-proofing. 68 */ 69 private static long getMaxCachedBufferSize() { 70 String s = GetPropertyAction 71 .privilegedGetProperty("jdk.nio.maxCachedBufferSize"); 72 if (s != null) { 73 try { 74 long m = Long.parseLong(s); 75 if (m >= 0) { 76 return m; 77 } else { 78 // if it's negative, ignore the system property | 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package sun.nio.ch; 27 28 import java.io.FileDescriptor; 29 import java.io.IOException; 30 import java.lang.reflect.Constructor; 31 import java.lang.reflect.InvocationTargetException; 32 import java.nio.ByteBuffer; 33 import java.nio.MappedByteBuffer; 34 import java.security.AccessController; 35 import java.security.PrivilegedAction; 36 import java.util.Collection; 37 import java.util.Iterator; 38 import java.util.Set; 39 40 import jdk.internal.misc.JdkThreadLocal; 41 import jdk.internal.misc.Unsafe; 42 import sun.security.action.GetPropertyAction; 43 44 public class Util { 45 46 // -- Caches -- 47 48 // The number of temp buffers in our pool 49 private static final int TEMP_BUF_POOL_SIZE = IOUtil.IOV_MAX; 50 51 // The max size allowed for a cached temp buffer, in bytes 52 private static final long MAX_CACHED_BUFFER_SIZE = getMaxCachedBufferSize(); 53 54 // Per-thread cache of temporary direct buffers 55 private static ThreadLocal<BufferCache> bufferCache = new JdkThreadLocal<>() { 56 @Override 57 protected BufferCache computeInitialValue() { 58 return new BufferCache(); 59 } 60 @Override 61 protected void threadTerminated() { 62 BufferCache cache = get(); // will never be null 63 while (!cache.isEmpty()) { 64 ByteBuffer bb = cache.removeFirst(); 65 free(bb); 66 } 67 } 68 }; 69 70 /** 71 * Returns the max size allowed for a cached temp buffers, in 72 * bytes. It defaults to Long.MAX_VALUE. It can be set with the 73 * jdk.nio.maxCachedBufferSize property. Even though 74 * ByteBuffer.capacity() returns an int, we're using a long here 75 * for potential future-proofing. 76 */ 77 private static long getMaxCachedBufferSize() { 78 String s = GetPropertyAction 79 .privilegedGetProperty("jdk.nio.maxCachedBufferSize"); 80 if (s != null) { 81 try { 82 long m = Long.parseLong(s); 83 if (m >= 0) { 84 return m; 85 } else { 86 // if it's negative, ignore the system property |