org.spicefactory.cinnamon.io.amf
Class AMFInput

java.lang.Object
  extended by org.spicefactory.cinnamon.io.amf.AMFInput
All Implemented Interfaces:
DataInput

public class AMFInput
extends Object
implements DataInput

The AMFInput class deserializes AMF data from a ByteBuffer and constructs and populates an AMFEnvelope instance with the decoded messages. This implementation only supports AMF3 data. The only method that should be called by application code is readEnvelope which does all the deserialization work. Other methods will be invoked by collaborating objects like class mapping and externalizer instances.

Author:
Jens Halm

Nested Class Summary
protected  class AMFInput.ClassDescriptor
          Represents a decoded class descriptor.
 
Constructor Summary
AMFInput(org.apache.mina.common.ByteBuffer buffer, CinnamonConfig config)
          Creates a new AMFInput instance.
 
Method Summary
protected  void addClassDescriptor(AMFInput.ClassDescriptor desc)
          Adds a decoded ClassDescriptor.
protected  int addObjectReference(Object obj)
          Adds an object to the list of references.
protected  void addStringReference(String str)
          Adds a string to the list of references.
protected  AMFInput.ClassDescriptor getClassDescriptor(int index)
          Returns the class descriptor for the given index.
protected  Object getObjectReference(int index)
          Returns the object reference for the given index.
protected  String getStringReference(int index)
          Returns the string reference for the given index.
protected  List<Object> readAMFArray()
          Reads an AMF3 Array and returns it as a List.
protected  byte[] readAMFByteArray()
          Reads and returns an AMF3 byte array.
protected  Date readAMFDate()
          Reads an AMF3 date value.
 int readAMFInt()
          Reads an AMF3 int value.
protected  Object readAMFObject()
          Reads an AMF3 object.
 String readAMFString()
          Reads an AMF3 string value.
protected  Object readAMFXml()
          Reads an XML string and returns it as a DOM instance.
 boolean readBoolean()
          Reads a boolean value.
 byte readByte()
          Reads and returns a single byte.
 void readBytes(byte[] bytearray, int offset, int length)
          Reads the number of bytes specified by the length parameter into the given byte buffer starting at the specified offset.
 double readDouble()
          Reads and returns a double.
 float readFloat()
          Reads and returns a float.
 int readInt()
          Reads and returns an int.
 String readMultiByte(int length, String charset)
          Reads a multibyte string of specified length using the given character set.
 Object readObject()
          Reads, deserializes and returns an object encoded in AMF3 binary format.
 short readShort()
          Reads and returns a short.
protected  String readString(Charset charset, int length)
          Decodes a String of the specified length with the given character set.
 long readUnsignedInt()
          Reads and returns an unsigned int.
 int readUnsignedShort()
          Reads and returns an unsigned short.
 String readUTF()
          Reads and returns a UTF-8 String.
 String readUTFBytes(int length)
          Reads the specified number of UTF-8 encoded bytes and returns the decoded string.
 void reset()
          Resets all internal Collections of object references.
protected  void setObjectReference(int index, Object obj)
          Sets the object reference for the specified index.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AMFInput

public AMFInput(org.apache.mina.common.ByteBuffer buffer,
                CinnamonConfig config)
Creates a new AMFInput instance.

Parameters:
buffer - the ByteBuffer to read AMF3 data from
config - the Cinnamon configuration instance
Method Detail

readAMFInt

public int readAMFInt()
Reads an AMF3 int value.

Returns:
an int

readAMFString

public String readAMFString()
Reads an AMF3 string value.

Returns:
the decoded string

readString

protected String readString(Charset charset,
                            int length)
Decodes a String of the specified length with the given character set.

Parameters:
charset - the character set to use
length - the length of the String in bytes
Returns:
the decoded String

readAMFDate

protected Date readAMFDate()
Reads an AMF3 date value.

Returns:
the decoded Date value.

readAMFArray

protected List<Object> readAMFArray()
Reads an AMF3 Array and returns it as a List. This implementation does not support "mixed" Arrays with arbitrary String keys as they are difficult to handle for transparent roundtrips from client to server. Use the Dictionary or base Object class in ActionScript for associative arrays.

Returns:
the decoded Array as a List

readAMFObject

protected Object readAMFObject()
Reads an AMF3 object. The object may have an associated class mapping defined in the Cinnamon configuration or it might be an "anonymous" object, for which no alias was registered in ActionScript.

Returns:
the decoded object

readObject

public Object readObject()
Description copied from interface: DataInput
Reads, deserializes and returns an object encoded in AMF3 binary format.

Specified by:
readObject in interface DataInput
Returns:
the deserialized object

readAMFXml

protected Object readAMFXml()
Reads an XML string and returns it as a DOM instance. Which type of DOM object this method returns depends on the Cinnamon configuration. Builtin XML adapters create W3C DOM, dom4j or XOM document instances.

Returns:
the decoded XML string as a DOM document instance.

readAMFByteArray

protected byte[] readAMFByteArray()
Reads and returns an AMF3 byte array.

Returns:
a byte array

reset

public void reset()
Resets all internal Collections of object references.


addStringReference

protected void addStringReference(String str)
Adds a string to the list of references. Each string will only be encoded once in an AMF3 message.

Parameters:
str - a string to add to the list of references

getStringReference

protected String getStringReference(int index)
Returns the string reference for the given index.

Parameters:
index - the index of the string reference
Returns:
the string reference for the given index

addObjectReference

protected int addObjectReference(Object obj)
Adds an object to the list of references. Each object will only be encoded once in an AMF3 message.

Parameters:
obj - an object to add to the list of references

setObjectReference

protected void setObjectReference(int index,
                                  Object obj)
Sets the object reference for the specified index.

Parameters:
index - the index of the object reference
obj - the object reference

getObjectReference

protected Object getObjectReference(int index)
Returns the object reference for the given index.

Parameters:
index - the index of the object reference
Returns:
the object reference for the given index

addClassDescriptor

protected void addClassDescriptor(AMFInput.ClassDescriptor desc)
Adds a decoded ClassDescriptor. For each encoded class a class descriptor will only be included for the first occurance of that class in an AMF message. Further occurrences will just reference the descriptor.

Parameters:
desc - the class descriptor to add

getClassDescriptor

protected AMFInput.ClassDescriptor getClassDescriptor(int index)
Returns the class descriptor for the given index.

Parameters:
index - the index of the class descriptor
Returns:
the class descriptor for the given index

readBoolean

public boolean readBoolean()
Description copied from interface: DataInput
Reads a boolean value. Reads a single byte and returns true if the value is 1 and false if the value is 0. Throws an Exception for all other values.

Specified by:
readBoolean in interface DataInput
Returns:
true if the byte value read was 1, false if it was 0

readByte

public byte readByte()
Description copied from interface: DataInput
Reads and returns a single byte.

Specified by:
readByte in interface DataInput
Returns:
a single byte.

readBytes

public void readBytes(byte[] bytearray,
                      int offset,
                      int length)
Description copied from interface: DataInput
Reads the number of bytes specified by the length parameter into the given byte buffer starting at the specified offset.

Specified by:
readBytes in interface DataInput
Parameters:
bytearray - the buffer to read into
offset - the offset into the data
length - the number of bytes to read

readDouble

public double readDouble()
Description copied from interface: DataInput
Reads and returns a double.

Specified by:
readDouble in interface DataInput
Returns:
a double

readFloat

public float readFloat()
Description copied from interface: DataInput
Reads and returns a float.

Specified by:
readFloat in interface DataInput
Returns:
a double

readShort

public short readShort()
Description copied from interface: DataInput
Reads and returns a short.

Specified by:
readShort in interface DataInput
Returns:
a short

readInt

public int readInt()
Description copied from interface: DataInput
Reads and returns an int.

Specified by:
readInt in interface DataInput
Returns:
an int

readUnsignedInt

public long readUnsignedInt()
Description copied from interface: DataInput
Reads and returns an unsigned int.

Specified by:
readUnsignedInt in interface DataInput
Returns:
an unsigned int as a long value

readUnsignedShort

public int readUnsignedShort()
Reads and returns an unsigned short.

Returns:
an unsigned short as an int value

readUTF

public String readUTF()
Description copied from interface: DataInput
Reads and returns a UTF-8 String. The string must be prefixed with an unsigned short indicating the length in bytes.

Specified by:
readUTF in interface DataInput
Returns:
the decoded string

readUTFBytes

public String readUTFBytes(int length)
Description copied from interface: DataInput
Reads the specified number of UTF-8 encoded bytes and returns the decoded string.

Specified by:
readUTFBytes in interface DataInput
Parameters:
length - the number of bytes to read
Returns:
the decoded string

readMultiByte

public String readMultiByte(int length,
                            String charset)
Description copied from interface: DataInput
Reads a multibyte string of specified length using the given character set.

Specified by:
readMultiByte in interface DataInput
Parameters:
length - the number of bytes to read
charset - the character set to use to decode the bytes
Returns:
the decoded string