de.netseeker.ejoe.io
Class DataChannel

java.lang.Object
  extended by de.netseeker.ejoe.io.DataChannel

public class DataChannel
extends java.lang.Object

Utility class handling all socket oriented data IO on nio channels. DataChannels must be implemented as singletons to avoid creation of a new object for each socket IO operation. Otherwise heavy load could result in fast-growing memory consumption.

Since:
0.3.9.1
Author:
netseeker

Constructor Summary
protected DataChannel()
          Singleton with hidden constructor, only child classes are allowed to construct new instances
 
Method Summary
 java.nio.ByteBuffer decode(java.nio.ByteBuffer buffer)
          Decodes and reformats request data if the underlying protocol layer makes it neccessary
static DataChannel getInstance()
          Invoking this method has the same effect as invoking DataChannel#getInstance(null)
static DataChannel getInstance(ConnectionHeader header)
          Returns appropiate instance of DataChannel for the given connection header.
 ConnectionHeader handshake(ConnectionHeader header, java.nio.channels.SocketChannel channel, long timeout)
          Handshake for a socket channel.
static void 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
 void nonBlockingWrite(java.nio.channels.WritableByteChannel channel, java.nio.ByteBuffer buffer)
          Tries to send the given ByteBuffer completely through the given SocketChannel three times
 int readHeader(ConnectionHeader header, long timeout)
          Receives a EJOE specific header containing the size of the next ByteBuffer.
static void 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.
 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
 void writeHeader(ConnectionHeader header, java.nio.ByteBuffer buffer, long timeout)
          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
 

Constructor Detail

DataChannel

protected DataChannel()
Singleton with hidden constructor, only child classes are allowed to construct new instances

Method Detail

getInstance

public static DataChannel getInstance()
Invoking this method has the same effect as invoking DataChannel#getInstance(null)

Returns:
a default instance of DataChannel

getInstance

public static DataChannel getInstance(ConnectionHeader header)
Returns appropiate instance of DataChannel for the given connection header. If the header is null an instance of this class will be returned.

Parameters:
header - a valid connection header or null
Returns:
an instance of DataChannel

handshake

public ConnectionHeader handshake(ConnectionHeader header,
                                  java.nio.channels.SocketChannel channel,
                                  long timeout)
                           throws java.io.IOException,
                                  java.text.ParseException
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:
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
java.text.ParseException

nonBlockingWrite

public 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 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 void 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 -
Throws:
java.io.IOException

semiBlockingRead

public static void 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 -
Throws:
java.io.IOException

readHeader

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

Parameters:
timeout - read timeout
Returns:
the length of the following data package
Throws:
java.io.IOException

writeHeader

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

Parameters:
timeout - write timeout
Throws:
java.io.IOException

decode

public java.nio.ByteBuffer decode(java.nio.ByteBuffer buffer)
                           throws java.io.UnsupportedEncodingException
Decodes and reformats request data if the underlying protocol layer makes it neccessary

Parameters:
buffer -
Throws:
java.io.UnsupportedEncodingException


Copyright © 2005-2007 netseeker. All Rights Reserved.