< prev index next >

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

Print this page

458             completed = true;
459             return bytesRead;
460 
461         } finally {
462             releaseScopes(handleReleasers);
463             // if an error occurred then clear refs to buffers and return any shadow
464             // buffers to cache
465             if (!completed) {
466                 for (int j=0; j<iov_len; j++) {
467                     ByteBuffer shadow = vec.getShadow(j);
468                     if (shadow != null)
469                         Util.offerLastTemporaryDirectBuffer(shadow);
470                     vec.clearRefs(j);
471                 }
472             }
473         }
474     }
475 
476     private static final JavaNioAccess NIO_ACCESS = SharedSecrets.getJavaNioAccess();
477 
478     static Scope.Handle acquireScope(ByteBuffer bb, boolean async) {
479         return NIO_ACCESS.acquireScope(bb, async);
480     }
481 
482     private static void releaseScope(Scope.Handle handle) {
483         if (handle == null)
484             return;
485         try {
486             handle.scope().release(handle);
487         } catch (Exception e) {
488             throw new IllegalStateException(e);
489         }
490     }
491 
492     static Runnable acquireScopes(ByteBuffer[] buffers) {
493         return acquireScopes(null, buffers);
494     }
495 
496     static Runnable acquireScopes(ByteBuffer buf, ByteBuffer[] buffers) {
497         if (buffers == null) {
498             assert buf != null;
499             return IOUtil.Releaser.ofNullable(IOUtil.acquireScope(buf, true));
500         } else {
501             assert buf == null;
502             Runnable handleReleasers = null;
503             for (var b : buffers) {
504                 var h = IOUtil.acquireScope(b, true);
505                 if (h != null) {
506                     handleReleasers = IOUtil.LinkedRunnable.of(IOUtil.Releaser.of(h), handleReleasers);

518     static record LinkedRunnable(Runnable node, Runnable next)
519         implements Runnable
520     {
521         LinkedRunnable {
522             Objects.requireNonNull(node);
523         }
524         @Override
525         public void run() {
526             try {
527                 node.run();
528             } finally {
529                 if (next != null)
530                     next.run();
531             }
532         }
533         static LinkedRunnable of(Runnable first, Runnable second) {
534             return new LinkedRunnable(first, second);
535         }
536     }
537 
538     static record Releaser(Scope.Handle handle) implements Runnable {
539         Releaser { Objects.requireNonNull(handle) ; }
540         @Override public void run() { releaseScope(handle); }
541         static Runnable of(Scope.Handle handle) { return new Releaser(handle); }
542         static Runnable ofNullable(Scope.Handle handle) {
543             if (handle == null)
544                 return () -> { };
545             return new Releaser(handle);
546         }
547     }
548 
549     static long bufferAddress(ByteBuffer buf) {
550         return NIO_ACCESS.getBufferAddress(buf);
551     }
552 
553     public static FileDescriptor newFD(int i) {
554         FileDescriptor fd = new FileDescriptor();
555         setfdVal(fd, i);
556         return fd;
557     }
558 
559     static native boolean randomBytes(byte[] someBytes);
560 
561     /**
562      * Returns two file descriptors for a pipe encoded in a long.

458             completed = true;
459             return bytesRead;
460 
461         } finally {
462             releaseScopes(handleReleasers);
463             // if an error occurred then clear refs to buffers and return any shadow
464             // buffers to cache
465             if (!completed) {
466                 for (int j=0; j<iov_len; j++) {
467                     ByteBuffer shadow = vec.getShadow(j);
468                     if (shadow != null)
469                         Util.offerLastTemporaryDirectBuffer(shadow);
470                     vec.clearRefs(j);
471                 }
472             }
473         }
474     }
475 
476     private static final JavaNioAccess NIO_ACCESS = SharedSecrets.getJavaNioAccess();
477 
478     static Runnable acquireScope(ByteBuffer bb, boolean async) {
479         return NIO_ACCESS.acquireScope(bb, async);
480     }
481 
482     private static void releaseScope(Runnable handle) {
483         if (handle == null)
484             return;
485         try {
486             handle.run();
487         } catch (Exception e) {
488             throw new IllegalStateException(e);
489         }
490     }
491 
492     static Runnable acquireScopes(ByteBuffer[] buffers) {
493         return acquireScopes(null, buffers);
494     }
495 
496     static Runnable acquireScopes(ByteBuffer buf, ByteBuffer[] buffers) {
497         if (buffers == null) {
498             assert buf != null;
499             return IOUtil.Releaser.ofNullable(IOUtil.acquireScope(buf, true));
500         } else {
501             assert buf == null;
502             Runnable handleReleasers = null;
503             for (var b : buffers) {
504                 var h = IOUtil.acquireScope(b, true);
505                 if (h != null) {
506                     handleReleasers = IOUtil.LinkedRunnable.of(IOUtil.Releaser.of(h), handleReleasers);

518     static record LinkedRunnable(Runnable node, Runnable next)
519         implements Runnable
520     {
521         LinkedRunnable {
522             Objects.requireNonNull(node);
523         }
524         @Override
525         public void run() {
526             try {
527                 node.run();
528             } finally {
529                 if (next != null)
530                     next.run();
531             }
532         }
533         static LinkedRunnable of(Runnable first, Runnable second) {
534             return new LinkedRunnable(first, second);
535         }
536     }
537 
538     static record Releaser(Runnable handle) implements Runnable {
539         Releaser { Objects.requireNonNull(handle) ; }
540         @Override public void run() { releaseScope(handle); }
541         static Runnable of(Runnable handle) { return new Releaser(handle); }
542         static Runnable ofNullable(Runnable handle) {
543             if (handle == null)
544                 return () -> { };
545             return new Releaser(handle);
546         }
547     }
548 
549     static long bufferAddress(ByteBuffer buf) {
550         return NIO_ACCESS.getBufferAddress(buf);
551     }
552 
553     public static FileDescriptor newFD(int i) {
554         FileDescriptor fd = new FileDescriptor();
555         setfdVal(fd, i);
556         return fd;
557     }
558 
559     static native boolean randomBytes(byte[] someBytes);
560 
561     /**
562      * Returns two file descriptors for a pipe encoded in a long.
< prev index next >