comm2j
Class Read2J

java.lang.Object
  |
  +--comm2j.Read2J

public class Read2J
extends java.lang.Object

Read2J is a class used to communicate with another peer (J2ME or J2SE), transmitting and receiving Messages throws an opened Socket connection.
Read2J supports compression (LZW), too. This means that you can send and receive transparently data through a compression tunnel.
It's possible to have just a compressed way (eg: from server to client), so if you have to send little quantity of data (that cannot be compressed), you can send them raw.
The other peer will automatically recognise if the communication to him is raw or compressed.

Version:
1.0
Author:
Luca Bertoncello <lucabert@lucabert.de>
See Also:
MessageI

Field Summary
static int COMPRESSED
          Defines a compressed (LZW) communication between peers
static int PACKLENGTH
          Defines the number of bytes used to describe the lenght of the packet
static int RAW
          Defines a normal communication between peers
 
Constructor Summary
Read2J()
          Constructor.
 
Method Summary
 int getMode()
          Returns the mode used by Read2J for the communication
static int readLength(java.io.InputStream is)
          Gets the length of the packet from given InputStream.
The length is coded in 3 bytes, so it's possible to use just 7 bits per byte and armor the codes so that LZW can encode/decode them.
 Message readMessageFromStream(java.io.InputStream is)
          Receive a message from the remote peer.
It automatically recognises if the communication was compressed or not.
 void setMode(int mode)
          Sets the mode used by Read2J for the communication
static void writeLength(java.io.OutputStream os, int len)
          Sends the length of the packet throws given OutputStream.
The length will be coded in 3 bytes, so it's possible to use just 7 bits per byte and armor the codes so that LZW can encode/decode them.
 int writeMessageToStream(Message msg, java.io.OutputStream os)
          Sends a message to the remote peer.
If compression mode was set, the stream will be compressed.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

RAW

public static final int RAW
Defines a normal communication between peers

COMPRESSED

public static final int COMPRESSED
Defines a compressed (LZW) communication between peers

PACKLENGTH

public static final int PACKLENGTH
Defines the number of bytes used to describe the lenght of the packet
Constructor Detail

Read2J

public Read2J()
Constructor. Used communication mode will be setted to default (RAW)
Method Detail

getMode

public int getMode()
Returns the mode used by Read2J for the communication
Returns:
Currently used communication mode

setMode

public void setMode(int mode)
Sets the mode used by Read2J for the communication
Parameters:
mode - Communication mode to be used

writeMessageToStream

public int writeMessageToStream(Message msg,
                                java.io.OutputStream os)
                         throws java.io.IOException
Sends a message to the remote peer.
If compression mode was set, the stream will be compressed.
Parameters:
msg - Message to be sent
os - OutputStream
Returns:
number of sent bytes
Throws:
java.io.IOException - if an error occoured

readMessageFromStream

public Message readMessageFromStream(java.io.InputStream is)
                              throws java.io.IOException
Receive a message from the remote peer.
It automatically recognises if the communication was compressed or not.
Parameters:
is - InputStream
Returns:
Message read from the remote host
Throws:
java.io.IOException - if an error occoured

writeLength

public static void writeLength(java.io.OutputStream os,
                               int len)
                        throws java.io.IOException
Sends the length of the packet throws given OutputStream.
The length will be coded in 3 bytes, so it's possible to use just 7 bits per byte and armor the codes so that LZW can encode/decode them.
Parameters:
os - OutputStream to be used
len - Length to be sent
Throws:
java.io.IOException - if an error occours
See Also:
LZW2J

readLength

public static int readLength(java.io.InputStream is)
                      throws java.io.IOException
Gets the length of the packet from given InputStream.
The length is coded in 3 bytes, so it's possible to use just 7 bits per byte and armor the codes so that LZW can encode/decode them.
Parameters:
is - InputStream to be used
Returns:
Length of the packet
Throws:
java.io.IOException - if an error occours
See Also:
LZW2J