< prev index next >

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

Print this page
rev 57242 : imported patch 8234049.patch


  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.TerminatingThreadLocal;
  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 TerminatingThreadLocal<>() {
  56         @Override
  57         protected BufferCache initialValue() {
  58             return new BufferCache();
  59         }


 399     private static int pageSize = -1;
 400 
 401     static int pageSize() {
 402         if (pageSize == -1)
 403             pageSize = unsafe().pageSize();
 404         return pageSize;
 405     }
 406 
 407     private static volatile Constructor<?> directByteBufferConstructor;
 408 
 409     private static void initDBBConstructor() {
 410         AccessController.doPrivileged(new PrivilegedAction<Void>() {
 411                 public Void run() {
 412                     try {
 413                         Class<?> cl = Class.forName("java.nio.DirectByteBuffer");
 414                         Constructor<?> ctor = cl.getDeclaredConstructor(
 415                             new Class<?>[] { int.class,
 416                                              long.class,
 417                                              FileDescriptor.class,
 418                                              Runnable.class,
 419                                              boolean.class });
 420                         ctor.setAccessible(true);
 421                         directByteBufferConstructor = ctor;
 422                     } catch (ClassNotFoundException   |
 423                              NoSuchMethodException    |
 424                              IllegalArgumentException |
 425                              ClassCastException x) {
 426                         throw new InternalError(x);
 427                     }
 428                     return null;
 429                 }});
 430     }
 431 
 432     static MappedByteBuffer newMappedByteBuffer(int size, long addr,
 433                                                 FileDescriptor fd,
 434                                                 Runnable unmapper,
 435                                                 boolean isSync)
 436     {
 437         MappedByteBuffer dbb;
 438         if (directByteBufferConstructor == null)
 439             initDBBConstructor();
 440         try {
 441             dbb = (MappedByteBuffer)directByteBufferConstructor.newInstance(
 442               new Object[] { size,
 443                              addr,
 444                              fd,
 445                              unmapper,
 446                              isSync});
 447         } catch (InstantiationException |
 448                  IllegalAccessException |
 449                  InvocationTargetException e) {
 450             throw new InternalError(e);
 451         }
 452         return dbb;
 453     }
 454 
 455     private static volatile Constructor<?> directByteBufferRConstructor;
 456 
 457     private static void initDBBRConstructor() {
 458         AccessController.doPrivileged(new PrivilegedAction<Void>() {
 459                 public Void run() {
 460                     try {
 461                         Class<?> cl = Class.forName("java.nio.DirectByteBufferR");
 462                         Constructor<?> ctor = cl.getDeclaredConstructor(
 463                             new Class<?>[] { int.class,
 464                                              long.class,
 465                                              FileDescriptor.class,
 466                                              Runnable.class,
 467                                              boolean.class });
 468                         ctor.setAccessible(true);
 469                         directByteBufferRConstructor = ctor;
 470                     } catch (ClassNotFoundException |
 471                              NoSuchMethodException |
 472                              IllegalArgumentException |
 473                              ClassCastException x) {
 474                         throw new InternalError(x);
 475                     }
 476                     return null;
 477                 }});
 478     }
 479 
 480     static MappedByteBuffer newMappedByteBufferR(int size, long addr,
 481                                                  FileDescriptor fd,
 482                                                  Runnable unmapper,
 483                                                  boolean isSync)
 484     {
 485         MappedByteBuffer dbb;
 486         if (directByteBufferRConstructor == null)
 487             initDBBRConstructor();
 488         try {
 489             dbb = (MappedByteBuffer)directByteBufferRConstructor.newInstance(
 490               new Object[] { size,
 491                              addr,
 492                              fd,
 493                              unmapper,
 494                              isSync});
 495         } catch (InstantiationException |
 496                  IllegalAccessException |
 497                  InvocationTargetException e) {
 498             throw new InternalError(e);
 499         }
 500         return dbb;
 501     }
 502 
 503     static void checkBufferPositionAligned(ByteBuffer bb,
 504                                                      int pos, int alignment)
 505         throws IOException
 506     {
 507         if (bb.alignmentOffset(pos, alignment) != 0) {
 508             throw new IOException("Current location of the bytebuffer ("
 509                 + pos + ") is not a multiple of the block size ("
 510                 + alignment + ")");
 511         }
 512     }
 513 
 514     static void checkRemainingBufferSizeAligned(int rem,




  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.access.foreign.MemorySegmentProxy;
  41 import jdk.internal.misc.TerminatingThreadLocal;
  42 import jdk.internal.misc.Unsafe;
  43 import sun.security.action.GetPropertyAction;
  44 
  45 public class Util {
  46 
  47     // -- Caches --
  48 
  49     // The number of temp buffers in our pool
  50     private static final int TEMP_BUF_POOL_SIZE = IOUtil.IOV_MAX;
  51 
  52     // The max size allowed for a cached temp buffer, in bytes
  53     private static final long MAX_CACHED_BUFFER_SIZE = getMaxCachedBufferSize();
  54 
  55     // Per-thread cache of temporary direct buffers
  56     private static ThreadLocal<BufferCache> bufferCache = new TerminatingThreadLocal<>() {
  57         @Override
  58         protected BufferCache initialValue() {
  59             return new BufferCache();
  60         }


 400     private static int pageSize = -1;
 401 
 402     static int pageSize() {
 403         if (pageSize == -1)
 404             pageSize = unsafe().pageSize();
 405         return pageSize;
 406     }
 407 
 408     private static volatile Constructor<?> directByteBufferConstructor;
 409 
 410     private static void initDBBConstructor() {
 411         AccessController.doPrivileged(new PrivilegedAction<Void>() {
 412                 public Void run() {
 413                     try {
 414                         Class<?> cl = Class.forName("java.nio.DirectByteBuffer");
 415                         Constructor<?> ctor = cl.getDeclaredConstructor(
 416                             new Class<?>[] { int.class,
 417                                              long.class,
 418                                              FileDescriptor.class,
 419                                              Runnable.class,
 420                                              boolean.class, MemorySegmentProxy.class});
 421                         ctor.setAccessible(true);
 422                         directByteBufferConstructor = ctor;
 423                     } catch (ClassNotFoundException   |
 424                              NoSuchMethodException    |
 425                              IllegalArgumentException |
 426                              ClassCastException x) {
 427                         throw new InternalError(x);
 428                     }
 429                     return null;
 430                 }});
 431     }
 432 
 433     static MappedByteBuffer newMappedByteBuffer(int size, long addr,
 434                                                 FileDescriptor fd,
 435                                                 Runnable unmapper,
 436                                                 boolean isSync)
 437     {
 438         MappedByteBuffer dbb;
 439         if (directByteBufferConstructor == null)
 440             initDBBConstructor();
 441         try {
 442             dbb = (MappedByteBuffer)directByteBufferConstructor.newInstance(
 443               new Object[] { size,
 444                              addr,
 445                              fd,
 446                              unmapper,
 447                              isSync, null});
 448         } catch (InstantiationException |
 449                  IllegalAccessException |
 450                  InvocationTargetException e) {
 451             throw new InternalError(e);
 452         }
 453         return dbb;
 454     }
 455 
 456     private static volatile Constructor<?> directByteBufferRConstructor;
 457 
 458     private static void initDBBRConstructor() {
 459         AccessController.doPrivileged(new PrivilegedAction<Void>() {
 460                 public Void run() {
 461                     try {
 462                         Class<?> cl = Class.forName("java.nio.DirectByteBufferR");
 463                         Constructor<?> ctor = cl.getDeclaredConstructor(
 464                             new Class<?>[] { int.class,
 465                                              long.class,
 466                                              FileDescriptor.class,
 467                                              Runnable.class,
 468                                              boolean.class, MemorySegmentProxy.class });
 469                         ctor.setAccessible(true);
 470                         directByteBufferRConstructor = ctor;
 471                     } catch (ClassNotFoundException |
 472                              NoSuchMethodException |
 473                              IllegalArgumentException |
 474                              ClassCastException x) {
 475                         throw new InternalError(x);
 476                     }
 477                     return null;
 478                 }});
 479     }
 480 
 481     static MappedByteBuffer newMappedByteBufferR(int size, long addr,
 482                                                  FileDescriptor fd,
 483                                                  Runnable unmapper,
 484                                                  boolean isSync)
 485     {
 486         MappedByteBuffer dbb;
 487         if (directByteBufferRConstructor == null)
 488             initDBBRConstructor();
 489         try {
 490             dbb = (MappedByteBuffer)directByteBufferRConstructor.newInstance(
 491               new Object[] { size,
 492                              addr,
 493                              fd,
 494                              unmapper,
 495                              isSync, null});
 496         } catch (InstantiationException |
 497                  IllegalAccessException |
 498                  InvocationTargetException e) {
 499             throw new InternalError(e);
 500         }
 501         return dbb;
 502     }
 503 
 504     static void checkBufferPositionAligned(ByteBuffer bb,
 505                                                      int pos, int alignment)
 506         throws IOException
 507     {
 508         if (bb.alignmentOffset(pos, alignment) != 0) {
 509             throw new IOException("Current location of the bytebuffer ("
 510                 + pos + ") is not a multiple of the block size ("
 511                 + alignment + ")");
 512         }
 513     }
 514 
 515     static void checkRemainingBufferSizeAligned(int rem,


< prev index next >