XNIO API 2.0.0.GA

org.jboss.xnio.channels
Class Channels

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

public final class Channels
extends java.lang.Object

A utility class containing static methods to support channel usage.


Method Summary
static MessageHandler.Setter createMessageReader(StreamSourceChannel channel, OptionMap optionMap)
          Create and install a stream channel listener which unwraps messages and passes them to a handler.
static WritableMessageChannel createMessageWriter(StreamSinkChannel channel, OptionMap optionMap)
          Create a writable message channel which wraps a stream sink channel using a simple length-body protocol.
static SslTcpChannel createSslTcpChannel(javax.net.ssl.SSLContext sslContext, TcpChannel tcpChannel, java.util.concurrent.Executor executor, OptionMap optionMap)
          Create a SSL/TLS-enabled channel over a TCP channel.
static ChannelListener<TcpChannel> createSslTcpChannelListener(javax.net.ssl.SSLContext sslContext, ChannelListener<? super SslTcpChannel> sslChannelListener, java.util.concurrent.Executor executor, OptionMap optionMap)
          Create a channel lister which wraps the incoming connection with an SSL connection.
static void flushBlocking(SuspendableWriteChannel channel)
          Simple utility method to execute a blocking flush on a writable channel.
static
<C extends java.nio.channels.ReadableByteChannel & SuspendableReadChannel>
int
readBlocking(C channel, java.nio.ByteBuffer buffer)
          Simple utility method to execute a blocking read on a readable byte channel.
static
<C extends java.nio.channels.ScatteringByteChannel & SuspendableReadChannel>
long
readBlocking(C channel, java.nio.ByteBuffer[] buffers, int offs, int len)
          Simple utility method to execute a blocking read on a scattering byte channel.
static
<C extends java.nio.channels.ScatteringByteChannel & SuspendableReadChannel>
long
readBlocking(C channel, java.nio.ByteBuffer[] buffers, int offs, int len, long time, java.util.concurrent.TimeUnit unit)
          Simple utility method to execute a blocking read on a scattering byte channel with a timeout.
static
<C extends java.nio.channels.ReadableByteChannel & SuspendableReadChannel>
int
readBlocking(C channel, java.nio.ByteBuffer buffer, long time, java.util.concurrent.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, java.nio.ByteBuffer buffer)
          Simple utility method to execute a blocking receive on a readable message channel.
static
<C extends ReadableMessageChannel & SuspendableReadChannel>
long
receiveBlocking(C channel, java.nio.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, java.nio.ByteBuffer[] buffers, int offs, int len, long time, java.util.concurrent.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, java.nio.ByteBuffer buffer, long time, java.util.concurrent.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, java.nio.ByteBuffer buffer)
          Simple utility method to execute a blocking send on a message channel.
static
<C extends WritableMessageChannel & SuspendableWriteChannel>
void
sendBlocking(C channel, java.nio.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, java.nio.ByteBuffer[] buffers, int offs, int len, long time, java.util.concurrent.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, java.nio.ByteBuffer buffer, long time, java.util.concurrent.TimeUnit unit)
          Simple utility method to execute a blocking send on a message channel with a timeout.
static void shutdownWritesBlocking(SuspendableWriteChannel channel)
          Simple utility method to execute a blocking write shutdown on a writable channel.
static
<C extends java.nio.channels.WritableByteChannel & SuspendableWriteChannel>
int
writeBlocking(C channel, java.nio.ByteBuffer buffer)
          Simple utility method to execute a blocking write on a byte channel.
static
<C extends java.nio.channels.GatheringByteChannel & SuspendableWriteChannel>
long
writeBlocking(C channel, java.nio.ByteBuffer[] buffers, int offs, int len)
          Simple utility method to execute a blocking write on a gathering byte channel.
static
<C extends java.nio.channels.GatheringByteChannel & SuspendableWriteChannel>
long
writeBlocking(C channel, java.nio.ByteBuffer[] buffers, int offs, int len, long time, java.util.concurrent.TimeUnit unit)
          Simple utility method to execute a blocking write on a gathering byte channel with a timeout.
static
<C extends java.nio.channels.WritableByteChannel & SuspendableWriteChannel>
int
writeBlocking(C channel, java.nio.ByteBuffer buffer, long time, java.util.concurrent.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

createMessageReader

public static MessageHandler.Setter createMessageReader(StreamSourceChannel channel,
                                                        OptionMap optionMap)
Create and install a stream channel listener which unwraps messages and passes them to a handler. The listener is installed into the given channel. The user message handler may be set (and changed at any time) via the returned setter. The corresponding write side can be created via createMessageWriter(StreamSinkChannel, org.jboss.xnio.OptionMap).

Parameters:
channel - the stream source channel
optionMap - the initial options
Returns:
the setter for the new message handler
Since:
2.0
See Also:
Options.MAX_INBOUND_MESSAGE_SIZE

createMessageWriter

public static WritableMessageChannel createMessageWriter(StreamSinkChannel channel,
                                                         OptionMap optionMap)
Create a writable message channel which wraps a stream sink channel using a simple length-body protocol. The corresponding read side can be created via createMessageReader(StreamSourceChannel, org.jboss.xnio.OptionMap).

Parameters:
channel - the stream sink channel
optionMap - the initial options
Returns:
the message channel
Since:
2.0
See Also:
Options.MAX_OUTBOUND_MESSAGE_SIZE

createSslTcpChannel

public static SslTcpChannel createSslTcpChannel(javax.net.ssl.SSLContext sslContext,
                                                TcpChannel tcpChannel,
                                                java.util.concurrent.Executor executor,
                                                OptionMap optionMap)
                                         throws java.io.IOException
Create a SSL/TLS-enabled channel over a TCP channel. Uses the given SSLContext, and uses the option map to configure the parameters of the connection (including whether this side is the client or the server). By default, the channel will run in client mode.

Parameters:
sslContext - the SSL context to use
tcpChannel - the TCP channel over which the connection is encapsulated
executor - the executor to use for executing asynchronous tasks
optionMap - the configuration options for the channel
Returns:
the new SSL TCP channel
Throws:
java.io.IOException
Since:
2.0
See Also:
Options.SSL_CLIENT_AUTH_MODE, Options.SSL_USE_CLIENT_MODE, Options.SSL_ENABLE_SESSION_CREATION, Options.SSL_ENABLED_CIPHER_SUITES, Options.SSL_ENABLED_PROTOCOLS

createSslTcpChannelListener

public static ChannelListener<TcpChannel> createSslTcpChannelListener(javax.net.ssl.SSLContext sslContext,
                                                                      ChannelListener<? super SslTcpChannel> sslChannelListener,
                                                                      java.util.concurrent.Executor executor,
                                                                      OptionMap optionMap)
Create a channel lister which wraps the incoming connection with an SSL connection. By default, the channel will run in server mode.

Parameters:
sslContext - the SSL context to use
sslChannelListener - the SSL TCP channel listener which should be executed with the SSL connection
executor - the executor to use for executing asynchronous tasks
optionMap - the configuration options for the channel
Returns:
the new SSL-enabled TCP channel listener
Since:
2.0
See Also:
Options.SSL_CLIENT_AUTH_MODE, Options.SSL_USE_CLIENT_MODE, Options.SSL_ENABLE_SESSION_CREATION, Options.SSL_ENABLED_CIPHER_SUITES, Options.SSL_ENABLED_PROTOCOLS

flushBlocking

public static void flushBlocking(SuspendableWriteChannel channel)
                          throws java.io.IOException
Simple utility method to execute a blocking flush on a writable channel. The method blocks until there are no remaining bytes in the send queue.

Parameters:
channel - the writable channel
Throws:
java.io.IOException - if an I/O exception occurs
Since:
2.0

shutdownWritesBlocking

public static void shutdownWritesBlocking(SuspendableWriteChannel channel)
                                   throws java.io.IOException
Simple utility method to execute a blocking write shutdown on a writable channel. The method blocks until the channel's output side is fully shut down.

Parameters:
channel - the writable channel
Throws:
java.io.IOException - if an I/O exception occurs
Since:
2.0

writeBlocking

public static <C extends java.nio.channels.WritableByteChannel & SuspendableWriteChannel> int writeBlocking(C channel,
                                                                                                           java.nio.ByteBuffer buffer)
                         throws java.io.IOException
Simple utility method to execute a blocking write on a byte channel. The method blocks until the bytes in the buffer have been fully written. To ensure that the data is sent, the flushBlocking(SuspendableWriteChannel) method should be called after all writes are complete.

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:
java.io.IOException - if an I/O exception occurs
Since:
1.2

writeBlocking

public static <C extends java.nio.channels.WritableByteChannel & SuspendableWriteChannel> int writeBlocking(C channel,
                                                                                                           java.nio.ByteBuffer buffer,
                                                                                                           long time,
                                                                                                           java.util.concurrent.TimeUnit unit)
                         throws java.io.IOException
Simple utility method to execute a blocking write on a byte channel with a timeout. The method blocks until either the bytes in the buffer have been fully written, or the timeout expires, whichever comes first.

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:
java.io.IOException - if an I/O exception occurs
Since:
1.2

writeBlocking

public static <C extends java.nio.channels.GatheringByteChannel & SuspendableWriteChannel> long writeBlocking(C channel,
                                                                                                             java.nio.ByteBuffer[] buffers,
                                                                                                             int offs,
                                                                                                             int len)
                          throws java.io.IOException
Simple utility method to execute a blocking write on a gathering byte channel. The method blocks until the bytes in the buffer have been fully 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:
java.io.IOException - if an I/O exception occurs
Since:
1.2

writeBlocking

public static <C extends java.nio.channels.GatheringByteChannel & SuspendableWriteChannel> long writeBlocking(C channel,
                                                                                                             java.nio.ByteBuffer[] buffers,
                                                                                                             int offs,
                                                                                                             int len,
                                                                                                             long time,
                                                                                                             java.util.concurrent.TimeUnit unit)
                          throws java.io.IOException
Simple utility method to execute a blocking write on a gathering byte channel with a timeout. The method blocks until all the bytes are written, or until the timeout occurs.

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:
java.io.IOException - if an I/O exception occurs
Since:
1.2

sendBlocking

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

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

sendBlocking

public static <C extends WritableMessageChannel & SuspendableWriteChannel> boolean sendBlocking(C channel,
                                                                                               java.nio.ByteBuffer buffer,
                                                                                               long time,
                                                                                               java.util.concurrent.TimeUnit unit)
                            throws java.io.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:
the write result
Throws:
java.io.IOException - if an I/O exception occurs
Since:
1.2

sendBlocking

public static <C extends WritableMessageChannel & SuspendableWriteChannel> void sendBlocking(C channel,
                                                                                            java.nio.ByteBuffer[] buffers,
                                                                                            int offs,
                                                                                            int len)
                         throws java.io.IOException
Simple utility method to execute a blocking gathering send on a message channel. The method blocks until 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:
java.io.IOException - if an I/O exception occurs
Since:
1.2

sendBlocking

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

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:
java.io.IOException - if an I/O exception occurs
Since:
1.2

readBlocking

public static <C extends java.nio.channels.ReadableByteChannel & SuspendableReadChannel> int readBlocking(C channel,
                                                                                                         java.nio.ByteBuffer buffer)
                        throws java.io.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:
java.io.IOException - if an I/O exception occurs
Since:
1.2

readBlocking

public static <C extends java.nio.channels.ReadableByteChannel & SuspendableReadChannel> int readBlocking(C channel,
                                                                                                         java.nio.ByteBuffer buffer,
                                                                                                         long time,
                                                                                                         java.util.concurrent.TimeUnit unit)
                        throws java.io.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:
java.io.IOException - if an I/O exception occurs
Since:
1.2

readBlocking

public static <C extends java.nio.channels.ScatteringByteChannel & SuspendableReadChannel> long readBlocking(C channel,
                                                                                                            java.nio.ByteBuffer[] buffers,
                                                                                                            int offs,
                                                                                                            int len)
                         throws java.io.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:
java.io.IOException - if an I/O exception occurs
Since:
1.2

readBlocking

public static <C extends java.nio.channels.ScatteringByteChannel & SuspendableReadChannel> long readBlocking(C channel,
                                                                                                            java.nio.ByteBuffer[] buffers,
                                                                                                            int offs,
                                                                                                            int len,
                                                                                                            long time,
                                                                                                            java.util.concurrent.TimeUnit unit)
                         throws java.io.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:
java.io.IOException - if an I/O exception occurs
Since:
1.2

receiveBlocking

public static <C extends ReadableMessageChannel & SuspendableReadChannel> int receiveBlocking(C channel,
                                                                                             java.nio.ByteBuffer buffer)
                           throws java.io.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:
java.io.IOException - if an I/O exception occurs
Since:
1.2

receiveBlocking

public static <C extends ReadableMessageChannel & SuspendableReadChannel> int receiveBlocking(C channel,
                                                                                             java.nio.ByteBuffer buffer,
                                                                                             long time,
                                                                                             java.util.concurrent.TimeUnit unit)
                           throws java.io.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:
java.io.IOException - if an I/O exception occurs
Since:
1.2

receiveBlocking

public static <C extends ReadableMessageChannel & SuspendableReadChannel> long receiveBlocking(C channel,
                                                                                              java.nio.ByteBuffer[] buffers,
                                                                                              int offs,
                                                                                              int len)
                            throws java.io.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:
java.io.IOException - if an I/O exception occurs
Since:
1.2

receiveBlocking

public static <C extends ReadableMessageChannel & SuspendableReadChannel> long receiveBlocking(C channel,
                                                                                              java.nio.ByteBuffer[] buffers,
                                                                                              int offs,
                                                                                              int len,
                                                                                              long time,
                                                                                              java.util.concurrent.TimeUnit unit)
                            throws java.io.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:
java.io.IOException - if an I/O exception occurs
Since:
1.2

XNIO API 2.0.0.GA

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