XNIO version 1.2.0.GA

org.jboss.xnio.channels
Class Channels

java.lang.Object
  extended by org.jboss.xnio.channels.Channels

public final class Channels
extends Object

A utility class containing static methods to convert from one channel type to another.


Method Summary
static ChannelSource<StreamChannel> convertAllocatedMessageToStream(ChannelSource<? extends AllocatedMessageChannel> messageChannelSource)
          Create a channel source for a stream channel.
static IoHandlerFactory<AllocatedMessageChannel> convertAllocatedMessageToStream(IoHandlerFactory<? super StreamChannel> handlerFactory)
          Create a channel source for a stream channel.
static ChannelSource<AllocatedMessageChannel> convertStreamToAllocatedMessage(ChannelSource<? extends StreamChannel> streamChannelSource, int maxInboundMessageSize, int maxOutboundMessageSize)
          Create a channel source for an allocated message channel.
static IoHandlerFactory<StreamChannel> convertStreamToAllocatedMessage(IoHandlerFactory<? super AllocatedMessageChannel> handlerFactory, int maxInboundMessageSize, int maxOutboundMessageSize)
          Create a handler factory for an allocated message channel.
static
<T extends SuspendableChannel>
IoHandler<T>
createMergedHandler(IoHandler<? super T> readSide, IoHandler<? super T> writeSide)
          Create a handler that is a merged view of two separate handlers, one for read operations and one for write operations.
static
<T extends SuspendableChannel>
IoHandlerFactory<T>
createMergedHandlerFactory(IoHandlerFactory<? super T> readFactory, IoHandlerFactory<? super T> writeFactory)
          Create a handler factory that is a merged view of two separate handler factories, one for read operations and one for write operations.
static
<C extends ReadableByteChannel & SuspendableReadChannel>
int
readBlocking(C channel, ByteBuffer buffer)
          Simple utility method to execute a blocking read on a readable byte channel.
static
<C extends ScatteringByteChannel & SuspendableReadChannel>
long
readBlocking(C channel, ByteBuffer[] buffers, int offs, int len)
          Simple utility method to execute a blocking read on a scattering byte channel.
static
<C extends ScatteringByteChannel & SuspendableReadChannel>
long
readBlocking(C channel, ByteBuffer[] buffers, int offs, int len, long time, TimeUnit unit)
          Simple utility method to execute a blocking read on a scattering byte channel with a timeout.
static
<C extends ReadableByteChannel & SuspendableReadChannel>
int
readBlocking(C channel, ByteBuffer buffer, long time, TimeUnit unit)
          Simple utility method to execute a blocking read on a readable byte channel with a timeout.
static
<C extends ReadableMessageChannel & SuspendableReadChannel>
int
receiveBlocking(C channel, ByteBuffer buffer)
          Simple utility method to execute a blocking receive on a readable message channel.
static
<C extends ReadableMessageChannel & SuspendableReadChannel>
long
receiveBlocking(C channel, ByteBuffer[] buffers, int offs, int len)
          Simple utility method to execute a blocking receive on a readable message channel.
static
<C extends ReadableMessageChannel & SuspendableReadChannel>
long
receiveBlocking(C channel, ByteBuffer[] buffers, int offs, int len, long time, TimeUnit unit)
          Simple utility method to execute a blocking receive on a readable message channel with a timeout.
static
<C extends ReadableMessageChannel & SuspendableReadChannel>
int
receiveBlocking(C channel, ByteBuffer buffer, long time, TimeUnit unit)
          Simple utility method to execute a blocking receive on a readable message channel with a timeout.
static
<C extends WritableMessageChannel & SuspendableWriteChannel>
void
sendBlocking(C channel, ByteBuffer buffer)
          Simple utility method to execute a blocking send on a message channel.
static
<C extends WritableMessageChannel & SuspendableWriteChannel>
void
sendBlocking(C channel, ByteBuffer[] buffers, int offs, int len)
          Simple utility method to execute a blocking gathering send on a message channel.
static
<C extends WritableMessageChannel & SuspendableWriteChannel>
boolean
sendBlocking(C channel, ByteBuffer[] buffers, int offs, int len, long time, TimeUnit unit)
          Simple utility method to execute a blocking gathering send on a message channel with a timeout.
static
<C extends WritableMessageChannel & SuspendableWriteChannel>
boolean
sendBlocking(C channel, ByteBuffer buffer, long time, TimeUnit unit)
          Simple utility method to execute a blocking send on a message channel with a timeout.
static
<C extends WritableByteChannel & SuspendableWriteChannel>
int
writeBlocking(C channel, ByteBuffer buffer)
          Simple utility method to execute a blocking write on a byte channel.
static
<C extends GatheringByteChannel & SuspendableWriteChannel>
long
writeBlocking(C channel, ByteBuffer[] buffers, int offs, int len)
          Simple utility method to execute a blocking write on a gathering byte channel.
static
<C extends GatheringByteChannel & SuspendableWriteChannel>
long
writeBlocking(C channel, ByteBuffer[] buffers, int offs, int len, long time, TimeUnit unit)
          Simple utility method to execute a blocking write on a gathering byte channel with a timeout.
static
<C extends WritableByteChannel & SuspendableWriteChannel>
int
writeBlocking(C channel, ByteBuffer buffer, long time, TimeUnit unit)
          Simple utility method to execute a blocking write on a byte channel with a timeout.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

convertStreamToAllocatedMessage

public static ChannelSource<AllocatedMessageChannel> convertStreamToAllocatedMessage(ChannelSource<? extends StreamChannel> streamChannelSource,
                                                                                     int maxInboundMessageSize,
                                                                                     int maxOutboundMessageSize)
Create a channel source for an allocated message channel. The resulting channel uses a simple protocol to send and receive messages. First, a four-byte length field is sent in network order; then a message of that length follows. If an incoming message is too large, it is ignored. If an outgoing message is too large, an exception is thrown for that send.

Parameters:
streamChannelSource - the stream channel source to encapsulate
maxInboundMessageSize - the maximum incoming message size
maxOutboundMessageSize - the maximum outgoing message size
Returns:
an allocated message channel source

convertStreamToAllocatedMessage

public static IoHandlerFactory<StreamChannel> convertStreamToAllocatedMessage(IoHandlerFactory<? super AllocatedMessageChannel> handlerFactory,
                                                                              int maxInboundMessageSize,
                                                                              int maxOutboundMessageSize)
Create a handler factory for an allocated message channel. The resulting channel uses a simple protocol to send and receive messages. First, a four-byte length field is sent in network order; then a message of that length follows. If an incoming message is too large, it is ignored. If an outgoing message is too large, an exception is thrown for that send.

Parameters:
handlerFactory - the user allocated message channel handler factory
maxInboundMessageSize - the maximum incoming message size
maxOutboundMessageSize - the maximum outgoing message size
Returns:
a stream channel handler factory that implements the protocol

convertAllocatedMessageToStream

public static ChannelSource<StreamChannel> convertAllocatedMessageToStream(ChannelSource<? extends AllocatedMessageChannel> messageChannelSource)
Create a channel source for a stream channel. The resulting channel simply converts the messages into stream bytes. The resulting channel is strictly edge-triggered: if a read handler does not fully consume all the readable bytes on the channel before calling StreamChannel.resumeReads() or StreamChannel.awaitReadable(), then the channel may block even though there is more data to be read.

Parameters:
messageChannelSource - the allocated message channel source
Returns:
a stream channel source

convertAllocatedMessageToStream

public static IoHandlerFactory<AllocatedMessageChannel> convertAllocatedMessageToStream(IoHandlerFactory<? super StreamChannel> handlerFactory)
Create a channel source for a stream channel. The resulting channel simply converts the messages into stream bytes. The resulting channel is strictly edge-triggered: if a read handler does not fully consume all the readable bytes on the channel before calling StreamChannel.resumeReads() or StreamChannel.awaitReadable(), then the channel may block even though there is more data to be read.

Parameters:
handlerFactory - the user stream channel handler factory
Returns:
an allocated message channel handler factory that implements the protocol

createMergedHandler

public static <T extends SuspendableChannel> IoHandler<T> createMergedHandler(IoHandler<? super T> readSide,
                                                                              IoHandler<? super T> writeSide)
Create a handler that is a merged view of two separate handlers, one for read operations and one for write operations. The handleOpened() and handleClosed() methods are called on each of the two sub-handlers.

Type Parameters:
T - the resultant channel type
Parameters:
readSide - the handler to handle read operations
writeSide - the handler to handle write operations
Returns:
a combined handler

createMergedHandlerFactory

public static <T extends SuspendableChannel> IoHandlerFactory<T> createMergedHandlerFactory(IoHandlerFactory<? super T> readFactory,
                                                                                            IoHandlerFactory<? super T> writeFactory)
Create a handler factory that is a merged view of two separate handler factories, one for read operations and one for write operations.

Type Parameters:
T - the resultant channel type
Parameters:
readFactory - the handler factory to create handlers that handle read operations
writeFactory - the handler factory to create handlers that handle write operations
Returns:
a combined handler factory

writeBlocking

public static <C extends WritableByteChannel & SuspendableWriteChannel> int writeBlocking(C channel,
                                                                                         ByteBuffer buffer)
                         throws IOException
Simple utility method to execute a blocking write on a byte channel. The method blocks until the channel is writable, and then the message is written.

Type Parameters:
C - the channel type
Parameters:
channel - the channel to write on
buffer - the data to write
Returns:
the number of bytes written
Throws:
IOException - if an I/O exception occurs
Since:
1.2

writeBlocking

public static <C extends WritableByteChannel & SuspendableWriteChannel> int writeBlocking(C channel,
                                                                                         ByteBuffer buffer,
                                                                                         long time,
                                                                                         TimeUnit unit)
                         throws IOException
Simple utility method to execute a blocking write on a byte channel with a timeout. The method blocks until the channel is writable, and then the message is written.

Type Parameters:
C - the channel type
Parameters:
channel - the channel to write on
buffer - the data to write
time - the amount of time to wait
unit - the unit of time to wait
Returns:
the number of bytes written
Throws:
IOException - if an I/O exception occurs
Since:
1.2

writeBlocking

public static <C extends GatheringByteChannel & SuspendableWriteChannel> long writeBlocking(C channel,
                                                                                           ByteBuffer[] buffers,
                                                                                           int offs,
                                                                                           int len)
                          throws IOException
Simple utility method to execute a blocking write on a gathering byte channel. The method blocks until the channel is writable, and then the message is written.

Type Parameters:
C - the channel type
Parameters:
channel - the channel to write on
buffers - the data to write
offs - the index of the first buffer to write
len - the number of buffers to write
Returns:
the number of bytes written
Throws:
IOException - if an I/O exception occurs
Since:
1.2

writeBlocking

public static <C extends GatheringByteChannel & SuspendableWriteChannel> long writeBlocking(C channel,
                                                                                           ByteBuffer[] buffers,
                                                                                           int offs,
                                                                                           int len,
                                                                                           long time,
                                                                                           TimeUnit unit)
                          throws IOException
Simple utility method to execute a blocking write on a gathering byte channel with a timeout. The method blocks until the channel is writable, and then the message is written.

Type Parameters:
C - the channel type
Parameters:
channel - the channel to write on
buffers - the data to write
offs - the index of the first buffer to write
len - the number of buffers to write
time - the amount of time to wait
unit - the unit of time to wait
Returns:
the number of bytes written
Throws:
IOException - if an I/O exception occurs
Since:
1.2

sendBlocking

public static <C extends WritableMessageChannel & SuspendableWriteChannel> void sendBlocking(C channel,
                                                                                            ByteBuffer buffer)
                         throws IOException
Simple utility method to execute a blocking send on a message channel. The method blocks until the channel is writable, and then the message is written.

Type Parameters:
C - the channel type
Parameters:
channel - the channel to write on
buffer - the data to write
Throws:
IOException - if an I/O exception occurs
Since:
1.2

sendBlocking

public static <C extends WritableMessageChannel & SuspendableWriteChannel> boolean sendBlocking(C channel,
                                                                                               ByteBuffer buffer,
                                                                                               long time,
                                                                                               TimeUnit unit)
                            throws IOException
Simple utility method to execute a blocking send on a message channel with a timeout. The method blocks until the channel is writable, and then the message is written.

Type Parameters:
C - the channel type
Parameters:
channel - the channel to write on
buffer - the data to write
time - the amount of time to wait
unit - the unit of time to wait
Returns:
true if the message was written before the timeout
Throws:
IOException - if an I/O exception occurs
Since:
1.2

sendBlocking

public static <C extends WritableMessageChannel & SuspendableWriteChannel> void sendBlocking(C channel,
                                                                                            ByteBuffer[] buffers,
                                                                                            int offs,
                                                                                            int len)
                         throws IOException
Simple utility method to execute a blocking gathering send on a message channel. The method blocks until the channel is writable, and then the message is written.

Type Parameters:
C - the channel type
Parameters:
channel - the channel to write on
buffers - the data to write
offs - the index of the first buffer to write
len - the number of buffers to write
Throws:
IOException - if an I/O exception occurs
Since:
1.2

sendBlocking

public static <C extends WritableMessageChannel & SuspendableWriteChannel> boolean sendBlocking(C channel,
                                                                                               ByteBuffer[] buffers,
                                                                                               int offs,
                                                                                               int len,
                                                                                               long time,
                                                                                               TimeUnit unit)
                            throws IOException
Simple utility method to execute a blocking gathering send on a message channel with a timeout. The method blocks until the channel is writable, and then the message is written.

Type Parameters:
C - the channel type
Parameters:
channel - the channel to write on
buffers - the data to write
offs - the index of the first buffer to write
len - the number of buffers to write
time - the amount of time to wait
unit - the unit of time to wait
Returns:
true if the message was written before the timeout
Throws:
IOException - if an I/O exception occurs
Since:
1.2

readBlocking

public static <C extends ReadableByteChannel & SuspendableReadChannel> int readBlocking(C channel,
                                                                                       ByteBuffer buffer)
                        throws IOException
Simple utility method to execute a blocking read on a readable byte channel. This method blocks until the channel is readable, and then the message is read.

Type Parameters:
C - the channel type
Parameters:
channel - the channel to read from
buffer - the buffer into which bytes are to be transferred
Returns:
the number of bytes read
Throws:
IOException - if an I/O exception occurs
Since:
1.2

readBlocking

public static <C extends ReadableByteChannel & SuspendableReadChannel> int readBlocking(C channel,
                                                                                       ByteBuffer buffer,
                                                                                       long time,
                                                                                       TimeUnit unit)
                        throws IOException
Simple utility method to execute a blocking read on a readable byte channel with a timeout. This method blocks until the channel is readable, and then the message is read.

Type Parameters:
C - the channel type
Parameters:
channel - the channel to read from
buffer - the buffer into which bytes are to be transferred
time - the amount of time to wait
unit - the unit of time to wait
Returns:
the number of bytes read
Throws:
IOException - if an I/O exception occurs
Since:
1.2

readBlocking

public static <C extends ScatteringByteChannel & SuspendableReadChannel> long readBlocking(C channel,
                                                                                          ByteBuffer[] buffers,
                                                                                          int offs,
                                                                                          int len)
                         throws IOException
Simple utility method to execute a blocking read on a scattering byte channel. This method blocks until the channel is readable, and then the message is read.

Type Parameters:
C - the channel type
Parameters:
channel - the channel to read from
buffers - the buffers into which bytes are to be transferred
offs - the first buffer to use
len - the number of buffers to use
Returns:
the number of bytes read
Throws:
IOException - if an I/O exception occurs
Since:
1.2

readBlocking

public static <C extends ScatteringByteChannel & SuspendableReadChannel> long readBlocking(C channel,
                                                                                          ByteBuffer[] buffers,
                                                                                          int offs,
                                                                                          int len,
                                                                                          long time,
                                                                                          TimeUnit unit)
                         throws IOException
Simple utility method to execute a blocking read on a scattering byte channel with a timeout. This method blocks until the channel is readable, and then the message is read.

Type Parameters:
C - the channel type
Parameters:
channel - the channel to read from
buffers - the buffers into which bytes are to be transferred
offs - the first buffer to use
len - the number of buffers to use
time - the amount of time to wait
unit - the unit of time to wait
Returns:
the number of bytes read
Throws:
IOException - if an I/O exception occurs
Since:
1.2

receiveBlocking

public static <C extends ReadableMessageChannel & SuspendableReadChannel> int receiveBlocking(C channel,
                                                                                             ByteBuffer buffer)
                           throws IOException
Simple utility method to execute a blocking receive on a readable message channel. This method blocks until the channel is readable, and then the message is received.

Type Parameters:
C - the channel type
Parameters:
channel - the channel to read from
buffer - the buffer into which bytes are to be transferred
Returns:
the number of bytes read
Throws:
IOException - if an I/O exception occurs
Since:
1.2

receiveBlocking

public static <C extends ReadableMessageChannel & SuspendableReadChannel> int receiveBlocking(C channel,
                                                                                             ByteBuffer buffer,
                                                                                             long time,
                                                                                             TimeUnit unit)
                           throws IOException
Simple utility method to execute a blocking receive on a readable message channel with a timeout. This method blocks until the channel is readable, and then the message is received.

Type Parameters:
C - the channel type
Parameters:
channel - the channel to read from
buffer - the buffer into which bytes are to be transferred
time - the amount of time to wait
unit - the unit of time to wait
Returns:
the number of bytes read
Throws:
IOException - if an I/O exception occurs
Since:
1.2

receiveBlocking

public static <C extends ReadableMessageChannel & SuspendableReadChannel> long receiveBlocking(C channel,
                                                                                              ByteBuffer[] buffers,
                                                                                              int offs,
                                                                                              int len)
                            throws IOException
Simple utility method to execute a blocking receive on a readable message channel. This method blocks until the channel is readable, and then the message is received.

Type Parameters:
C - the channel type
Parameters:
channel - the channel to read from
buffers - the buffers into which bytes are to be transferred
offs - the first buffer to use
len - the number of buffers to use
Returns:
the number of bytes read
Throws:
IOException - if an I/O exception occurs
Since:
1.2

receiveBlocking

public static <C extends ReadableMessageChannel & SuspendableReadChannel> long receiveBlocking(C channel,
                                                                                              ByteBuffer[] buffers,
                                                                                              int offs,
                                                                                              int len,
                                                                                              long time,
                                                                                              TimeUnit unit)
                            throws IOException
Simple utility method to execute a blocking receive on a readable message channel with a timeout. This method blocks until the channel is readable, and then the message is received.

Type Parameters:
C - the channel type
Parameters:
channel - the channel to read from
buffers - the buffers into which bytes are to be transferred
offs - the first buffer to use
len - the number of buffers to use
time - the amount of time to wait
unit - the unit of time to wait
Returns:
the number of bytes read
Throws:
IOException - if an I/O exception occurs
Since:
1.2

XNIO version 1.2.0.GA

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