feud.clientserver
Class DataContainer

java.lang.Object
  extended byfeud.clientserver.DataContainer
All Implemented Interfaces:
java.lang.Cloneable
Direct Known Subclasses:
EventDataContainer, ExtendedDataContainer, InfoDataContainer

public class DataContainer
extends java.lang.Object
implements java.lang.Cloneable

DataContainers are the basic transfer containers for data in this little client/server-world. Each transfer from or to a server or a client will most likely contain at least one DataContainer dropped into an array of DataContainers which itself will be wrapped into an array of bytes.
A transfer can contain multiple DataContainers of different types. What's stored into these containers is up to the implementation.
One can easily extend the DataContainer to wrap complex data in a convenient way.


Field Summary
protected  ClientInfo ci
           
protected  int msgType
           
protected  java.util.List objs
           
protected  int pos
           
static byte TYPE_BYTE
           
static byte TYPE_FLOAT
           
static byte TYPE_INT
           
static byte TYPE_STRING
           
protected  java.util.List types
           
 
Constructor Summary
DataContainer()
          Creates a new, empty DataContainer.
DataContainer(byte[] bytes)
          Creates a new DataContainer and populates it with the data wrapped in the given byte arrays.
DataContainer(byte[] bytes, boolean zip)
          Creates a new DataContainer and populates it with the data wrapped in the given byte arrays.
 
Method Summary
 void add(byte byt)
          Adds a new byte to the container.
 void add(float flt)
          Adds a new float to the container.
 void add(int in)
          Adds a new int to the container.
 void add(java.lang.String str)
          Adds a new String to the container.
 java.lang.Object clone()
           
 int getBufferLength()
          Returns the number of entries in this container
 ClientInfo getClientInfo()
          Returns the ClientInfo, the info about the client from which this container came.
 int getLength()
          Returns the length of the byte array from which this container has been created.
 int getMessageType()
          Gets the message type of this container as defined by MessageTypes.
 byte getNextByte()
          Reads the next byte from the container.
 float getNextFloat()
          Reads the next float from the container.
 int getNextInt()
          Reads the next int from the container.
 java.lang.String getNextString()
          Reads the next string from the container.
 byte[] getRawData()
          Returns the raw data (after unzipping) if this DataContainer has been created from a byte array.
 byte getType()
          Returns the type of the next entry in the container.
(package private)  boolean getZip()
          Gets the zip mode of this container
 boolean hasData()
          Checks, if the container contains more data to read.
 void reset()
          Clears the container.
 void setClientInfo(ClientInfo c)
          Sets the ClientInfo, the info about the client from which this container came.
 void setMessageType(int type)
          Sets the message type of this container as defined by MessageTypes.
(package private)  void setZip(boolean zip)
          Sets zip mode for this container
 DataContainer skip()
          Skips an entry in the container.
(package private)  byte[] toByteArray(boolean zip)
          Wraps the container's data into a byte array.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

TYPE_INT

public static final byte TYPE_INT
See Also:
Constant Field Values

TYPE_FLOAT

public static final byte TYPE_FLOAT
See Also:
Constant Field Values

TYPE_STRING

public static final byte TYPE_STRING
See Also:
Constant Field Values

TYPE_BYTE

public static final byte TYPE_BYTE
See Also:
Constant Field Values

objs

protected java.util.List objs

types

protected java.util.List types

pos

protected int pos

ci

protected ClientInfo ci

msgType

protected int msgType
Constructor Detail

DataContainer

public DataContainer()
Creates a new, empty DataContainer.


DataContainer

public DataContainer(byte[] bytes)
Creates a new DataContainer and populates it with the data wrapped in the given byte arrays.

Parameters:
bytes - the data for this container

DataContainer

public DataContainer(byte[] bytes,
                     boolean zip)
Creates a new DataContainer and populates it with the data wrapped in the given byte arrays.

Parameters:
bytes - the data for this container
zip - is the data in this array zipped?
Method Detail

getLength

public int getLength()
Returns the length of the byte array from which this container has been created. If it hasn't, it's 0.

Returns:
int the length or 0

getBufferLength

public int getBufferLength()
Returns the number of entries in this container

Returns:
int the number of entries

clone

public java.lang.Object clone()
                       throws java.lang.CloneNotSupportedException
Throws:
java.lang.CloneNotSupportedException

getRawData

public byte[] getRawData()
Returns the raw data (after unzipping) if this DataContainer has been created from a byte array. If it hasn't, this method returns null. This method is useful for extending DataContainer so that the extended instance can be created from an instance of DataContainer simply by feeding the raw data back into super(...);

Returns:
byte[] the raw data. Another DataContainer can be created from this one.

setClientInfo

public void setClientInfo(ClientInfo c)
Sets the ClientInfo, the info about the client from which this container came. This information isn't transfered, it's set by the server itself when creating the containers from the stream.

Parameters:
c - the client info

getClientInfo

public ClientInfo getClientInfo()
Returns the ClientInfo, the info about the client from which this container came. This information isn't transfered, it's set by the server itself when creating the containers from the stream.

Returns:
ClientInfo the client info

getMessageType

public int getMessageType()
Gets the message type of this container as defined by MessageTypes.

Returns:
int the type

setMessageType

public void setMessageType(int type)
Sets the message type of this container as defined by MessageTypes.

Parameters:
type - the message type

add

public void add(float flt)
Adds a new float to the container.

Parameters:
flt - a float

add

public void add(int in)
Adds a new int to the container.

Parameters:
in - a int

add

public void add(byte byt)
Adds a new byte to the container.

Parameters:
byt - a byte

add

public void add(java.lang.String str)
Adds a new String to the container.

Parameters:
str - a String

reset

public void reset()
Clears the container. It's empty afterwards.


hasData

public boolean hasData()
Checks, if the container contains more data to read.

Returns:
boolean does it?

skip

public DataContainer skip()
Skips an entry in the container.

Returns:
DataContainer the same container as return value for convenience reasons.

getType

public byte getType()
Returns the type of the next entry in the container.

Returns:
byte the type

getNextFloat

public float getNextFloat()
Reads the next float from the container. If it's not a float, this will cause an ClassCastException.

Returns:
float the next float

getNextInt

public int getNextInt()
Reads the next int from the container. If it's not an int, this will cause an ClassCastException.

Returns:
int the next int

getNextByte

public byte getNextByte()
Reads the next byte from the container. If it's not a byte, this will cause an ClassCastException.

Returns:
byte the next byte

getNextString

public java.lang.String getNextString()
Reads the next string from the container. If it's not a string, this will cause an ClassCastException.

Returns:
String the next string

toByteArray

byte[] toByteArray(boolean zip)
Wraps the container's data into a byte array.

Parameters:
zip - boolean
Returns:
byte[]

setZip

void setZip(boolean zip)
Sets zip mode for this container

Parameters:
zip - zipped?

getZip

boolean getZip()
Gets the zip mode of this container

Returns:
boolean zipped?