com.distinct.rpc
Class XDRStream

java.lang.Object
  |
  +--com.distinct.rpc.XDRStream
Direct Known Subclasses:
XDRInputStream, XDROutputStream

public class XDRStream
extends java.lang.Object

XDRStream implements a dynamically growing buffer of bytes (in fact a queue). The constructors allow for creating an empty XDRStream (with default allocation size 1024), an empty XDRStream with user defined allocation size (just a possible optimization), or an XDRStream initialized with the content of an existing byte-array (usually the start of the decoding procedure). The put_byte(), put_bytes() and all xdr_encode_xxx() methods add bytes to the head of the queue, while the get_byte(), get_bytes() and all xdr_decode_xxx() methods consume bytes from the tail. With get_length() and get_data() you can request the current length and content of the queue without changing its status. The reset() method resets the XDRStream to an empty queue. dump() just prints the content of the queue in hex and ASCII to System.out (typically used for debugging).

All xdr_decode and xdr_encode methods for the basic types (and therefore also the methods created by Jrpcgen for constructed XDR types) finally use the put_byte/s and get_byte/s methods for writing to and reading from the buffer. So you can derive your own XDRStream class and overwrite just these methods for building an XDRStream that reads and/or writes from/to any data source you like (like XDRInputStream which reads from a standard Java InputStream (writing is undefined in this class)).

See Also:
XDRInputStream

Field Summary
protected  int alloc_size
           
protected  byte[] buffer
           
protected  int in
           
protected  int out
           
 
Constructor Summary
XDRStream()
          Creates a new XDRStream with default allocation size.
XDRStream(byte[] b, int length)
          Creates a new XDRStream initialized from a byte array.
XDRStream(int alloc)
          Creates a new XDRStream with a given allocation size.
 
Method Summary
 void dump()
          Writes the contents of the XDRStream to System.out.
 byte get_byte()
          Reads one byte from the XDRStream.
 byte[] get_bytes(int n)
          Reads a number of bytes from the XDRStream.
 byte[] get_data()
          Returns all bytes available in the XDRStream.
 int get_length()
          Reports the number of bytes available in the XDRStream.
 void put_byte(byte b)
          Writes one byte to the XDRStream.
 void put_bytes(byte[] b, int n)
          Writes a number of bytes to the XDRStream.
 void reset()
          Clears the XDRStream.
 boolean xdr_decode_boolean()
          Decodes a boolean in compliance to RFC 1832 (XDR).
 char xdr_decode_char()
          Decodes a character in compliance to RFC 1832 (XDR).
 double xdr_decode_double()
          Decodes a double in compliance to RFC 1832 (XDR).
 float xdr_decode_float()
          Decodes a float in compliance to RFC 1832 (XDR).
 int xdr_decode_int()
          Decodes an integer in compliance to RFC 1832 (XDR).
 long xdr_decode_long()
          Decodes a long in compliance to RFC 1832 (XDR).
 byte[] xdr_decode_opaque()
          Decodes an opaque byte array in compliance to RFC 1832 (XDR).
 byte[] xdr_decode_opaque(int n)
          Decodes an opaque byte array in compliance to RFC 1832 (XDR).
 short xdr_decode_short()
          Decodes a short in compliance to RFC 1832 (XDR).
 java.lang.String xdr_decode_string()
          Decodes a string in compliance to RFC 1832 (XDR).
 java.lang.String xdr_decode_string(int n)
          Decodes a string in compliance to RFC 1832 (XDR).
 void xdr_encode_boolean(boolean x)
          Encodes a boolean in compliance to RFC 1832 (XDR).
 void xdr_encode_char(char x)
          Encodes a character in compliance to RFC 1832 (XDR).
 void xdr_encode_double(double data)
          Encodes a double in compliance to RFC 1832 (XDR).
 void xdr_encode_float(float data)
          Encodes a float in compliance to RFC 1832 (XDR).
 void xdr_encode_int(int x)
          Encodes an integer in compliance to RFC 1832 (XDR).
 void xdr_encode_long(long x)
          Encodes a long in compliance to RFC 1832 (XDR).
 void xdr_encode_opaque(byte[] data)
          Encodes an opaque array in compliance to RFC 1832 (XDR).
 void xdr_encode_opaque(byte[] data, int n)
          Encodes an opaque array in compliance to RFC 1832 (XDR).
 void xdr_encode_short(short x)
          Encodes a short in compliance to RFC 1832 (XDR).
 void xdr_encode_string(java.lang.String s)
          Encodes a string in compliance to RFC 1832 (XDR).
 void xdr_encode_string(java.lang.String s, int n)
          Encodes a string in compliance to RFC 1832 (XDR).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

buffer

protected byte[] buffer

alloc_size

protected int alloc_size

in

protected int in

out

protected int out
Constructor Detail

XDRStream

public XDRStream()
Creates a new XDRStream with default allocation size.


XDRStream

public XDRStream(byte[] b,
                 int length)
Creates a new XDRStream initialized from a byte array.

Parameters:
b - The byte array with the initialization data.
length - The length of the byte array.

XDRStream

public XDRStream(int alloc)
Creates a new XDRStream with a given allocation size.

Parameters:
alloc - The allocation size of the XDRStream.
Method Detail

put_byte

public void put_byte(byte b)
Writes one byte to the XDRStream. Used by the "encode" methods.

Parameters:
b - The byte to be written.

put_bytes

public void put_bytes(byte[] b,
                      int n)
Writes a number of bytes to the XDRStream. Used by the "decode" methods.

Parameters:
b - The bytes to be written.
n - The number of bytes to be written.

get_byte

public byte get_byte()
              throws RPCError
Reads one byte from the XDRStream. Used by the "decode" methods.

Returns:
Byte read.
Throws:
RPCError - When the calls fails because of timeout, empty stream or other input error.

get_bytes

public byte[] get_bytes(int n)
                 throws RPCError
Reads a number of bytes from the XDRStream. Used by the "decode" methods.

Parameters:
n - The number of bytes to be read.
Returns:
Bytes read.
Throws:
RPCError - When the calls fails because of timeout, empty stream or other input error.

get_length

public int get_length()
Reports the number of bytes available in the XDRStream.

Returns:
Number of bytes available.

get_data

public byte[] get_data()
Returns all bytes available in the XDRStream.

Returns:
Bytes available.

reset

public void reset()
Clears the XDRStream.


dump

public void dump()
Writes the contents of the XDRStream to System.out.


xdr_encode_int

public void xdr_encode_int(int x)
Encodes an integer in compliance to RFC 1832 (XDR).

Parameters:
x - The integer to be encoded.

xdr_decode_int

public int xdr_decode_int()
                   throws RPCError
Decodes an integer in compliance to RFC 1832 (XDR).

Returns:
The decoded integer.
Throws:
RPCError - When the calls fails for any reason.

xdr_encode_long

public void xdr_encode_long(long x)
Encodes a long in compliance to RFC 1832 (XDR).

Parameters:
x - The long to be encoded.

xdr_decode_long

public long xdr_decode_long()
                     throws RPCError
Decodes a long in compliance to RFC 1832 (XDR).

Returns:
The decoded long.
Throws:
RPCError - When the calls fails for any reason.

xdr_encode_short

public void xdr_encode_short(short x)
Encodes a short in compliance to RFC 1832 (XDR).

Parameters:
x - The short to be encoded.

xdr_decode_short

public short xdr_decode_short()
                       throws RPCError
Decodes a short in compliance to RFC 1832 (XDR).

Returns:
The decoded short.
Throws:
RPCError - When the calls fails for any reason.

xdr_encode_char

public void xdr_encode_char(char x)
Encodes a character in compliance to RFC 1832 (XDR).

Parameters:
x - The character to be encoded.

xdr_decode_char

public char xdr_decode_char()
                     throws RPCError
Decodes a character in compliance to RFC 1832 (XDR).

Returns:
The decoded character.
Throws:
RPCError - When the calls fails for any reason.

xdr_encode_boolean

public void xdr_encode_boolean(boolean x)
Encodes a boolean in compliance to RFC 1832 (XDR).

Parameters:
x - The boolean to be encoded.

xdr_decode_boolean

public boolean xdr_decode_boolean()
                           throws RPCError
Decodes a boolean in compliance to RFC 1832 (XDR).

Returns:
The decoded boolean.
Throws:
RPCError - When the calls fails for any reason.

xdr_encode_opaque

public void xdr_encode_opaque(byte[] data,
                              int n)
Encodes an opaque array in compliance to RFC 1832 (XDR).

Parameters:
data - The array of bytes to be encoded.
n - The length of the array.

xdr_decode_opaque

public byte[] xdr_decode_opaque(int n)
                         throws RPCError
Decodes an opaque byte array in compliance to RFC 1832 (XDR).

Parameters:
n - The length of the opaque byte array.
Returns:
The decoded opaque byte array.
Throws:
RPCError - When the calls fails for any reason.

xdr_encode_opaque

public void xdr_encode_opaque(byte[] data)
Encodes an opaque array in compliance to RFC 1832 (XDR).

Parameters:
data - The array of bytes to be encoded.

xdr_decode_opaque

public byte[] xdr_decode_opaque()
                         throws RPCError
Decodes an opaque byte array in compliance to RFC 1832 (XDR).

Returns:
The decoded opaque byte array.
Throws:
RPCError - When the calls fails for any reason.

xdr_encode_float

public void xdr_encode_float(float data)
Encodes a float in compliance to RFC 1832 (XDR).

Parameters:
data - The float to be encoded.

xdr_decode_float

public float xdr_decode_float()
                       throws RPCError
Decodes a float in compliance to RFC 1832 (XDR).

Returns:
The decoded float.
Throws:
RPCError - When the calls fails for any reason.

xdr_encode_double

public void xdr_encode_double(double data)
Encodes a double in compliance to RFC 1832 (XDR).

Parameters:
data - The double to be encoded.

xdr_decode_double

public double xdr_decode_double()
                         throws RPCError
Decodes a double in compliance to RFC 1832 (XDR).

Returns:
The decoded double.
Throws:
RPCError - When the calls fails for any reason.

xdr_encode_string

public void xdr_encode_string(java.lang.String s,
                              int n)
Encodes a string in compliance to RFC 1832 (XDR).

Parameters:
s - The string of bytes to be encoded.
n - The length of the string.

xdr_decode_string

public java.lang.String xdr_decode_string(int n)
                                   throws RPCError
Decodes a string in compliance to RFC 1832 (XDR).

Parameters:
n - The length of the string.
Returns:
The decoded string.
Throws:
RPCError - When the calls fails for any reason.

xdr_encode_string

public void xdr_encode_string(java.lang.String s)
Encodes a string in compliance to RFC 1832 (XDR).

Parameters:
s - The string to be encoded.

xdr_decode_string

public java.lang.String xdr_decode_string()
                                   throws RPCError
Decodes a string in compliance to RFC 1832 (XDR).

Returns:
The decoded string.
Throws:
RPCError - When the calls fails for any reason.