XNIO version 1.2.0.GA

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
<T> IoFuture.Notifier<T,Closeable>
attachmentClosingNotifier()
          Get a notifier that closes the attachment.
static void awaitAll(IoFuture<?>... futures)
           
static void awaitAllInterruptibly(IoFuture<?>... futures)
           
static CloseableExecutor closeableExecutor(ExecutorService executorService, long timeout, TimeUnit unit)
          Get a closeable executor wrapper for an ExecutorService.
static
<T extends Closeable>
IoFuture.Notifier<T,Void>
closingNotifier()
          Get a notifier that closes the result.
static
<T extends StreamChannel>
Closeable
createConnection(ChannelSource<T> channelSource, IoHandler<? super T> handler, Executor reconnectExecutor)
          Create a persistent connection using a channel source.
static Executor delayedExecutor(ScheduledExecutorService scheduledExecutorService, long delay, TimeUnit unit)
          Create a delayed executor.
static Executor directExecutor()
          Get the direct executor.
static
<T> Future<T>
getFuture(IoFuture<T> ioFuture)
          Get a java.util.concurrent-style Future instance wrapper for an IoFuture instance.
static Closeable nullCloseable()
          Get the null closeable.
static Executor nullExecutor()
          Get the null executor.
static
<T extends Channel>
IoHandler<T>
nullHandler()
          Get the null handler.
static
<T extends Channel>
IoHandlerFactory<T>
nullHandlerFactory()
          Get the null handler factory.
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(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.
static
<T extends Channel>
IoHandlerFactory<T>
singletonHandlerFactory(IoHandler<T> handler)
          Get a singleton handler factory that returns the given handler one time only.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

createConnection

public static <T extends StreamChannel> Closeable createConnection(ChannelSource<T> channelSource,
                                                                   IoHandler<? super T> handler,
                                                                   Executor reconnectExecutor)
Create a persistent connection using a channel source. The provided handler will handle the connection. The reconnectExecutor will be used to execute a reconnect task in the event that the connection fails or is lost or terminated. If you wish to impose a time delay on reconnect, use the delayedExecutor() method to create a delayed executor. If you do not want to auto-reconnect use the nullExecutor() method to create a null executor. If you want auto-reconnect to take place immediately, use the directExecutor() method to create a direct executor.

Type Parameters:
T - the channel type
Parameters:
channelSource - the client to connect on
handler - the handler for the connection
reconnectExecutor - the executor that should execute the reconnect task
Returns:
a handle which can be used to terminate the connection

delayedExecutor

public static Executor delayedExecutor(ScheduledExecutorService scheduledExecutorService,
                                       long delay,
                                       TimeUnit unit)
Create a delayed executor. This is an executor that executes tasks after a given time delay with the help of the provided ScheduledExecutorService. To get an executor for this method, use one of the methods on the Executors class.

Parameters:
scheduledExecutorService - the executor service to use to schedule the task
delay - the time delay before reconnect
unit - the unit of time to use
Returns:
an executor that delays execution

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

nullHandler

public static <T extends Channel> IoHandler<T> nullHandler()
Get the null handler. This is a handler whose handler methods all return without taking any action.

Type Parameters:
T - the channel type
Returns:
the null handler

nullHandlerFactory

public static <T extends Channel> IoHandlerFactory<T> nullHandlerFactory()
Get the null handler factory. This is a handler factory that returns the null handler.

Type Parameters:
T - the channel type
Returns:
the null handler factory

singletonHandlerFactory

public static <T extends Channel> IoHandlerFactory<T> singletonHandlerFactory(IoHandler<T> handler)
Get a singleton handler factory that returns the given handler one time only.

Parameters:
handler - the handler to return
Returns:
a singleton handler factory

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

attachmentClosingNotifier

public static <T> IoFuture.Notifier<T,Closeable> attachmentClosingNotifier()
Get a notifier that closes the attachment.

Type Parameters:
T - the future type (not used)
Returns:
a notifier which will close its attachment

closingNotifier

public static <T extends Closeable> IoFuture.Notifier<T,Void> closingNotifier()
Get a notifier that closes the result.

Type Parameters:
T - the future type, which must be closeable
Returns:
a notifier which will close the result of the operation (if successful)

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)

awaitAllInterruptibly

public static void awaitAllInterruptibly(IoFuture<?>... futures)
                                  throws InterruptedException
Throws:
InterruptedException

XNIO version 1.2.0.GA

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