com.caucho.hessian.io

Class HessianOutput

Known Direct Subclasses:
HessianSerializerOutput

public class HessianOutput
extends AbstractHessianOutput

Output stream for Hessian requests, compatible with microedition Java. It only uses classes and types available in JDK.

Since HessianOutput does not depend on any classes other than in the JDK, it can be extracted independently into a smaller package.

HessianOutput is unbuffered, so any client needs to provide its own buffering.

 OutputStream os = ...; // from http connection
 HessianOutput out = new HessianOutput(os);
 String value;

 out.startCall("hello");  // start hello call
 out.writeString("arg1"); // write a string argument
 out.completeCall();      // complete the call
 

Field Summary

private IdentityHashMap
_refs
protected OutputStream
os

Fields inherited from class com.caucho.hessian.io.AbstractHessianOutput

_serializerFactory

Constructor Summary

HessianOutput()
Creates an uninitialized Hessian output stream.
HessianOutput(OutputStream os)
Creates a new Hessian output stream, initialized with an underlying output stream.

Method Summary

boolean
addRef(Object object)
If the object has already been written, just write its ref.
void
call(String method, Object[] args)
Writes a complete method call.
void
completeCall()
Completes.
void
completeReply()
Completes reading the reply

A successful completion will have a single value:

 z
 
void
init(OutputStream os)
Initializes the output
void
printLenString(String v)
Prints a string to the stream, encoded as UTF-8 with preceeding length
void
printString(String v)
Prints a string to the stream, encoded as UTF-8
void
printString(String v, int offset, int length)
Prints a string to the stream, encoded as UTF-8
void
printString(char[] v, int offset, int length)
Prints a string to the stream, encoded as UTF-8
boolean
removeRef(Object obj)
Removes a reference.
boolean
replaceRef(Object oldRef, Object newRef)
Replaces a reference from one object to another.
void
startCall(String method)
Starts the method call.
void
startReply()
Starts the reply

A successful completion will have a single value:

 r
 
void
writeBoolean(boolean value)
Writes a boolean value to the stream.
void
writeByteBufferEnd(byte[] buffer, int offset, int length)
Writes a byte buffer to the stream.
void
writeByteBufferPart(byte[] buffer, int offset, int length)
Writes a byte buffer to the stream.
void
writeBytes(byte[] buffer)
Writes a byte array to the stream.
void
writeBytes(byte[] buffer, int offset, int length)
Writes a byte array to the stream.
void
writeDouble(double value)
Writes a double value to the stream.
void
writeFault(String code, String message, Object detail)
Writes a fault.
void
writeHeader(String name)
Writes a header name.
void
writeInt(int value)
Writes an integer value to the stream.
void
writeListBegin(int length, String type)
Writes the list header to the stream.
void
writeListEnd()
Writes the tail of the list to the stream.
void
writeLong(long value)
Writes a long value to the stream.
void
writeMapBegin(String type)
Writes the map header to the stream.
void
writeMapEnd()
Writes the tail of the map to the stream.
void
writeNull()
Writes a null value to the stream.
void
writeObject(Object object)
Writes any object to the output stream.
void
writePlaceholder()
Writes a placeholder.
void
writeRef(int value)
Writes a reference.
void
writeRemote(String type, String url)
Writes a remote object reference to the stream.
void
writeString(String value)
Writes a string value to the stream using UTF-8 encoding.
void
writeString(char[] buffer, int offset, int length)
Writes a string value to the stream using UTF-8 encoding.
void
writeUTCDate(long time)
Writes a date to the stream.

Methods inherited from class com.caucho.hessian.io.AbstractHessianOutput

addRef, completeCall, init, removeRef, replaceRef, setSerializerFactory, startCall, writeBoolean, writeByteBufferEnd, writeByteBufferPart, writeBytes, writeBytes, writeDouble, writeInt, writeListBegin, writeListEnd, writeLong, writeMapBegin, writeMapEnd, writeNull, writeObject, writeRef, writeRemote, writeString, writeString, writeUTCDate

Field Details

_refs

private IdentityHashMap _refs

os

protected OutputStream os

Constructor Details

HessianOutput

public HessianOutput()
Creates an uninitialized Hessian output stream.

HessianOutput

public HessianOutput(OutputStream os)
Creates a new Hessian output stream, initialized with an underlying output stream.
Parameters:
os - the underlying output stream.

Method Details

addRef

public boolean addRef(Object object)
            throws IOException
If the object has already been written, just write its ref.
Overrides:
addRef in interface AbstractHessianOutput
Returns:
true if we're writing a ref.

call

public void call(String method,
                 Object[] args)
            throws IOException
Writes a complete method call.

completeCall

public void completeCall()
            throws IOException
Completes.
 z
 
Overrides:
completeCall in interface AbstractHessianOutput

completeReply

public void completeReply()
            throws IOException
Completes reading the reply

A successful completion will have a single value:

 z
 

init

public void init(OutputStream os)
Initializes the output
Overrides:
init in interface AbstractHessianOutput

printLenString

public void printLenString(String v)
            throws IOException
Prints a string to the stream, encoded as UTF-8 with preceeding length
Parameters:
v - the string to print.

printString

public void printString(String v)
            throws IOException
Prints a string to the stream, encoded as UTF-8
Parameters:
v - the string to print.

printString

public void printString(String v,
                        int offset,
                        int length)
            throws IOException
Prints a string to the stream, encoded as UTF-8
Parameters:
v - the string to print.

printString

public void printString(char[] v,
                        int offset,
                        int length)
            throws IOException
Prints a string to the stream, encoded as UTF-8
Parameters:
v - the string to print.

removeRef

public boolean removeRef(Object obj)
            throws IOException
Removes a reference.
Overrides:
removeRef in interface AbstractHessianOutput

replaceRef

public boolean replaceRef(Object oldRef,
                          Object newRef)
            throws IOException
Replaces a reference from one object to another.
Overrides:
replaceRef in interface AbstractHessianOutput

startCall

public void startCall(String method)
            throws IOException
Starts the method call. Clients would use startCall instead of call if they wanted finer control over writing the arguments, or needed to write headers.
 c major minor
 m b16 b8 method-name
 
Overrides:
startCall in interface AbstractHessianOutput
Parameters:
method - the method name to call.

startReply

public void startReply()
            throws IOException
Starts the reply

A successful completion will have a single value:

 r
 

writeBoolean

public void writeBoolean(boolean value)
            throws IOException
Writes a boolean value to the stream. The boolean will be written with the following syntax:
 T
 F
 
Overrides:
writeBoolean in interface AbstractHessianOutput
Parameters:
value - the boolean value to write.

writeByteBufferEnd

public void writeByteBufferEnd(byte[] buffer,
                               int offset,
                               int length)
            throws IOException
Writes a byte buffer to the stream.
 b b16 b18 bytes
 
Overrides:
writeByteBufferEnd in interface AbstractHessianOutput

writeByteBufferPart

public void writeByteBufferPart(byte[] buffer,
                                int offset,
                                int length)
            throws IOException
Writes a byte buffer to the stream.
 b b16 b18 bytes
 
Overrides:
writeByteBufferPart in interface AbstractHessianOutput

writeBytes

public void writeBytes(byte[] buffer)
            throws IOException
Writes a byte array to the stream. The array will be written with the following syntax:
 B b16 b18 bytes
 
If the value is null, it will be written as
 N
 
Overrides:
writeBytes in interface AbstractHessianOutput
Parameters:

writeBytes

public void writeBytes(byte[] buffer,
                       int offset,
                       int length)
            throws IOException
Writes a byte array to the stream. The array will be written with the following syntax:
 B b16 b18 bytes
 
If the value is null, it will be written as
 N
 
Overrides:
writeBytes in interface AbstractHessianOutput
Parameters:

writeDouble

public void writeDouble(double value)
            throws IOException
Writes a double value to the stream. The double will be written with the following syntax:
 D b64 b56 b48 b40 b32 b24 b16 b8
 
Overrides:
writeDouble in interface AbstractHessianOutput
Parameters:
value - the double value to write.

writeFault

public void writeFault(String code,
                       String message,
                       Object detail)
            throws IOException
Writes a fault. The fault will be written as a descriptive string followed by an object:
 f
 <string>code
 <string>the fault code

 <string>message
 <string>the fault mesage

 <string>detail
 mt\x00\xnnjavax.ejb.FinderException
     ...
 z
 z
 
Parameters:
code - the fault code, a three digit

writeHeader

public void writeHeader(String name)
            throws IOException
Writes a header name. The header value must immediately follow.
 H b16 b8 foo value
 

writeInt

public void writeInt(int value)
            throws IOException
Writes an integer value to the stream. The integer will be written with the following syntax:
 I b32 b24 b16 b8
 
Overrides:
writeInt in interface AbstractHessianOutput
Parameters:
value - the integer value to write.

writeListBegin

public void writeListBegin(int length,
                           String type)
            throws IOException
Writes the list header to the stream. List writers will call writeListBegin followed by the list contents and then call writeListEnd.
 V
 t b16 b8 type
 l b32 b24 b16 b8
 
Overrides:
writeListBegin in interface AbstractHessianOutput

writeListEnd

public void writeListEnd()
            throws IOException
Writes the tail of the list to the stream.
Overrides:
writeListEnd in interface AbstractHessianOutput

writeLong

public void writeLong(long value)
            throws IOException
Writes a long value to the stream. The long will be written with the following syntax:
 L b64 b56 b48 b40 b32 b24 b16 b8
 
Overrides:
writeLong in interface AbstractHessianOutput
Parameters:
value - the long value to write.

writeMapBegin

public void writeMapBegin(String type)
            throws IOException
Writes the map header to the stream. Map writers will call writeMapBegin followed by the map contents and then call writeMapEnd.
 Mt b16 b8 ( )z
 
Overrides:
writeMapBegin in interface AbstractHessianOutput

writeMapEnd

public void writeMapEnd()
            throws IOException
Writes the tail of the map to the stream.
Overrides:
writeMapEnd in interface AbstractHessianOutput

writeNull

public void writeNull()
            throws IOException
Writes a null value to the stream. The null will be written with the following syntax
 N
 
Overrides:
writeNull in interface AbstractHessianOutput
Parameters:

writeObject

public void writeObject(Object object)
            throws IOException
Writes any object to the output stream.
Overrides:
writeObject in interface AbstractHessianOutput

writePlaceholder

public void writePlaceholder()
            throws IOException
Writes a placeholder.
 P
 

writeRef

public void writeRef(int value)
            throws IOException
Writes a reference.
 R b32 b24 b16 b8
 
Overrides:
writeRef in interface AbstractHessianOutput
Parameters:
value - the integer value to write.

writeRemote

public void writeRemote(String type,
                        String url)
            throws IOException
Writes a remote object reference to the stream. The type is the type of the remote interface.
 'r' 't' b16 b8 type url
 
Overrides:
writeRemote in interface AbstractHessianOutput

writeString

public void writeString(String value)
            throws IOException
Writes a string value to the stream using UTF-8 encoding. The string will be written with the following syntax:
 S b16 b8 string-value
 
If the value is null, it will be written as
 N
 
Overrides:
writeString in interface AbstractHessianOutput
Parameters:
value - the string value to write.

writeString

public void writeString(char[] buffer,
                        int offset,
                        int length)
            throws IOException
Writes a string value to the stream using UTF-8 encoding. The string will be written with the following syntax:
 S b16 b8 string-value
 
If the value is null, it will be written as
 N
 
Overrides:
writeString in interface AbstractHessianOutput
Parameters:

writeUTCDate

public void writeUTCDate(long time)
            throws IOException
Writes a date to the stream.
 T  b64 b56 b48 b40 b32 b24 b16 b8
 
Overrides:
writeUTCDate in interface AbstractHessianOutput
Parameters:
time - the date in milliseconds from the epoch in UTC