XNIO version 1.0.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 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 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.
 
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

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

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

XNIO version 1.0.0.GA

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