Sea 0.2.0

gov.lbl.dsd.sea.nio
Class NioUtil

java.lang.Object
  extended bygov.lbl.dsd.sea.nio.NioUtil

public class NioUtil
extends Object

Various utilities related to the java.nio package.

Version:
$Revision: 1.16 $, $Date: 2004/05/29 21:07:26 $

Method Summary
static void addInterestBits(SelectionKey key, int opBits)
          Adds the given operation bits to the interest set of the given key.
static ByteBuffer copy(ByteBuffer src)
          Creates a new bytebuffer that is a deep copy of the given buffer between index buf.position() and buf.limit(); leaves the src buffer unmodified.
static SocketChannel createClientChannel(InetSocketAddress address)
          Creates and returns a non-blocking TCP client channel bound to the given network address.
static SocketChannel createClientChannel(String hostName, int port)
          Creates and returns a non-blocking TCP server channel bound to the given network host and port.
static DatagramChannel createDatagramChannel(InetSocketAddress address)
          Creates and returns a non-blocking UDP datagram channel bound to the given network address.
static DatagramChannel createDatagramChannel(int port)
          Creates and returns a non-blocking UDP datagram channel bound to the given network port.
static ServerSocketChannel createServerChannel(InetSocketAddress address)
          Creates and returns a non-blocking TCP server channel bound to the given network address.
static ServerSocketChannel createServerChannel(int port)
          Creates and returns a non-blocking TCP server channel bound to the given network port.
static int getNumberOfReadyOps(SelectionKey key)
          Returns the number of ready operations of the given selection key.
static List getRegisteredChannels(Selector selector)
          Returns all selectable channels registered with the given selector, excluding channels of invalid keys.
static boolean hasMark(Buffer buffer)
          Returns whether or not a given buffer has the mark defined.
static int readMany(ReadableByteChannel channel, ByteBuffer buffer)
          Efficiently reads (without ever blocking) as many bytes as possible from the given non-blocking channel into the given buffer.
static void removeInterestBits(SelectionKey key, int opBits)
          Removes the given operation bits from the interest set of the given key.
static byte[] toAsciiByteArray(String str)
          Returns a byte array holding the ASCII bytes of the given string.
static ByteBuffer toAsciiByteBuffer(String str)
          Returns a bytebuffer holding the ASCII bytes of the given string.
static String toAsciiString(byte[] bytes)
          Returns a ASCII string representation of the given bytes.
static String toAsciiString(ByteBuffer buffer)
          Returns a ASCII string representation of the bytes in the given buffer.
static byte[] toByteArray(ByteBuffer src)
          Creates and returns a byte array filled with the given buffer's contents between index buf.position() and buf.limit(); leaves the src buffer unmodified.
static String toString(int selectionKeyOps)
          Returns a detailed string representation of the given selection key readyOps or interestOps for debugging purposes.
static String toString(SelectionKey key)
          Returns a detailed string representation of the given selection key for debugging purposes.
static String toString(Selector selector)
          Returns a detailed string representation of the given selector for debugging purposes.
static String toString(Set keySet)
          Returns a detailed string representation for debugging purposes.
static int writeMany(WritableByteChannel channel, ByteBuffer buffer)
          Efficiently writes (without ever blocking) as many bytes as possible from the given buffer to the given non-blocking channel.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

createServerChannel

public static ServerSocketChannel createServerChannel(int port)
                                               throws IOException
Creates and returns a non-blocking TCP server channel bound to the given network port.

Parameters:
port - the port to bind to.
Returns:
the channel
Throws:
IOException

createServerChannel

public static ServerSocketChannel createServerChannel(InetSocketAddress address)
                                               throws IOException
Creates and returns a non-blocking TCP server channel bound to the given network address.

Parameters:
address - the address to bind to.
Returns:
the channel
Throws:
IOException

createClientChannel

public static SocketChannel createClientChannel(String hostName,
                                                int port)
                                         throws IOException
Creates and returns a non-blocking TCP server channel bound to the given network host and port.

Parameters:
hostName - the host to bind to.
port - the port to bind to.
Returns:
the channel
Throws:
IOException

createClientChannel

public static SocketChannel createClientChannel(InetSocketAddress address)
                                         throws IOException
Creates and returns a non-blocking TCP client channel bound to the given network address.

Parameters:
address - the address to bind to.
Returns:
the channel
Throws:
IOException

createDatagramChannel

public static DatagramChannel createDatagramChannel(int port)
                                             throws IOException
Creates and returns a non-blocking UDP datagram channel bound to the given network port.

Parameters:
port - the port to bind to.
Returns:
the channel
Throws:
IOException

createDatagramChannel

public static DatagramChannel createDatagramChannel(InetSocketAddress address)
                                             throws IOException
Creates and returns a non-blocking UDP datagram channel bound to the given network address.

Parameters:
address - the address to bind to.
Returns:
the channel
Throws:
IOException

readMany

public static int readMany(ReadableByteChannel channel,
                           ByteBuffer buffer)
                    throws IOException
Efficiently reads (without ever blocking) as many bytes as possible from the given non-blocking channel into the given buffer. Returns the total number of bytes that have been read. The algorithm strongly improves throughput at the expense of somewhat increased latency. This method simply propagates exceptions thrown in the underlying ReadableByteChannel.read(ByteBuffer)NIO code - it never causes an exception itself.

Parameters:
channel - - the channel to read from
buffer - - the buffer to read into
Returns:
the total number of bytes read, possibly zero. If end-of-stream (EOS) is encountered then the number returned is precisely -(total+1). Hence, if EOS is encountered the returned number is always < 0, and else >= 0. In the EOS case, the caller can determine the total number of read bytes with -(readMany(...)+1). (The same trick is used in Arrays.binarySearch(long[], long)to effectively return two return parameters within one parameters).

Example usage:

 
 
 int total = readMany(channel, buffer);
 boolean eos = false;
 if (total < 0) {
 	eos = true;
 	total = -(total + 1);
 }
 
 System.out.println(total + "bytes read");
 
 // some meaningful processing of the read bytes goes here
 
 if (eos) {
 	System.out.println(total + "bytes read, EOS reached, now closing channel");
 	channel.close();
 	System.exit(0);
 }
 
 
 
Throws:
NonReadableChannelException - If this channel was not opened for reading
ClosedChannelException - If this channel is closed
AsynchronousCloseException - If another thread closes this channel while the read operation is in progress
ClosedByInterruptException - If another thread interrupts the current thread while the read operation is in progress, thereby closing the channel and setting the current thread's interrupt status
IOException - If some other I/O error occurs

writeMany

public static int writeMany(WritableByteChannel channel,
                            ByteBuffer buffer)
                     throws IOException
Efficiently writes (without ever blocking) as many bytes as possible from the given buffer to the given non-blocking channel. Returns the total number of bytes that have been written. The algorithm strongly improves throughput at the expense of somewhat increased latency. This method simply propagates exceptions thrown in the underlying WritableByteChannel.write(ByteBuffer)NIO code - it never causes an exception itself.

Parameters:
channel - - the channel to write to
buffer - - the buffer to read from
Returns:
the total number of bytes written, possibly zero.
Throws:
NonWritableChannelException - If this channel was not opened for writing
ClosedChannelException - If this channel is closed
AsynchronousCloseException - If another thread closes this channel while the write operation is in progress
ClosedByInterruptException - If another thread interrupts the current thread while the write operation is in progress, thereby closing the channel and setting the current thread's interrupt status
IOException - If some other I/O error occurs

getRegisteredChannels

public static List getRegisteredChannels(Selector selector)
Returns all selectable channels registered with the given selector, excluding channels of invalid keys.

Parameters:
selector -
Returns:
the channels

getNumberOfReadyOps

public static int getNumberOfReadyOps(SelectionKey key)
Returns the number of ready operations of the given selection key.

Parameters:
key -
Returns:
the number

addInterestBits

public static void addInterestBits(SelectionKey key,
                                   int opBits)
Adds the given operation bits to the interest set of the given key.

Parameters:
key - the selection key to work on.
opBits - the bits to add.

removeInterestBits

public static void removeInterestBits(SelectionKey key,
                                      int opBits)
Removes the given operation bits from the interest set of the given key.

Parameters:
key - the selection key to work on.
opBits - the bits to remove.

hasMark

public static boolean hasMark(Buffer buffer)
Returns whether or not a given buffer has the mark defined.

Parameters:
buffer - the buffer to check
Returns:
true if it has a mark

toString

public static String toString(SelectionKey key)
Returns a detailed string representation of the given selection key for debugging purposes.

Parameters:
key - the object to represent
Returns:
a string representation

toString

public static String toString(int selectionKeyOps)
Returns a detailed string representation of the given selection key readyOps or interestOps for debugging purposes.

Parameters:
selectionKeyOps - the object to represent
Returns:
a string representation

toString

public static String toString(Set keySet)
Returns a detailed string representation for debugging purposes.

Parameters:
keySet - a selector key set (e.g. selector.keys() or selector.selectedKeys()).
Returns:
a string representation

toString

public static String toString(Selector selector)
Returns a detailed string representation of the given selector for debugging purposes.

Parameters:
selector - the object to represent
Returns:
a string representation

copy

public static ByteBuffer copy(ByteBuffer src)
Creates a new bytebuffer that is a deep copy of the given buffer between index buf.position() and buf.limit(); leaves the src buffer unmodified. High performance implementation.

Parameters:
src - the bytebuffer to copy
Returns:
the new byte buffer

toByteArray

public static byte[] toByteArray(ByteBuffer src)
Creates and returns a byte array filled with the given buffer's contents between index buf.position() and buf.limit(); leaves the src buffer unmodified. High performance implementation.

Parameters:
src - the bytebuffer to read from
Returns:
the byte array

toAsciiByteBuffer

public static ByteBuffer toAsciiByteBuffer(String str)
Returns a bytebuffer holding the ASCII bytes of the given string.

Parameters:
str - the string to convert.
Returns:
the byte buffer

toAsciiByteArray

public static byte[] toAsciiByteArray(String str)
Returns a byte array holding the ASCII bytes of the given string.

Parameters:
str - the string to convert.
Returns:
the byte array

toAsciiString

public static String toAsciiString(ByteBuffer buffer)
Returns a ASCII string representation of the bytes in the given buffer.

Parameters:
buffer - the buffer to convert
Returns:
the string representation

toAsciiString

public static String toAsciiString(byte[] bytes)
Returns a ASCII string representation of the given bytes.

Parameters:
bytes - the bytes to convert
Returns:
the string representation

Sea 0.2.0