com.caucho.hessian.io
Class Hessian2Output

java.lang.Object
  extended by com.caucho.hessian.io.AbstractHessianOutput
      extended by com.caucho.hessian.io.Hessian2Output
All Implemented Interfaces:
Hessian2Constants

public class Hessian2Output
extends AbstractHessianOutput
implements Hessian2Constants

Output stream for Hessian 2 requests.

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
 Hessian2Output out = new Hessian11Output(os);
 String value;

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


Nested Class Summary
(package private)  class Hessian2Output.BytesOutputStream
           
 
Field Summary
private  byte[] _buffer
           
private  java.util.HashMap _classRefs
           
private  boolean _isCloseStreamOnClose
           
private  boolean _isStreaming
           
private  int _offset
           
protected  java.io.OutputStream _os
           
private  IdentityIntMap _refs
           
private  java.util.HashMap _serializerMap
           
private  java.util.HashMap _typeRefs
           
private static int SIZE
           
 
Fields inherited from class com.caucho.hessian.io.AbstractHessianOutput
_serializerFactory
 
Fields inherited from interface com.caucho.hessian.io.Hessian2Constants
BYTES_DIRECT, BYTES_DIRECT_MAX, DOUBLE_BYTE, DOUBLE_FLOAT, DOUBLE_ONE, DOUBLE_SHORT, DOUBLE_ZERO, INT_BYTE_MAX, INT_BYTE_MIN, INT_BYTE_ZERO, INT_DIRECT_MAX, INT_DIRECT_MIN, INT_SHORT_MAX, INT_SHORT_MIN, INT_SHORT_ZERO, INT_ZERO, LENGTH_BYTE, LIST_FIXED, LONG_BYTE_MAX, LONG_BYTE_MIN, LONG_BYTE_ZERO, LONG_DIRECT_MAX, LONG_DIRECT_MIN, LONG_INT, LONG_SHORT_MAX, LONG_SHORT_MIN, LONG_SHORT_ZERO, LONG_ZERO, REF_BYTE, REF_SHORT, STRING_DIRECT, STRING_DIRECT_MAX, TYPE_REF
 
Constructor Summary
Hessian2Output(java.io.OutputStream os)
          Creates a new Hessian output stream, initialized with an underlying output stream.
 
Method Summary
 boolean addRef(java.lang.Object object)
          If the object has already been written, just write its ref.
 void call(java.lang.String method, java.lang.Object[] args)
          Writes a complete method call.
 void close()
           
 void completeCall()
          Completes.
 void completeEnvelope()
          Completes an envelope.
 void completeMessage()
          Completes reading the message
 void completeReply()
          Completes reading the reply
 void flush()
           
private  void flushIfFull()
           
 java.io.OutputStream getBytesOutputStream()
          Returns an output stream to write binary data.
 boolean isCloseStreamOnClose()
           
 void printLenString(java.lang.String v)
          Prints a string to the stream, encoded as UTF-8 with preceeding length
 void printString(char[] v, int strOffset, int length)
          Prints a string to the stream, encoded as UTF-8
 void printString(java.lang.String v)
          Prints a string to the stream, encoded as UTF-8
 void printString(java.lang.String v, int strOffset, int length)
          Prints a string to the stream, encoded as UTF-8
 boolean removeRef(java.lang.Object obj)
          Removes a reference.
 boolean replaceRef(java.lang.Object oldRef, java.lang.Object newRef)
          Replaces a reference from one object to another.
 void resetReferences()
          Resets the references for streaming.
 void setCloseStreamOnClose(boolean isClose)
           
 void startCall()
          Writes the call tag.
 void startCall(java.lang.String method)
          Starts the method call.
 void startEnvelope(java.lang.String method)
          Starts an envelope.
 void startMessage()
          Starts the message
 void startReply()
          Starts the reply
 void startStreamingCall()
          Writes the streaming call tag.
 void startStreamingReply()
          Starts the streaming reply
 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 writeByteBufferStart()
          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 writeClassFieldLength(int len)
          Writes the tail of the class definition to the stream.
 void writeDouble(double value)
          Writes a double value to the stream.
 void writeFault(java.lang.String code, java.lang.String message, java.lang.Object detail)
          Writes a fault.
 void writeHeader(java.lang.String name)
          Writes a header name.
 void writeInt(int value)
          Writes an integer value to the stream.
 boolean writeListBegin(int length, java.lang.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(java.lang.String type)
          Writes the map header to the stream.
 void writeMapEnd()
          Writes the tail of the map to the stream.
 void writeMethod(java.lang.String method)
          Writes the method tag.
 void writeNull()
          Writes a null value to the stream.
 void writeObject(java.lang.Object object)
          Writes any object to the output stream.
 int writeObjectBegin(java.lang.String type)
          Writes the object definition
 void writeObjectEnd()
          Writes the tail of the object definition to the stream.
 void writeRef(int value)
          Writes a reference.
 void writeRemote(java.lang.String type, java.lang.String url)
          Writes a remote object reference to the stream.
 void writeStreamingObject(java.lang.Object obj)
          Starts the streaming message
 void writeString(char[] buffer, int offset, int length)
          Writes a string value to the stream using UTF-8 encoding.
 void writeString(java.lang.String value)
          Writes a string value to the stream using UTF-8 encoding.
private  void writeType(java.lang.String type)
           
 void writeUTCDate(long time)
          Writes a date to the stream.
 
Methods inherited from class com.caucho.hessian.io.AbstractHessianOutput
findSerializerFactory, getSerializerFactory, init, setSerializerFactory
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_os

protected java.io.OutputStream _os

_refs

private IdentityIntMap _refs

_serializerMap

private java.util.HashMap _serializerMap

_isCloseStreamOnClose

private boolean _isCloseStreamOnClose

_classRefs

private java.util.HashMap _classRefs

_typeRefs

private java.util.HashMap _typeRefs

SIZE

private static final int SIZE
See Also:
Constant Field Values

_buffer

private final byte[] _buffer

_offset

private int _offset

_isStreaming

private boolean _isStreaming
Constructor Detail

Hessian2Output

public Hessian2Output(java.io.OutputStream os)
Creates a new Hessian output stream, initialized with an underlying output stream.

Parameters:
os - the underlying output stream.
Method Detail

setCloseStreamOnClose

public void setCloseStreamOnClose(boolean isClose)

isCloseStreamOnClose

public boolean isCloseStreamOnClose()

call

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

Overrides:
call in class AbstractHessianOutput
Throws:
java.io.IOException

startCall

public void startCall(java.lang.String method)
               throws java.io.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
 

Specified by:
startCall in class AbstractHessianOutput
Parameters:
method - the method name to call.
Throws:
java.io.IOException

startCall

public void startCall()
               throws java.io.IOException
Writes the call tag. This would be followed by the headers and the method tag.
 c major minor
 

Specified by:
startCall in class AbstractHessianOutput
Parameters:
method - the method name to call.
Throws:
java.io.IOException

startStreamingCall

public void startStreamingCall()
                        throws java.io.IOException
Writes the streaming call tag. This would be followed by the headers and the method tag.
 C major minor
 

Parameters:
method - the method name to call.
Throws:
java.io.IOException

startEnvelope

public void startEnvelope(java.lang.String method)
                   throws java.io.IOException
Starts an envelope.
 E major minor
 m b16 b8 method-name
 

Parameters:
method - the method name to call.
Throws:
java.io.IOException

completeEnvelope

public void completeEnvelope()
                      throws java.io.IOException
Completes an envelope.

A successful completion will have a single value:

 z
 

Throws:
java.io.IOException

writeMethod

public void writeMethod(java.lang.String method)
                 throws java.io.IOException
Writes the method tag.
 m b16 b8 method-name
 

Specified by:
writeMethod in class AbstractHessianOutput
Parameters:
method - the method name to call.
Throws:
java.io.IOException

completeCall

public void completeCall()
                  throws java.io.IOException
Completes.
 z
 

Specified by:
completeCall in class AbstractHessianOutput
Throws:
java.io.IOException

startReply

public void startReply()
                throws java.io.IOException
Starts the reply

A successful completion will have a single value:

 r
 

Overrides:
startReply in class AbstractHessianOutput
Throws:
java.io.IOException

startStreamingReply

public void startStreamingReply()
                         throws java.io.IOException
Starts the streaming reply

A successful completion will have a single value:

 r
 

Throws:
java.io.IOException

completeReply

public void completeReply()
                   throws java.io.IOException
Completes reading the reply

A successful completion will have a single value:

 z
 

Overrides:
completeReply in class AbstractHessianOutput
Throws:
java.io.IOException

startMessage

public void startMessage()
                  throws java.io.IOException
Starts the message

A message contains several objects followed by a 'z'

 p x02 x00
 

Throws:
java.io.IOException

completeMessage

public void completeMessage()
                     throws java.io.IOException
Completes reading the message

A successful completion will have a single value:

 z
 

Throws:
java.io.IOException

writeHeader

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

Specified by:
writeHeader in class AbstractHessianOutput
Throws:
java.io.IOException

writeFault

public void writeFault(java.lang.String code,
                       java.lang.String message,
                       java.lang.Object detail)
                throws java.io.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
 

Overrides:
writeFault in class AbstractHessianOutput
Parameters:
code - the fault code, a three digit
Throws:
java.io.IOException

writeObject

public void writeObject(java.lang.Object object)
                 throws java.io.IOException
Writes any object to the output stream.

Specified by:
writeObject in class AbstractHessianOutput
Throws:
java.io.IOException

writeListBegin

public boolean writeListBegin(int length,
                              java.lang.String type)
                       throws java.io.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
 

Specified by:
writeListBegin in class AbstractHessianOutput
Throws:
java.io.IOException

writeListEnd

public void writeListEnd()
                  throws java.io.IOException
Writes the tail of the list to the stream.

Specified by:
writeListEnd in class AbstractHessianOutput
Throws:
java.io.IOException

writeMapBegin

public void writeMapBegin(java.lang.String type)
                   throws java.io.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
 

Specified by:
writeMapBegin in class AbstractHessianOutput
Throws:
java.io.IOException

writeMapEnd

public void writeMapEnd()
                 throws java.io.IOException
Writes the tail of the map to the stream.

Specified by:
writeMapEnd in class AbstractHessianOutput
Throws:
java.io.IOException

writeObjectBegin

public int writeObjectBegin(java.lang.String type)
                     throws java.io.IOException
Writes the object definition
 O t b16 b8 *
 

Overrides:
writeObjectBegin in class AbstractHessianOutput
Returns:
true if the object has already been defined.
Throws:
java.io.IOException

writeClassFieldLength

public void writeClassFieldLength(int len)
                           throws java.io.IOException
Writes the tail of the class definition to the stream.

Overrides:
writeClassFieldLength in class AbstractHessianOutput
Throws:
java.io.IOException

writeObjectEnd

public void writeObjectEnd()
                    throws java.io.IOException
Writes the tail of the object definition to the stream.

Overrides:
writeObjectEnd in class AbstractHessianOutput
Throws:
java.io.IOException

writeRemote

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

Specified by:
writeRemote in class AbstractHessianOutput
Throws:
java.io.IOException

writeType

private void writeType(java.lang.String type)
                throws java.io.IOException
Throws:
java.io.IOException

writeBoolean

public void writeBoolean(boolean value)
                  throws java.io.IOException
Writes a boolean value to the stream. The boolean will be written with the following syntax:
 T
 F
 

Specified by:
writeBoolean in class AbstractHessianOutput
Parameters:
value - the boolean value to write.
Throws:
java.io.IOException

writeInt

public void writeInt(int value)
              throws java.io.IOException
Writes an integer value to the stream. The integer will be written with the following syntax:
 I b32 b24 b16 b8
 

Specified by:
writeInt in class AbstractHessianOutput
Parameters:
value - the integer value to write.
Throws:
java.io.IOException

writeLong

public void writeLong(long value)
               throws java.io.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
 

Specified by:
writeLong in class AbstractHessianOutput
Parameters:
value - the long value to write.
Throws:
java.io.IOException

writeDouble

public void writeDouble(double value)
                 throws java.io.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
 

Specified by:
writeDouble in class AbstractHessianOutput
Parameters:
value - the double value to write.
Throws:
java.io.IOException

writeUTCDate

public void writeUTCDate(long time)
                  throws java.io.IOException
Writes a date to the stream.
 T  b64 b56 b48 b40 b32 b24 b16 b8
 

Specified by:
writeUTCDate in class AbstractHessianOutput
Parameters:
time - the date in milliseconds from the epoch in UTC
Throws:
java.io.IOException

writeNull

public void writeNull()
               throws java.io.IOException
Writes a null value to the stream. The null will be written with the following syntax
 N
 

Specified by:
writeNull in class AbstractHessianOutput
Parameters:
value - the string value to write.
Throws:
java.io.IOException

writeString

public void writeString(java.lang.String value)
                 throws java.io.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
 

Specified by:
writeString in class AbstractHessianOutput
Parameters:
value - the string value to write.
Throws:
java.io.IOException

writeString

public void writeString(char[] buffer,
                        int offset,
                        int length)
                 throws java.io.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
 

Specified by:
writeString in class AbstractHessianOutput
Parameters:
value - the string value to write.
Throws:
java.io.IOException

writeBytes

public void writeBytes(byte[] buffer)
                throws java.io.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
 

Specified by:
writeBytes in class AbstractHessianOutput
Parameters:
value - the string value to write.
Throws:
java.io.IOException

writeBytes

public void writeBytes(byte[] buffer,
                       int offset,
                       int length)
                throws java.io.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
 

Specified by:
writeBytes in class AbstractHessianOutput
Parameters:
value - the string value to write.
Throws:
java.io.IOException

writeByteBufferStart

public void writeByteBufferStart()
                          throws java.io.IOException
Writes a byte buffer to the stream.
 

Specified by:
writeByteBufferStart in class AbstractHessianOutput
Throws:
java.io.IOException

writeByteBufferPart

public void writeByteBufferPart(byte[] buffer,
                                int offset,
                                int length)
                         throws java.io.IOException
Writes a byte buffer to the stream.
 b b16 b18 bytes
 

Specified by:
writeByteBufferPart in class AbstractHessianOutput
Throws:
java.io.IOException

writeByteBufferEnd

public void writeByteBufferEnd(byte[] buffer,
                               int offset,
                               int length)
                        throws java.io.IOException
Writes a byte buffer to the stream.
 b b16 b18 bytes
 

Specified by:
writeByteBufferEnd in class AbstractHessianOutput
Throws:
java.io.IOException

getBytesOutputStream

public java.io.OutputStream getBytesOutputStream()
                                          throws java.io.IOException
Returns an output stream to write binary data.

Throws:
java.io.IOException

writeRef

public void writeRef(int value)
              throws java.io.IOException
Writes a reference.
 R b32 b24 b16 b8
 

Specified by:
writeRef in class AbstractHessianOutput
Parameters:
value - the integer value to write.
Throws:
java.io.IOException

addRef

public boolean addRef(java.lang.Object object)
               throws java.io.IOException
If the object has already been written, just write its ref.

Specified by:
addRef in class AbstractHessianOutput
Parameters:
object - the object to add as a reference.
Returns:
true if we're writing a ref.
Throws:
java.io.IOException

removeRef

public boolean removeRef(java.lang.Object obj)
                  throws java.io.IOException
Removes a reference.

Specified by:
removeRef in class AbstractHessianOutput
Throws:
java.io.IOException

replaceRef

public boolean replaceRef(java.lang.Object oldRef,
                          java.lang.Object newRef)
                   throws java.io.IOException
Replaces a reference from one object to another.

Specified by:
replaceRef in class AbstractHessianOutput
Throws:
java.io.IOException

resetReferences

public void resetReferences()
Resets the references for streaming.

Overrides:
resetReferences in class AbstractHessianOutput

writeStreamingObject

public void writeStreamingObject(java.lang.Object obj)
                          throws java.io.IOException
Starts the streaming message

A streaming message starts with 'P'

 P x02 x00
 

Throws:
java.io.IOException

printLenString

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

Parameters:
v - the string to print.
Throws:
java.io.IOException

printString

public void printString(java.lang.String v)
                 throws java.io.IOException
Prints a string to the stream, encoded as UTF-8

Parameters:
v - the string to print.
Throws:
java.io.IOException

printString

public void printString(java.lang.String v,
                        int strOffset,
                        int length)
                 throws java.io.IOException
Prints a string to the stream, encoded as UTF-8

Parameters:
v - the string to print.
Throws:
java.io.IOException

printString

public void printString(char[] v,
                        int strOffset,
                        int length)
                 throws java.io.IOException
Prints a string to the stream, encoded as UTF-8

Parameters:
v - the string to print.
Throws:
java.io.IOException

flushIfFull

private final void flushIfFull()
                        throws java.io.IOException
Throws:
java.io.IOException

flush

public final void flush()
                 throws java.io.IOException
Overrides:
flush in class AbstractHessianOutput
Throws:
java.io.IOException

close

public final void close()
                 throws java.io.IOException
Overrides:
close in class AbstractHessianOutput
Throws:
java.io.IOException