Package org.xnio
Class IoUtils
java.lang.Object
org.xnio.IoUtils
General I/O utility methods.
-
Method Summary
Modifier and TypeMethodDescriptionstatic IoFuture.Notifier<Object,
Closeable> Get a notifier that closes the attachment.static void
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> Create anIoFuture
which wraps anotherIoFuture
, but returns a different type.static <T extends Channel>
IoFuture.Notifier<T,ChannelListener<? super T>> Get the notifier that invokes the channel listener given as an attachment.static Cancellable
A cancellable which closes the given resource on cancel.static IoFuture.Notifier<Closeable,
Void> Get a notifier that closes the result.static Executor
Get the direct executor.static <T> Future<T>
Get ajava.util.concurrent
-styleFuture
instance wrapper for anIoFuture
instance.static <T> IoFuture.Notifier<T,
FutureResult<T>> Get a notifier which forwards the result to anotherIoFuture
's manager.static <T extends 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 Random
Get a thread-local RNG.static Cancellable
Get the null cancellable.static Closeable
Get the null closeable.static Executor
Get the null executor.static <T> IoFuture.Notifier<T,
Result<T>> Get the result notifier.static <T> IoFuture.Notifier<T,
Void> runnableNotifier
(Runnable runnable) Get a notifier that runs the supplied action.static void
Close a resource, logging an error if an error occurs.static void
Close a series of resources, logging errors if they occur.static void
safeClose
(AutoCloseable resource) Close a resource, logging an error if an error occurs.static void
safeClose
(DatagramSocket resource) Close a resource, logging an error if an error occurs.static void
safeClose
(ServerSocket resource) Close a resource, logging an error if an error occurs.static void
Close a resource, logging an error if an error occurs.static void
Close a resource, logging an error if an error occurs.static void
Close a resource, logging an error if an error occurs.static void
Close a resource, logging an error if an error occurs.static void
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
(ReadableByteChannel source, long count, ByteBuffer throughBuffer, WritableByteChannel sink) Platform-independent channel-to-channel transfer method.
-
Method Details
-
directExecutor
Get the direct executor. This is an executor that executes the provided task in the same thread.- Returns:
- a direct executor
-
nullExecutor
Get the null executor. This is an executor that never actually executes the provided task.- Returns:
- a null executor
-
nullCloseable
Get the null closeable. This is a simpleCloseable
instance that does nothing when itsclose()
method is invoked.- Returns:
- the null closeable
-
safeClose
Close a resource, logging an error if an error occurs.- Parameters:
resource
- the resource to close
-
safeClose
Close a resource, logging an error if an error occurs.- Parameters:
resource
- the resource to close
-
safeClose
Close a series of resources, logging errors if they occur.- Parameters:
resources
- the resources to close
-
safeClose
Close a resource, logging an error if an error occurs.- Parameters:
resource
- the resource to close
-
safeClose
Close a resource, logging an error if an error occurs.- Parameters:
resource
- the resource to close
-
safeClose
Close a resource, logging an error if an error occurs.- Parameters:
resource
- the resource to close
-
safeClose
Close a resource, logging an error if an error occurs.- Parameters:
resource
- the resource to close
-
safeClose
Close a resource, logging an error if an error occurs.- Parameters:
resource
- the resource to close
-
safeClose
Close a resource, logging an error if an error occurs.- Parameters:
resource
- the resource to close
-
safeClose
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
Get a notifier that closes the attachment.- Returns:
- a notifier which will close its attachment
-
closingNotifier
Get a notifier that closes the result.- Returns:
- a notifier which will close the result of the operation (if successful)
-
runnableNotifier
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
Get the result notifier. This notifier will forward the result of theIoFuture
to the attachedResult
.- Type Parameters:
T
- the result type- Returns:
- the notifier
-
channelListenerNotifier
public static <T extends 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
Get ajava.util.concurrent
-styleFuture
instance wrapper for anIoFuture
instance.- Parameters:
ioFuture
- theIoFuture
to wrap- Returns:
- a
Future
-
awaitAll
Wait for all the futures to complete.- Parameters:
futures
- the futures to wait for
-
awaitAllInterruptibly
Wait for all the futures to complete.- Parameters:
futures
- the futures to wait for- Throws:
InterruptedException
- if the current thread is interrupted while waiting
-
cast
Create anIoFuture
which wraps anotherIoFuture
, but returns a different type.- Type Parameters:
I
- the type of the original resultO
- the type of the wrapped result- Parameters:
parent
- the originalIoFuture
type
- the class of the newIoFuture
- Returns:
- a wrapper
IoFuture
-
safeShutdownReads
Safely shutdown reads on the given channel.- Parameters:
channel
- the channel
-
transfer
public static long transfer(ReadableByteChannel source, long count, ByteBuffer throughBuffer, WritableByteChannel sink) throws IOException Platform-independent channel-to-channel transfer method. Uses regularread
andwrite
operations to move bytes from thesource
channel to thesink
channel. After this call, thethroughBuffer
should be checked for remaining bytes; if there are any, they should be written to thesink
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 fromcount
- the number of bytes to transfer (must be >=0L
)throughBuffer
- the buffer to transfer through (must not benull
)sink
- the sink channel to write bytes to- Returns:
- the number of bytes actually transferred (possibly 0)
- Throws:
IOException
- if an I/O error occurs during the transfer of bytes
-
getManagerNotifier
Get a notifier which forwards the result to anotherIoFuture
's manager.- Type Parameters:
T
- the channel type- Returns:
- the notifier
-
getRetryingChannelSource
public static <T extends Channel> ChannelSource<T> getRetryingChannelSource(ChannelSource<T> delegate, int maxTries) throws 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 sourcemaxTries
- the number of times to retry- Returns:
- the retrying channel source
- Throws:
IllegalArgumentException
-
closingCancellable
A cancellable which closes the given resource on cancel.- Parameters:
c
- the resource- Returns:
- the cancellable
-
nullCancellable
Get the null cancellable.- Returns:
- the null cancellable
-
getThreadLocalRandom
Get a thread-local RNG. Do not share this instance with other threads.- Returns:
- the thread-local RNG
-