org.spicefactory.cinnamon.io.amf
Class AMFOutput

java.lang.Object
  extended by org.spicefactory.cinnamon.io.amf.AMFOutput
All Implemented Interfaces:
DataOutput

public class AMFOutput
extends Object
implements DataOutput

The AMFOutput class serializes an AMFEnvelope instance containing one or more results from service invocations to AMF data and writes it to a ByteBuffer. This implementation only supports AMF3 data. The only method that should be called by application code is writeEnvelope which does all the serialization work. Other methods will be invoked by collaborating objects like class mapping and externalizer instances.

Author:
Jens Halm

Field Summary
static int MAX_INT
           
static int MIN_INT
           
 
Constructor Summary
AMFOutput(org.apache.mina.common.ByteBuffer buffer, CinnamonConfig config)
          Creates a new AMFOutput instance.
 
Method Summary
protected  void addClassDescriptor(ClassMapping mapping)
          Adds a class mapping to the list of references.
protected  void addObjectReference(Object value)
          Adds an object to the list of references.
protected  void addStringReference(String str)
          Adds a string to the list of references.
protected  Integer getClassDescriptorIndex(ClassMapping mapping)
          Returns the index for the specified mapping if this mapping has already been serialized in the current AMF message.
 void reset()
          Resets all internal Collections of object references.
protected  void writeAMFArray(Object value)
          Writes an AMF3 Array value.
protected  void writeAMFByteArray(byte[] value)
          Writes an AMF3 byte array.
protected  void writeAMFDate(Date value)
          Writes an AMF3 Date value.
protected  void writeAMFInt(int value)
          Writes an AMF3 int value including the type code byte.
protected  void writeAMFIntOrNumber(Number value)
          Writes the specified Number value as an AMF int or AMF Number depending on the type of Number.
 void writeAMFIntValue(int value)
          Writes an AMF3 int value without the type code byte.
protected  void writeAMFLengthAndString(String value)
          Writes the specified String in UTF-8 encoding prepending by an int for the length of the String in bytes.
protected  void writeAMFNumber(double value)
          Writes a double value as an AMF3 Number.
protected  void writeAMFObject(Object object)
          Writes an AMF3 object.
protected  void writeAMFString(String value)
          Writes an AMF3 String value in UTF-8 encoding including the type code byte.
 void writeAMFStringValue(String value)
          Writes an AMF3 String value in UTF-8 encoding without the type code byte.
protected  void writeAMFXml(Object value)
          Writes a DOM document instance.
 void writeBoolean(boolean value)
          Writes a boolean value as a single byte.
 void writeByte(byte value)
          Writes a single byte.
 void writeBytes(byte[] bytes, int offset, int length)
          Writes the number of bytes specified by the length parameter from the given byte buffer starting at the specified offset.
 void writeDouble(double value)
          Writes a double value.
 void writeFloat(float value)
          Writes a float value.
 void writeInt(int value)
          Writes an int value.
 void writeMultiByte(String value, String charset)
          Writes a multibyte string using the given character set.
 void writeObject(Object value)
          Encodes and writes an object in AMF3 binary format.
protected  boolean writeObjectReference(Object value)
          Writes an object reference to the ByteBuffer if the specified value was already encoded in this AMF message.
 void writeShort(short value)
          Writes a short value.
protected  void writeString(String value, Charset charset)
          Writes a string value using the specified character set.
protected  boolean writeStringReference(String value)
          Writes a String reference to the ByteBuffer if the specified value was already encoded in this AMF message.
 void writeUnsignedInt(long value)
          Writes an unsigned int.
 void writeUTF(String value)
          Writes a UTF-8 string prepended by a short value representing the length of the string in bytes.
 void writeUTFBytes(String value)
          Writes a UTF-8 string.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MAX_INT

public static final int MAX_INT
See Also:
Constant Field Values

MIN_INT

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

AMFOutput

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

Parameters:
buffer - the ByteBuffer to write AMF3 data into
config - the Cinnamon configuration instance
Method Detail

writeObject

public void writeObject(Object value)
Description copied from interface: DataOutput
Encodes and writes an object in AMF3 binary format.

Specified by:
writeObject in interface DataOutput
Parameters:
value - the object to encode and write

writeAMFIntOrNumber

protected void writeAMFIntOrNumber(Number value)
Writes the specified Number value as an AMF int or AMF Number depending on the type of Number. Double and float values will always be written as an AMF Number. Long and int values will be written as an AMF int if the value is in the range of an AMF integer, otherwise it will be written as an AMF Number. Byte and short values will always be written as an AMF int.

Parameters:
value - the Number to be serialized

writeAMFInt

protected void writeAMFInt(int value)
Writes an AMF3 int value including the type code byte.

Parameters:
value - the int value to serialize

writeAMFIntValue

public void writeAMFIntValue(int value)
Writes an AMF3 int value without the type code byte.

Parameters:
value - the int value to serialize

writeAMFNumber

protected void writeAMFNumber(double value)
Writes a double value as an AMF3 Number.

Parameters:
value - the double value to serialize

writeAMFString

protected void writeAMFString(String value)
Writes an AMF3 String value in UTF-8 encoding including the type code byte.

Parameters:
value - the String value to be serialized

writeAMFStringValue

public void writeAMFStringValue(String value)
Writes an AMF3 String value in UTF-8 encoding without the type code byte.

Parameters:
value - the String value to be serialized

writeAMFLengthAndString

protected void writeAMFLengthAndString(String value)
Writes the specified String in UTF-8 encoding prepending by an int for the length of the String in bytes.

Parameters:
value - the String value to be serialized

writeAMFXml

protected void writeAMFXml(Object value)
Writes a DOM document instance. The actual type of document instance depends on the XML DOM API in use. This can be specified in the Cinnamon configuration file.

Parameters:
value - the DOM document instance to serialize

writeAMFDate

protected void writeAMFDate(Date value)
Writes an AMF3 Date value.

Parameters:
value - the Date to be serialized

writeAMFArray

protected void writeAMFArray(Object value)
Writes an AMF3 Array value. Permitted types for the specified value are Arrays and Collections.

Parameters:
value - the array or collection to be serialized

writeAMFByteArray

protected void writeAMFByteArray(byte[] value)
Writes an AMF3 byte array.

Parameters:
value - the byte array to be serialized

writeAMFObject

protected void writeAMFObject(Object object)
Writes an AMF3 object. The object may have an associated class mapping defined in the Cinnamon configuration or it might be an "anonymous" object (usually represented by a Map in Java), for which no alias was registered in ActionScript.

Parameters:
object - the object to be serialized

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

writeStringReference

protected boolean writeStringReference(String value)
Writes a String reference to the ByteBuffer if the specified value was already encoded in this AMF message.

Parameters:
value - the string value a reference should be written for
Returns:
true if a reference was written

addObjectReference

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

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

writeObjectReference

protected boolean writeObjectReference(Object value)
Writes an object reference to the ByteBuffer if the specified value was already encoded in this AMF message.

Parameters:
value - the object value a reference should be written for
Returns:
true if a reference was written

addClassDescriptor

protected void addClassDescriptor(ClassMapping mapping)
Adds a class mapping to the list of references. For each mapping a class descriptor will only be encoded once in an AMF3 message.

Parameters:
mapping - a mapping to add to the map of class descriptors

getClassDescriptorIndex

protected Integer getClassDescriptorIndex(ClassMapping mapping)
Returns the index for the specified mapping if this mapping has already been serialized in the current AMF message.

Parameters:
mapping - the class mapping to return the reference index for
Returns:
the index of the class descriptor reference or null if it has not been serialized yet in the current message

writeBoolean

public void writeBoolean(boolean value)
Description copied from interface: DataOutput
Writes a boolean value as a single byte. Writes 1 if the value is true, 0 otherwise.

Specified by:
writeBoolean in interface DataOutput
Parameters:
value - the boolean value to write

writeByte

public void writeByte(byte value)
Description copied from interface: DataOutput
Writes a single byte.

Specified by:
writeByte in interface DataOutput
Parameters:
value - the byte value to write

writeBytes

public void writeBytes(byte[] bytes,
                       int offset,
                       int length)
Description copied from interface: DataOutput
Writes the number of bytes specified by the length parameter from the given byte buffer starting at the specified offset.

Specified by:
writeBytes in interface DataOutput
Parameters:
bytes - the data to write
offset - the offset into the data
length - the number of bytes to write

writeDouble

public void writeDouble(double value)
Description copied from interface: DataOutput
Writes a double value.

Specified by:
writeDouble in interface DataOutput
Parameters:
value - the double value to write

writeFloat

public void writeFloat(float value)
Description copied from interface: DataOutput
Writes a float value.

Specified by:
writeFloat in interface DataOutput
Parameters:
value - the float value to write

writeShort

public void writeShort(short value)
Description copied from interface: DataOutput
Writes a short value.

Specified by:
writeShort in interface DataOutput
Parameters:
value - the short value to write

writeInt

public void writeInt(int value)
Description copied from interface: DataOutput
Writes an int value.

Specified by:
writeInt in interface DataOutput
Parameters:
value - the int value to write

writeUnsignedInt

public void writeUnsignedInt(long value)
Description copied from interface: DataOutput
Writes an unsigned int. The specified long value must be in the range of a 32bit unsigned integer.

Specified by:
writeUnsignedInt in interface DataOutput

writeUTF

public void writeUTF(String value)
Description copied from interface: DataOutput
Writes a UTF-8 string prepended by a short value representing the length of the string in bytes.

Specified by:
writeUTF in interface DataOutput

writeUTFBytes

public void writeUTFBytes(String value)
Description copied from interface: DataOutput
Writes a UTF-8 string.

Specified by:
writeUTFBytes in interface DataOutput

writeMultiByte

public void writeMultiByte(String value,
                           String charset)
Description copied from interface: DataOutput
Writes a multibyte string using the given character set.

Specified by:
writeMultiByte in interface DataOutput
Parameters:
value - the string to write
charset - the character set to use to decode the bytes

writeString

protected void writeString(String value,
                           Charset charset)
Writes a string value using the specified character set.

Parameters:
value - the string value to be serialized
charset - the character set to use