|
Sea 0.2.0 | ||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object gov.lbl.dsd.sea.nio.NioUtil
Various utilities related to the java.nio
package.
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 |
public static ServerSocketChannel createServerChannel(int port) throws IOException
port
- the port to bind to.
IOException
public static ServerSocketChannel createServerChannel(InetSocketAddress address) throws IOException
address
- the address to bind to.
IOException
public static SocketChannel createClientChannel(String hostName, int port) throws IOException
hostName
- the host to bind to.port
- the port to bind to.
IOException
public static SocketChannel createClientChannel(InetSocketAddress address) throws IOException
address
- the address to bind to.
IOException
public static DatagramChannel createDatagramChannel(int port) throws IOException
port
- the port to bind to.
IOException
public static DatagramChannel createDatagramChannel(InetSocketAddress address) throws IOException
address
- the address to bind to.
IOException
public static int readMany(ReadableByteChannel channel, ByteBuffer buffer) throws IOException
ReadableByteChannel.read(ByteBuffer)
NIO code - it never causes
an exception itself.
channel
- -
the channel to read frombuffer
- -
the buffer to read into
-(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); }
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 occurspublic static int writeMany(WritableByteChannel channel, ByteBuffer buffer) throws IOException
WritableByteChannel.write(ByteBuffer)
NIO code - it never causes
an exception itself.
channel
- -
the channel to write tobuffer
- -
the buffer to read from
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 occurspublic static List getRegisteredChannels(Selector selector)
selector
-
public static int getNumberOfReadyOps(SelectionKey key)
key
-
public static void addInterestBits(SelectionKey key, int opBits)
key
- the selection key to work on.opBits
- the bits to add.public static void removeInterestBits(SelectionKey key, int opBits)
key
- the selection key to work on.opBits
- the bits to remove.public static boolean hasMark(Buffer buffer)
buffer
- the buffer to check
public static String toString(SelectionKey key)
key
- the object to represent
public static String toString(int selectionKeyOps)
selectionKeyOps
- the object to represent
public static String toString(Set keySet)
keySet
- a selector key set (e.g. selector.keys() or
selector.selectedKeys()).
public static String toString(Selector selector)
selector
- the object to represent
public static ByteBuffer copy(ByteBuffer src)
src
- the bytebuffer to copy
public static byte[] toByteArray(ByteBuffer src)
src
- the bytebuffer to read from
public static ByteBuffer toAsciiByteBuffer(String str)
str
- the string to convert.
public static byte[] toAsciiByteArray(String str)
str
- the string to convert.
public static String toAsciiString(ByteBuffer buffer)
buffer
- the buffer to convert
public static String toAsciiString(byte[] bytes)
bytes
- the bytes to convert
|
Sea 0.2.0 | ||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |