de.netseeker.ejoe.io
Class IOUtils

java.lang.Object
  extended byde.netseeker.ejoe.io.IOUtils

public final class IOUtils
extends java.lang.Object

Some useful methods for closing io streams, io readers, channels and Selector quitely, as well as methods for non-blocking, semi-blocking and full blocking io read/write operations.

Author:
netseeker aka Michael Manske

Field Summary
static int NIO_MAX_ITERATIONS
           
 
Constructor Summary
IOUtils()
           
 
Method Summary
static java.lang.Object adapterDeserialize(SerializeAdapter adapter, java.io.InputStream in, boolean compressed)
          Invokes a (De)SerializeAdapter for the given Object and handles decompression and buffering
static void adapterSerialize(SerializeAdapter adapter, java.io.OutputStream out, java.lang.Object obj, boolean compressed, int compressionLevel)
          Invokes a SerializeAdapter for the given Object and handles compression and buffering
static void closeQuite(java.nio.channels.Channel channel)
          Tries to close an NIO Channel and handles null values and IOExceptions quietly
static void closeQuite(java.io.InputStream in)
          Tries to close an InputStream and handles null values and IOExceptions quietly
static void closeQuite(java.io.OutputStream out)
          Tries to close an OutputStream and handles null values and IOExceptions quietly
static void closeQuite(java.io.Reader reader)
          Tries to close a Reader and handles null values and IOExceptions quietly
static void closeQuite(java.nio.channels.Selector selector)
          Tries to close a NIO Selector and handles null values and IOExceptions quietly
static void closeQuite(java.io.Writer out)
          Tries to close a Writer and handles null values and IOExceptions quietly
static ConnectionHeader handshake(java.nio.channels.SocketChannel channel, ConnectionHeader header, boolean sendBeforeReceive, long timeout)
          Handshake for a socket channel.
static java.nio.ByteBuffer nonBlockingRead(java.nio.channels.ReadableByteChannel channel, java.nio.ByteBuffer buffer)
          Tries to send the given ByteBuffer completely through the given SocketChannel within a given timeout
static void nonBlockingWrite(java.nio.channels.WritableByteChannel channel, java.nio.ByteBuffer buffer)
          Tries to send the given ByteBuffer completely through the given SocketChannel three times
static int readHeader(ConnectionHeader header)
          Receives a EJOE specific header containing the size of the next ByteBuffer.
static java.nio.ByteBuffer semiBlockingRead(java.nio.channels.ReadableByteChannel channel, java.nio.ByteBuffer buffer, long timeout)
          Tries to read ByteBuffer.remaining() bytes the into given ByteBuffer from the given SocketChannel within a given timeout
static void semiBlockingWrite(java.nio.channels.WritableByteChannel channel, java.nio.ByteBuffer buffer, long timeout)
          Tries to send the given ByteBuffer completely through the given SocketChannel within a given timeout
static void setReceiveBufferSize(java.net.Socket socket, int size)
          Set the SO_RCVBUF hint on a connected socket to the size of the data which are expected to be read next time
static void setSendBufferSize(java.net.Socket socket, int size)
          Set the SO_SNDBUF hint on a connected socket to the size of the data which are expected to be written next time
static void writeHeader(ConnectionHeader header, java.nio.ByteBuffer buffer)
          Sends a EJOE specific header containing the lengh of the given ByteBuffer
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NIO_MAX_ITERATIONS

public static final int NIO_MAX_ITERATIONS
See Also:
Constant Field Values
Constructor Detail

IOUtils

public IOUtils()
Method Detail

closeQuite

public static void closeQuite(java.io.OutputStream out)
Tries to close an OutputStream and handles null values and IOExceptions quietly

Parameters:
out -

closeQuite

public static void closeQuite(java.io.Writer out)
Tries to close a Writer and handles null values and IOExceptions quietly

Parameters:
out -

closeQuite

public static void closeQuite(java.io.InputStream in)
Tries to close an InputStream and handles null values and IOExceptions quietly

Parameters:
in -

closeQuite

public static void closeQuite(java.io.Reader reader)
Tries to close a Reader and handles null values and IOExceptions quietly

Parameters:
reader -

closeQuite

public static void closeQuite(java.nio.channels.Channel channel)
Tries to close an NIO Channel and handles null values and IOExceptions quietly

Parameters:
channel -

closeQuite

public static void closeQuite(java.nio.channels.Selector selector)
Tries to close a NIO Selector and handles null values and IOExceptions quietly

Parameters:
selector -

handshake

public static ConnectionHeader handshake(java.nio.channels.SocketChannel channel,
                                         ConnectionHeader header,
                                         boolean sendBeforeReceive,
                                         long timeout)
                                  throws java.io.IOException
Handshake for a socket channel. It's used as workaround for a know issue with java sockets: Sometimes only the first byte will get transferred through a socket connection when reading from it first time. The other bytes will follow not until the next read. This method sends/receives one Byte through the socket to "initialize" the socket channel. So all following read/write operations don't have to handle that "1-Byte issue". The send/received Byte is used also as connection header, it contains information about compression, nio usage, if the connection is a persistent or non-persistent one...

Parameters:
channel - A connected, readable and writeable socket channel
sendBeforeReceive - if true we will try to send one byte then read one byte otherwise we will use the opposite way around.
Throws:
java.io.IOException

adapterSerialize

public static void adapterSerialize(SerializeAdapter adapter,
                                    java.io.OutputStream out,
                                    java.lang.Object obj,
                                    boolean compressed,
                                    int compressionLevel)
                             throws java.io.IOException
Invokes a SerializeAdapter for the given Object and handles compression and buffering

Parameters:
adapter -
out -
obj -
compressed -
Throws:
java.io.IOException

adapterDeserialize

public static java.lang.Object adapterDeserialize(SerializeAdapter adapter,
                                                  java.io.InputStream in,
                                                  boolean compressed)
                                           throws java.io.IOException
Invokes a (De)SerializeAdapter for the given Object and handles decompression and buffering

Parameters:
adapter -
in -
compressed -
Returns:
Throws:
java.io.IOException

nonBlockingWrite

public static void nonBlockingWrite(java.nio.channels.WritableByteChannel channel,
                                    java.nio.ByteBuffer buffer)
                             throws java.io.IOException
Tries to send the given ByteBuffer completely through the given SocketChannel three times

Parameters:
channel -
buffer -
Throws:
IncompleteIOException - if the given ByteBuffer could not be send completely
java.io.IOException

semiBlockingWrite

public static void semiBlockingWrite(java.nio.channels.WritableByteChannel channel,
                                     java.nio.ByteBuffer buffer,
                                     long timeout)
                              throws java.io.IOException
Tries to send the given ByteBuffer completely through the given SocketChannel within a given timeout

Parameters:
channel -
buffer -
timeout -
Throws:
IncompleteIOException - if the given ByteBuffer could not be send completely
java.io.IOException

nonBlockingRead

public static java.nio.ByteBuffer nonBlockingRead(java.nio.channels.ReadableByteChannel channel,
                                                  java.nio.ByteBuffer buffer)
                                           throws java.io.IOException
Tries to send the given ByteBuffer completely through the given SocketChannel within a given timeout

Parameters:
channel -
buffer -
Returns:
Throws:
java.io.IOException

semiBlockingRead

public static java.nio.ByteBuffer semiBlockingRead(java.nio.channels.ReadableByteChannel channel,
                                                   java.nio.ByteBuffer buffer,
                                                   long timeout)
                                            throws java.io.IOException
Tries to read ByteBuffer.remaining() bytes the into given ByteBuffer from the given SocketChannel within a given timeout

Parameters:
channel -
buffer -
timeout -
Returns:
Throws:
java.io.IOException

readHeader

public static int readHeader(ConnectionHeader header)
                      throws java.io.IOException
Receives a EJOE specific header containing the size of the next ByteBuffer.

Returns:
the length of the following data package
Throws:
java.io.IOException

writeHeader

public static void writeHeader(ConnectionHeader header,
                               java.nio.ByteBuffer buffer)
                        throws java.io.IOException
Sends a EJOE specific header containing the lengh of the given ByteBuffer

Parameters:
buffer -
Throws:
java.io.IOException

setSendBufferSize

public static void setSendBufferSize(java.net.Socket socket,
                                     int size)
                              throws java.net.SocketException
Set the SO_SNDBUF hint on a connected socket to the size of the data which are expected to be written next time

Parameters:
socket - the connected socket
size - size to set for SO_SNDBUF
Throws:
java.net.SocketException

setReceiveBufferSize

public static void setReceiveBufferSize(java.net.Socket socket,
                                        int size)
                                 throws java.net.SocketException
Set the SO_RCVBUF hint on a connected socket to the size of the data which are expected to be read next time

Parameters:
socket - the connected socket
size - size to set for SO_RCVBUF
Throws:
java.net.SocketException


Copyright © 2005-2006 netseeker. All Rights Reserved.