com.caucho.burlap.io
Class BurlapOutput

java.lang.Object
  extended bycom.caucho.burlap.io.AbstractBurlapOutput
      extended bycom.caucho.burlap.io.BurlapOutput

public class BurlapOutput
extends AbstractBurlapOutput

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

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

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

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

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


Field Summary
private  java.util.Date date
           
private  java.util.Calendar localCalendar
           
protected  java.io.OutputStream os
           
private  java.util.ArrayList refs
           
protected  SerializerFactory serializerFactory
           
private  java.util.Calendar utcCalendar
           
 
Constructor Summary
BurlapOutput()
          Creates an uninitialized Burlap output stream.
BurlapOutput(java.io.OutputStream os)
          Creates a new Burlap 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 completeCall()
          Completes.
 void completeReply()
          Completes reading the reply
private  char encode(int d)
          Encodes a digit
 void init(java.io.OutputStream os)
          Initializes the output
protected  void print(char v)
          Prints a char to the stream.
protected  void print(double v)
          Prints a double to the stream.
protected  void print(int v)
          Prints an integer to the stream.
protected  void print(long v)
          Prints a long to the stream.
protected  void print(java.lang.String s)
          Prints a string as ascii to the stream.
 void printDate(java.util.Calendar calendar)
          Prints a date.
 void printString(char[] v, int offset, 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 offset, int length)
          Prints a string to the stream, encoded as UTF-8
 void startCall()
          Starts the method call.
 void startCall(java.lang.String method)
          Starts the method call.
 void startReply()
          Starts the reply
 void writeBoolean(boolean value)
          Writes a boolean value 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(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.
 void 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 for a call.
 void writeNull()
          Writes a null value to the stream.
 void writeObject(java.lang.Object object)
          Writes any object to the output 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 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.
 void writeUTCDate(long time)
          Writes a date to the stream.
 
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 java.util.ArrayList refs

serializerFactory

protected SerializerFactory serializerFactory

date

private java.util.Date date

utcCalendar

private java.util.Calendar utcCalendar

localCalendar

private java.util.Calendar localCalendar
Constructor Detail

BurlapOutput

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

Parameters:
os - the underlying output stream.

BurlapOutput

public BurlapOutput()
Creates an uninitialized Burlap output stream.

Method Detail

init

public void init(java.io.OutputStream os)
Initializes the output

Specified by:
init in class AbstractBurlapOutput

call

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

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.
 <burlap:call>
 <method>method-name</method>
 

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

startCall

public void startCall()
               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.
 <method>method-name</method>
 

Throws:
java.io.IOException

writeMethod

public void writeMethod(java.lang.String method)
                 throws java.io.IOException
Writes the method for a call.
 <method>value</method>
 

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

completeCall

public void completeCall()
                  throws java.io.IOException
Completes.
 </burlap:call>
 

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

startReply

public void startReply()
                throws java.io.IOException
Starts the 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:

 </burlap:reply>
 

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.
 <header>foo</header><int>value</int>
 

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:
 <fault>
 <string>code
 <string>the fault code

 <string>message
 <string>the fault mesage

 <string>detail
 <map>t\x00\xnnjavax.ejb.FinderException
     ...
 </map>
 </fault>
 

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 AbstractBurlapOutput
Throws:
java.io.IOException

writeListBegin

public void 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.
 <list>
   <type>java.util.ArrayList</type>
   <length>3</length>
   <int>1</int>
   <int>2</int>
   <int>3</int>
 </list>
 

Specified by:
writeListBegin in class AbstractBurlapOutput
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 AbstractBurlapOutput
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.
 <map>
   <type>type</type>
   (<key> <value>)*
 </map>
 

Specified by:
writeMapBegin in class AbstractBurlapOutput
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 AbstractBurlapOutput
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.
 <remote>
   <type>test.account.Account</type>
   <string>http://caucho.com/foo;ejbid=bar</string>
 </remote>
 

Specified by:
writeRemote in class AbstractBurlapOutput
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:
 <boolean>0</boolean>
 <boolean>1</boolean>
 

Specified by:
writeBoolean in class AbstractBurlapOutput
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:
 <int>int value</int>
 

Specified by:
writeInt in class AbstractBurlapOutput
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:
 <long>int value</long>
 

Specified by:
writeLong in class AbstractBurlapOutput
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:
 <double>value</double>
 

Specified by:
writeDouble in class AbstractBurlapOutput
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.
 <date>iso8901</date>
 

Specified by:
writeUTCDate in class AbstractBurlapOutput
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
 <null></null>
 

Specified by:
writeNull in class AbstractBurlapOutput
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:
 <string>string-value</string>
 
If the value is null, it will be written as
 <null></null>
 

Specified by:
writeString in class AbstractBurlapOutput
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 AbstractBurlapOutput
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:
 <base64>bytes</base64>
 
If the value is null, it will be written as
 <null></null>
 

Specified by:
writeBytes in class AbstractBurlapOutput
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:
 <base64>bytes</base64>
 
If the value is null, it will be written as
 <null></null>
 

Specified by:
writeBytes in class AbstractBurlapOutput
Throws:
java.io.IOException

encode

private char encode(int d)
Encodes a digit


writeRef

public void writeRef(int value)
              throws java.io.IOException
Writes a reference.
 <ref>int</ref>
 

Specified by:
writeRef in class AbstractBurlapOutput
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 AbstractBurlapOutput
Parameters:
object - the object to add as a reference.
Returns:
true if we're writing a ref.
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 offset,
                        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 offset,
                        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

printDate

public void printDate(java.util.Calendar calendar)
               throws java.io.IOException
Prints a date.

Throws:
java.io.IOException

print

protected void print(char v)
              throws java.io.IOException
Prints a char to the stream.

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

print

protected void print(int v)
              throws java.io.IOException
Prints an integer to the stream.

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

print

protected void print(long v)
              throws java.io.IOException
Prints a long to the stream.

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

print

protected void print(double v)
              throws java.io.IOException
Prints a double to the stream.

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

print

protected void print(java.lang.String s)
              throws java.io.IOException
Prints a string as ascii to the stream. Used for tags, etc. that are known to the ascii.

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