Module 
Package org.xnio

Class IoUtils


  • public final class IoUtils
    extends java.lang.Object
    General I/O utility methods.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static IoFuture.Notifier<java.lang.Object,​java.io.Closeable> attachmentClosingNotifier()
      Get a notifier that closes the attachment.
      static void awaitAll​(IoFuture<?>... futures)
      Wait for all the futures to complete.
      static void awaitAllInterruptibly​(IoFuture<?>... futures)
      Wait for all the futures to complete.
      static <I,​O>
      IoFuture<? extends O>
      cast​(IoFuture<I> parent, java.lang.Class<O> type)
      Create an IoFuture which wraps another IoFuture, but returns a different type.
      static <T extends java.nio.channels.Channel>
      IoFuture.Notifier<T,​ChannelListener<? super T>>
      channelListenerNotifier()
      Get the notifier that invokes the channel listener given as an attachment.
      static Cancellable closingCancellable​(java.io.Closeable c)
      A cancellable which closes the given resource on cancel.
      static IoFuture.Notifier<java.io.Closeable,​java.lang.Void> closingNotifier()
      Get a notifier that closes the result.
      static java.util.concurrent.Executor directExecutor()
      Get the direct executor.
      static <T> java.util.concurrent.Future<T> getFuture​(IoFuture<T> ioFuture)
      Get a java.util.concurrent-style Future instance wrapper for an IoFuture instance.
      static <T> IoFuture.Notifier<T,​FutureResult<T>> getManagerNotifier()
      Get a notifier which forwards the result to another IoFuture's manager.
      static <T extends java.nio.channels.Channel>
      ChannelSource<T>
      getRetryingChannelSource​(ChannelSource<T> delegate, int maxTries)
      A channel source which tries to acquire a channel from a delegate channel source the given number of times before giving up.
      static java.util.Random getThreadLocalRandom()
      Get a thread-local RNG.
      static Cancellable nullCancellable()
      Get the null cancellable.
      static java.io.Closeable nullCloseable()
      Get the null closeable.
      static java.util.concurrent.Executor nullExecutor()
      Get the null executor.
      static <T> IoFuture.Notifier<T,​Result<T>> resultNotifier()
      Get the result notifier.
      static <T> IoFuture.Notifier<T,​java.lang.Void> runnableNotifier​(java.lang.Runnable runnable)
      Get a notifier that runs the supplied action.
      static void safeClose​(java.io.Closeable resource)
      Close a resource, logging an error if an error occurs.
      static void safeClose​(java.io.Closeable... resources)
      Close a series of resources, logging errors if they occur.
      static void safeClose​(java.lang.AutoCloseable resource)
      Close a resource, logging an error if an error occurs.
      static void safeClose​(java.net.DatagramSocket resource)
      Close a resource, logging an error if an error occurs.
      static void safeClose​(java.net.ServerSocket resource)
      Close a resource, logging an error if an error occurs.
      static void safeClose​(java.net.Socket resource)
      Close a resource, logging an error if an error occurs.
      static void safeClose​(java.nio.channels.Selector resource)
      Close a resource, logging an error if an error occurs.
      static void safeClose​(java.util.logging.Handler resource)
      Close a resource, logging an error if an error occurs.
      static void safeClose​(java.util.zip.ZipFile resource)
      Close a resource, logging an error if an error occurs.
      static void safeClose​(IoFuture<? extends java.io.Closeable> futureResource)
      Close a future resource, logging an error if an error occurs.
      static void safeShutdownReads​(SuspendableReadChannel channel)
      Safely shutdown reads on the given channel.
      static long transfer​(java.nio.channels.ReadableByteChannel source, long count, java.nio.ByteBuffer throughBuffer, java.nio.channels.WritableByteChannel sink)
      Platform-independent channel-to-channel transfer method.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • directExecutor

        public static java.util.concurrent.Executor directExecutor()
        Get the direct executor. This is an executor that executes the provided task in the same thread.
        Returns:
        a direct executor
      • nullExecutor

        public static java.util.concurrent.Executor nullExecutor()
        Get the null executor. This is an executor that never actually executes the provided task.
        Returns:
        a null executor
      • nullCloseable

        public static java.io.Closeable nullCloseable()
        Get the null closeable. This is a simple Closeable instance that does nothing when its close() method is invoked.
        Returns:
        the null closeable
      • safeClose

        public static void safeClose​(java.lang.AutoCloseable resource)
        Close a resource, logging an error if an error occurs.
        Parameters:
        resource - the resource to close
      • safeClose

        public static void safeClose​(java.io.Closeable resource)
        Close a resource, logging an error if an error occurs.
        Parameters:
        resource - the resource to close
      • safeClose

        public static void safeClose​(java.io.Closeable... resources)
        Close a series of resources, logging errors if they occur.
        Parameters:
        resources - the resources to close
      • safeClose

        public static void safeClose​(java.net.Socket resource)
        Close a resource, logging an error if an error occurs.
        Parameters:
        resource - the resource to close
      • safeClose

        public static void safeClose​(java.net.DatagramSocket resource)
        Close a resource, logging an error if an error occurs.
        Parameters:
        resource - the resource to close
      • safeClose

        public static void safeClose​(java.nio.channels.Selector resource)
        Close a resource, logging an error if an error occurs.
        Parameters:
        resource - the resource to close
      • safeClose

        public static void safeClose​(java.net.ServerSocket resource)
        Close a resource, logging an error if an error occurs.
        Parameters:
        resource - the resource to close
      • safeClose

        public static void safeClose​(java.util.zip.ZipFile resource)
        Close a resource, logging an error if an error occurs.
        Parameters:
        resource - the resource to close
      • safeClose

        public static void safeClose​(java.util.logging.Handler resource)
        Close a resource, logging an error if an error occurs.
        Parameters:
        resource - the resource to close
      • safeClose

        public static void safeClose​(IoFuture<? extends java.io.Closeable> futureResource)
        Close a future resource, logging an error if an error occurs. Attempts to cancel the operation if it is still in progress.
        Parameters:
        futureResource - the resource to close
      • attachmentClosingNotifier

        public static IoFuture.Notifier<java.lang.Object,​java.io.Closeable> attachmentClosingNotifier()
        Get a notifier that closes the attachment.
        Returns:
        a notifier which will close its attachment
      • closingNotifier

        public static IoFuture.Notifier<java.io.Closeable,​java.lang.Void> closingNotifier()
        Get a notifier that closes the result.
        Returns:
        a notifier which will close the result of the operation (if successful)
      • runnableNotifier

        public static <T> IoFuture.Notifier<T,​java.lang.Void> runnableNotifier​(java.lang.Runnable runnable)
        Get a notifier that runs the supplied action.
        Type Parameters:
        T - the future type (not used)
        Parameters:
        runnable - the notifier type
        Returns:
        a notifier which will run the given command
      • resultNotifier

        public static <T> IoFuture.Notifier<T,​Result<T>> resultNotifier()
        Get the result notifier. This notifier will forward the result of the IoFuture to the attached Result.
        Type Parameters:
        T - the result type
        Returns:
        the notifier
      • channelListenerNotifier

        public static <T extends java.nio.channels.Channel> IoFuture.Notifier<T,​ChannelListener<? super T>> channelListenerNotifier()
        Get the notifier that invokes the channel listener given as an attachment.
        Type Parameters:
        T - the channel type
        Returns:
        the notifier
      • getFuture

        public static <T> java.util.concurrent.Future<T> getFuture​(IoFuture<T> ioFuture)
        Get a java.util.concurrent-style Future instance wrapper for an IoFuture instance.
        Parameters:
        ioFuture - the IoFuture to wrap
        Returns:
        a Future
      • awaitAll

        public static void awaitAll​(IoFuture<?>... futures)
        Wait for all the futures to complete.
        Parameters:
        futures - the futures to wait for
      • awaitAllInterruptibly

        public static void awaitAllInterruptibly​(IoFuture<?>... futures)
                                          throws java.lang.InterruptedException
        Wait for all the futures to complete.
        Parameters:
        futures - the futures to wait for
        Throws:
        java.lang.InterruptedException - if the current thread is interrupted while waiting
      • cast

        public static <I,​O> IoFuture<? extends O> cast​(IoFuture<I> parent,
                                                             java.lang.Class<O> type)
        Create an IoFuture which wraps another IoFuture, but returns a different type.
        Type Parameters:
        I - the type of the original result
        O - the type of the wrapped result
        Parameters:
        parent - the original IoFuture
        type - the class of the new IoFuture
        Returns:
        a wrapper IoFuture
      • safeShutdownReads

        public static void safeShutdownReads​(SuspendableReadChannel channel)
        Safely shutdown reads on the given channel.
        Parameters:
        channel - the channel
      • transfer

        public static long transfer​(java.nio.channels.ReadableByteChannel source,
                                    long count,
                                    java.nio.ByteBuffer throughBuffer,
                                    java.nio.channels.WritableByteChannel sink)
                             throws java.io.IOException
        Platform-independent channel-to-channel transfer method. Uses regular read and write operations to move bytes from the source channel to the sink channel. After this call, the throughBuffer should be checked for remaining bytes; if there are any, they should be written to the sink channel before proceeding. This method may be used with NIO channels, XNIO channels, or a combination of the two.

        If either or both of the given channels are blocking channels, then this method may block.

        Parameters:
        source - the source channel to read bytes from
        count - the number of bytes to transfer (must be >= 0L)
        throughBuffer - the buffer to transfer through (must not be null)
        sink - the sink channel to write bytes to
        Returns:
        the number of bytes actually transferred (possibly 0)
        Throws:
        java.io.IOException - if an I/O error occurs during the transfer of bytes
      • getManagerNotifier

        public static <T> IoFuture.Notifier<T,​FutureResult<T>> getManagerNotifier()
        Get a notifier which forwards the result to another IoFuture's manager.
        Type Parameters:
        T - the channel type
        Returns:
        the notifier
      • getRetryingChannelSource

        public static <T extends java.nio.channels.Channel> ChannelSource<T> getRetryingChannelSource​(ChannelSource<T> delegate,
                                                                                                      int maxTries)
                                                                                               throws java.lang.IllegalArgumentException
        A channel source which tries to acquire a channel from a delegate channel source the given number of times before giving up.
        Type Parameters:
        T - the channel type
        Parameters:
        delegate - the delegate channel source
        maxTries - the number of times to retry
        Returns:
        the retrying channel source
        Throws:
        java.lang.IllegalArgumentException
      • closingCancellable

        public static Cancellable closingCancellable​(java.io.Closeable c)
        A cancellable which closes the given resource on cancel.
        Parameters:
        c - the resource
        Returns:
        the cancellable
      • nullCancellable

        public static Cancellable nullCancellable()
        Get the null cancellable.
        Returns:
        the null cancellable
      • getThreadLocalRandom

        public static java.util.Random getThreadLocalRandom()
        Get a thread-local RNG. Do not share this instance with other threads.
        Returns:
        the thread-local RNG