XNIO API 2.1.0.CR2

org.jboss.xnio
Class IoUtils

java.lang.Object
  extended by org.jboss.xnio.IoUtils

public final class IoUtils
extends Object

General I/O utility methods.


Method Summary
static IoFuture.Notifier<Object,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, Class<O> type)
          Create an IoFuture which wraps another IoFuture, but returns a different type.
static
<T extends Channel>
IoFuture.Notifier<T,ChannelListener<? super T>>
channelListenerNotifier()
          Get the notifier that invokes the channel listener given as an attachment.
static CloseableExecutor closeableExecutor(ExecutorService executorService, long timeout, TimeUnit unit)
          Get a closeable executor wrapper for an ExecutorService.
static Cancellable closingCancellable(Closeable c)
          A cancellable which closes the given resource on cancel.
static ChannelListener<Channel> closingChannelListener()
          Get a channel listener which closes the channel when notified.
static IoFuture.Notifier<Closeable,Void> closingNotifier()
          Get a notifier that closes the result.
static Executor directExecutor()
          Get the direct executor.
static
<T extends Channel>
ChannelListener<T>
executorChannelListener(ChannelListener<T> listener, Executor executor)
          Get a channel listener which executes a delegate channel listener via an executor.
static
<T extends Channel>
Runnable
getChannelListenerTask(T channel, ChannelListener<? super T> channelListener)
          Get a task which invokes the given channel listener on the given channel.
static
<T extends Channel>
ChannelListener.Setter<T>
getDelegatingSetter(ChannelListener.Setter<? extends Channel> target, T realChannel)
          Get a channel listener setter which delegates to the given target setter with a different channel type.
static
<T> 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 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
<T extends Channel>
ChannelListener.Setter<T>
getSetter(AtomicReference<ChannelListener<? super T>> atomicReference)
          Get a setter based on an atomic reference.
static
<T extends Channel,C>
ChannelListener.Setter<T>
getSetter(C channel, AtomicReferenceFieldUpdater<C,ChannelListener> updater)
          Get a setter based on an atomic reference field updater.
static
<T extends Channel>
void
invokeChannelListener(Executor executor, T channel, ChannelListener<? super T> channelListener)
          Invoke a channel listener on a given channel, logging any errors, using the given executor.
static
<T extends Channel>
boolean
invokeChannelListener(T channel, ChannelListener<? super T> channelListener)
          Invoke a channel listener on a given channel, logging any errors.
static Cancellable nullCancellable()
          Get the null cancellable.
static ChannelListener<Channel> nullChannelListener()
          Get a channel listener which does nothing.
static Closeable nullCloseable()
          Get the null closeable.
static Executor nullExecutor()
          Get the null executor.
static
<T extends Channel>
ChannelListener.Setter<T>
nullSetter()
          Get a channel listener setter which does nothing.
static
<T> IoFuture.Notifier<T,Result<T>>
resultNotifier()
          Get the result notifier.
static
<T> IoFuture.Notifier<T,Void>
runnableNotifier(Runnable runnable)
          Get a notifier that runs the supplied action.
static void safeClose(Closeable 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(Handler resource)
          Close a resource, logging an error if an error occurs.
static void safeClose(IoFuture<? extends Closeable> futureResource)
          Close a future resource, logging an error if an error occurs.
static void safeClose(Selector 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 safeClose(Socket resource)
          Close a resource, logging an error if an error occurs.
static void safeClose(ZipFile resource)
          Close a resource, logging an error if an error occurs.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

directExecutor

public static 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 Executor nullExecutor()
Get the null executor. This is an executor that never actually executes the provided task.

Returns:
a null executor

closeableExecutor

public static CloseableExecutor closeableExecutor(ExecutorService executorService,
                                                  long timeout,
                                                  TimeUnit unit)
Get a closeable executor wrapper for an ExecutorService. The given timeout is used to determine how long the close() method will wait for a clean shutdown before the executor is shut down forcefully.

Parameters:
executorService - the executor service
timeout - the timeout
unit - the unit for the timeout
Returns:
a new closeable executor

nullCloseable

public static 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(Closeable resource)
Close a resource, logging an error if an error occurs.

Parameters:
resource - the resource to close

safeClose

public static void safeClose(Socket resource)
Close a resource, logging an error if an error occurs.

Parameters:
resource - the resource to close

safeClose

public static void safeClose(DatagramSocket resource)
Close a resource, logging an error if an error occurs.

Parameters:
resource - the resource to close

safeClose

public static void safeClose(Selector resource)
Close a resource, logging an error if an error occurs.

Parameters:
resource - the resource to close

safeClose

public static void safeClose(ServerSocket resource)
Close a resource, logging an error if an error occurs.

Parameters:
resource - the resource to close

safeClose

public static void safeClose(ZipFile resource)
Close a resource, logging an error if an error occurs.

Parameters:
resource - the resource to close

safeClose

public static void safeClose(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 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<Object,Closeable> attachmentClosingNotifier()
Get a notifier that closes the attachment.

Returns:
a notifier which will close its attachment

closingNotifier

public static IoFuture.Notifier<Closeable,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,Void> runnableNotifier(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 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> 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 InterruptedException
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

public static <I,O> IoFuture<? extends O> cast(IoFuture<I> parent,
                                               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

invokeChannelListener

public static <T extends Channel> boolean invokeChannelListener(T channel,
                                                                ChannelListener<? super T> channelListener)
Invoke a channel listener on a given channel, logging any errors.

Type Parameters:
T - the channel type
Parameters:
channel - the channel
channelListener - the channel listener
Returns:
true if the listener completed successfully, or false if it failed

invokeChannelListener

public static <T extends Channel> void invokeChannelListener(Executor executor,
                                                             T channel,
                                                             ChannelListener<? super T> channelListener)
Invoke a channel listener on a given channel, logging any errors, using the given executor.

Type Parameters:
T - the channel type
Parameters:
executor - the executor
channel - the channel
channelListener - the channel listener

getChannelListenerTask

public static <T extends Channel> Runnable getChannelListenerTask(T channel,
                                                                  ChannelListener<? super T> channelListener)
Get a task which invokes the given channel listener on the given channel.

Type Parameters:
T - the channel type
Parameters:
channel - the channel
channelListener - the channel listener
Returns:
the runnable task

closingChannelListener

public static ChannelListener<Channel> closingChannelListener()
Get a channel listener which closes the channel when notified.

Returns:
the channel listener

nullChannelListener

public static ChannelListener<Channel> nullChannelListener()
Get a channel listener which does nothing.

Returns:
the null channel listener

getSetter

public static <T extends Channel,C> ChannelListener.Setter<T> getSetter(C channel,
                                                                        AtomicReferenceFieldUpdater<C,ChannelListener> updater)
Get a setter based on an atomic reference field updater. Used by channel implementations to avoid having to define an anonymous class for each listener field.

Type Parameters:
T - the channel type
C - the holding class
Parameters:
channel - the channel
updater - the updater
Returns:
the setter

getSetter

public static <T extends Channel> ChannelListener.Setter<T> getSetter(AtomicReference<ChannelListener<? super T>> atomicReference)
Get a setter based on an atomic reference. Used by channel implementations to avoid having to define an anonymous class for each listener field.

Type Parameters:
T - the channel type
Parameters:
atomicReference - the atomic reference
Returns:
the setter

getDelegatingSetter

public static <T extends Channel> ChannelListener.Setter<T> getDelegatingSetter(ChannelListener.Setter<? extends Channel> target,
                                                                                T realChannel)
Get a channel listener setter which delegates to the given target setter with a different channel type.

Type Parameters:
T - the real channel type
Parameters:
target - the target setter
realChannel - the channel to send in to the listener
Returns:
the delegating setter

nullSetter

public static <T extends Channel> ChannelListener.Setter<T> nullSetter()
Get a channel listener setter which does nothing.

Type Parameters:
T - the channel type
Returns:
a setter which does nothing

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 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.

Type Parameters:
T - the channel type
Parameters:
delegate - the delegate channel source
maxTries - the number of times to retry
Returns:
the retrying channel source

closingCancellable

public static Cancellable closingCancellable(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

executorChannelListener

public static <T extends Channel> ChannelListener<T> executorChannelListener(ChannelListener<T> listener,
                                                                             Executor executor)
Get a channel listener which executes a delegate channel listener via an executor. If an exception occurs submitting the task, the associated channel is closed.

Type Parameters:
T - the channel type
Parameters:
listener - the listener to invoke
executor - the executor with which to invoke the listener
Returns:
a delegating channel listener

XNIO API 2.1.0.CR2

Copyright © 2010 JBoss, a division of Red Hat, Inc.