< prev index next >

src/java.base/share/classes/sun/nio/ch/Util.java

Print this page
rev 14210 : 8154231: Simplify access to System properties from JDK code
Reviewed-by: rriggs


  47     private static final long MAX_CACHED_BUFFER_SIZE = getMaxCachedBufferSize();
  48 
  49     // Per-thread cache of temporary direct buffers
  50     private static ThreadLocal<BufferCache> bufferCache =
  51         new ThreadLocal<BufferCache>()
  52     {
  53         @Override
  54         protected BufferCache initialValue() {
  55             return new BufferCache();
  56         }
  57     };
  58 
  59     /**
  60      * Returns the max size allowed for a cached temp buffers, in
  61      * bytes. It defaults to Long.MAX_VALUE. It can be set with the
  62      * jdk.nio.maxCachedBufferSize property. Even though
  63      * ByteBuffer.capacity() returns an int, we're using a long here
  64      * for potential future-proofing.
  65      */
  66     private static long getMaxCachedBufferSize() {
  67         String s = java.security.AccessController.doPrivileged(
  68             new PrivilegedAction<String>() {
  69                 @Override
  70                 public String run() {
  71                     return System.getProperty("jdk.nio.maxCachedBufferSize");
  72                 }
  73             });
  74         if (s != null) {
  75             try {
  76                 long m = Long.parseLong(s);
  77                 if (m >= 0) {
  78                     return m;
  79                 } else {
  80                     // if it's negative, ignore the system property
  81                 }
  82             } catch (NumberFormatException e) {
  83                 // if the string is not well formed, ignore the system property
  84             }
  85         }
  86         return Long.MAX_VALUE;
  87     }
  88 
  89     /**
  90      * Returns true if a buffer of this size is too large to be
  91      * added to the buffer cache, false otherwise.
  92      */
  93     private static boolean isBufferTooLarge(int size) {


 454                              addr,
 455                              fd,
 456                              unmapper });
 457         } catch (InstantiationException |
 458                  IllegalAccessException |
 459                  InvocationTargetException e) {
 460             throw new InternalError(e);
 461         }
 462         return dbb;
 463     }
 464 
 465 
 466     // -- Bug compatibility --
 467 
 468     private static volatile String bugLevel;
 469 
 470     static boolean atBugLevel(String bl) {              // package-private
 471         if (bugLevel == null) {
 472             if (!jdk.internal.misc.VM.isBooted())
 473                 return false;
 474             String value = AccessController.doPrivileged(
 475                 new GetPropertyAction("sun.nio.ch.bugLevel"));
 476             bugLevel = (value != null) ? value : "";
 477         }
 478         return bugLevel.equals(bl);
 479     }
 480 
 481 }


  47     private static final long MAX_CACHED_BUFFER_SIZE = getMaxCachedBufferSize();
  48 
  49     // Per-thread cache of temporary direct buffers
  50     private static ThreadLocal<BufferCache> bufferCache =
  51         new ThreadLocal<BufferCache>()
  52     {
  53         @Override
  54         protected BufferCache initialValue() {
  55             return new BufferCache();
  56         }
  57     };
  58 
  59     /**
  60      * Returns the max size allowed for a cached temp buffers, in
  61      * bytes. It defaults to Long.MAX_VALUE. It can be set with the
  62      * jdk.nio.maxCachedBufferSize property. Even though
  63      * ByteBuffer.capacity() returns an int, we're using a long here
  64      * for potential future-proofing.
  65      */
  66     private static long getMaxCachedBufferSize() {
  67         String s = GetPropertyAction.getProperty("jdk.nio.maxCachedBufferSize");






  68         if (s != null) {
  69             try {
  70                 long m = Long.parseLong(s);
  71                 if (m >= 0) {
  72                     return m;
  73                 } else {
  74                     // if it's negative, ignore the system property
  75                 }
  76             } catch (NumberFormatException e) {
  77                 // if the string is not well formed, ignore the system property
  78             }
  79         }
  80         return Long.MAX_VALUE;
  81     }
  82 
  83     /**
  84      * Returns true if a buffer of this size is too large to be
  85      * added to the buffer cache, false otherwise.
  86      */
  87     private static boolean isBufferTooLarge(int size) {


 448                              addr,
 449                              fd,
 450                              unmapper });
 451         } catch (InstantiationException |
 452                  IllegalAccessException |
 453                  InvocationTargetException e) {
 454             throw new InternalError(e);
 455         }
 456         return dbb;
 457     }
 458 
 459 
 460     // -- Bug compatibility --
 461 
 462     private static volatile String bugLevel;
 463 
 464     static boolean atBugLevel(String bl) {              // package-private
 465         if (bugLevel == null) {
 466             if (!jdk.internal.misc.VM.isBooted())
 467                 return false;
 468             String value = GetPropertyAction.getProperty("sun.nio.ch.bugLevel");

 469             bugLevel = (value != null) ? value : "";
 470         }
 471         return bugLevel.equals(bl);
 472     }
 473 
 474 }
< prev index next >