org.apache.hadoop.oncrpc
Class XDR

java.lang.Object
  extended by org.apache.hadoop.oncrpc.XDR

public class XDR
extends Object

Utility class for building XDR messages based on RFC 4506.

This class maintains a buffer into which java types are written as XDR types for building XDR messages. Similarly this class can be used to get java types from an XDR request or response.

Currently only a subset of XDR types defined in RFC 4506 are supported.


Constructor Summary
XDR()
           
XDR(byte[] data)
           
 
Method Summary
static byte[] append(byte[] bytes, byte[] bytesToAdd)
           
 void dump(PrintStream out)
           
static int fragmentSize(byte[] mark)
           
 byte[] getBytes()
           
static byte[] getVariableOpque(byte[] data)
           
static boolean isLastFragment(byte[] mark)
           
 boolean readBoolean()
          Read an XDR boolean and return as Java primitive boolean.
 byte[] readFixedOpaque(int size)
           
 long readHyper()
          Read XDR signed hyper and return as java primitive long.
 int readInt()
          Read an XDR signed integer and return as Java primitive integer.
 String readString()
           
 byte[] readVariableOpaque()
           
 int size()
           
 void skip(int size)
          Skip some bytes by moving the cursor
 void skipVariableOpaque()
           
static boolean verifyLength(XDR xdr, int len)
          check if the rest of data has more than bytes
 void writeBoolean(boolean data)
          Write Java primitive boolean as an XDR boolean.
 void writeFixedOpaque(byte[] data)
          Write a Java primitive byte array to XDR fixed-length opaque data.
 void writeFixedOpaque(byte[] data, int length)
           
 void writeInt(int data)
          Write Java primitive integer as XDR signed integer.
 void writeLongAsHyper(long data)
          Write Java primitive long to an XDR signed long.
static org.jboss.netty.buffer.ChannelBuffer writeMessageTcp(XDR request, boolean last)
          Write an XDR message to a TCP ChannelBuffer
static org.jboss.netty.buffer.ChannelBuffer writeMessageUdp(XDR response)
          Write an XDR message to a UDP ChannelBuffer
 void writeString(String data)
          Write Java String as XDR string.
 void writeVariableOpaque(byte[] data)
          Write a Java primitive byte array as XDR variable-length opque data.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XDR

public XDR()

XDR

public XDR(byte[] data)
Method Detail

size

public int size()

skip

public void skip(int size)
Skip some bytes by moving the cursor


writeInt

public void writeInt(int data)
Write Java primitive integer as XDR signed integer. Definition of XDR signed integer from RFC 4506:
 An XDR signed integer is a 32-bit datum that encodes an integer in
 the range [-2147483648,2147483647].  The integer is represented in
 two's complement notation.  The most and least significant bytes are
 0 and 3, respectively.  Integers are declared as follows:
 
       int identifier;
 
            (MSB)                   (LSB)
          +-------+-------+-------+-------+
          |byte 0 |byte 1 |byte 2 |byte 3 |                      INTEGER
          +-------+-------+-------+-------+
          <------------32 bits------------>
 


readInt

public int readInt()
Read an XDR signed integer and return as Java primitive integer.


writeBoolean

public void writeBoolean(boolean data)
Write Java primitive boolean as an XDR boolean. Definition of XDR boolean from RFC 4506:
    Booleans are important enough and occur frequently enough to warrant
    their own explicit type in the standard.  Booleans are declared as
    follows:
 
          bool identifier;
 
    This is equivalent to:
 
          enum { FALSE = 0, TRUE = 1 } identifier;
 


readBoolean

public boolean readBoolean()
Read an XDR boolean and return as Java primitive boolean.


writeLongAsHyper

public void writeLongAsHyper(long data)
Write Java primitive long to an XDR signed long. Definition of XDR signed long from RFC 4506:
    The standard also defines 64-bit (8-byte) numbers called hyper
    integers and unsigned hyper integers.  Their representations are the
    obvious extensions of integer and unsigned integer defined above.
    They are represented in two's complement notation.The most and
    least significant bytes are 0 and 7, respectively. Their
    declarations:
 
    hyper identifier; unsigned hyper identifier;
 
         (MSB)                                                   (LSB)
       +-------+-------+-------+-------+-------+-------+-------+-------+
       |byte 0 |byte 1 |byte 2 |byte 3 |byte 4 |byte 5 |byte 6 |byte 7 |
       +-------+-------+-------+-------+-------+-------+-------+-------+
       <----------------------------64 bits---------------------------->
                                                  HYPER INTEGER
                                                  UNSIGNED HYPER INTEGER
 


readHyper

public long readHyper()
Read XDR signed hyper and return as java primitive long.


writeFixedOpaque

public void writeFixedOpaque(byte[] data)
Write a Java primitive byte array to XDR fixed-length opaque data. Defintion of fixed-length opaque data from RFC 4506:
    At times, fixed-length uninterpreted data needs to be passed among
    machines.  This data is called "opaque" and is declared as follows:
 
          opaque identifier[n];
 
    where the constant n is the (static) number of bytes necessary to
    contain the opaque data.  If n is not a multiple of four, then the n
    bytes are followed by enough (0 to 3) residual zero bytes, r, to make
    the total byte count of the opaque object a multiple of four.
 
           0        1     ...
       +--------+--------+...+--------+--------+...+--------+
       | byte 0 | byte 1 |...|byte n-1|    0   |...|    0   |
       +--------+--------+...+--------+--------+...+--------+
       |<-----------n bytes---------->|<------r bytes------>|
       |<-----------n+r (where (n+r) mod 4 = 0)------------>|
                                                    FIXED-LENGTH OPAQUE
 


writeFixedOpaque

public void writeFixedOpaque(byte[] data,
                             int length)

readFixedOpaque

public byte[] readFixedOpaque(int size)

writeVariableOpaque

public void writeVariableOpaque(byte[] data)
Write a Java primitive byte array as XDR variable-length opque data. Definition of XDR variable-length opaque data RFC 4506:
    The standard also provides for variable-length (counted) opaque data,
    defined as a sequence of n (numbered 0 through n-1) arbitrary bytes
    to be the number n encoded as an unsigned integer (as described
    below), and followed by the n bytes of the sequence.
 
    Byte m of the sequence always precedes byte m+1 of the sequence, and
    byte 0 of the sequence always follows the sequence's length (count).
    If n is not a multiple of four, then the n bytes are followed by
    enough (0 to 3) residual zero bytes, r, to make the total byte count
    a multiple of four.  Variable-length opaque data is declared in the
    following way:
 
          opaque identifier;
       or
          opaque identifier<>;
 
    The constant m denotes an upper bound of the number of bytes that the
    sequence may contain.  If m is not specified, as in the second
    declaration, it is assumed to be (2**32) - 1, the maximum length.
 
    The constant m would normally be found in a protocol specification.
    For example, a filing protocol may state that the maximum data
    transfer size is 8192 bytes, as follows:
 
          opaque filedata<8192>;
 
             0     1     2     3     4     5   ...
          +-----+-----+-----+-----+-----+-----+...+-----+-----+...+-----+
          |        length n       |byte0|byte1|...| n-1 |  0  |...|  0  |
          +-----+-----+-----+-----+-----+-----+...+-----+-----+...+-----+
          |<-------4 bytes------->|<------n bytes------>|<---r bytes--->|
                                  |<----n+r (where (n+r) mod 4 = 0)---->|
                                                   VARIABLE-LENGTH OPAQUE
 
    It is an error to encode a length greater than the maximum described
    in the specification.
 


readVariableOpaque

public byte[] readVariableOpaque()

skipVariableOpaque

public void skipVariableOpaque()

writeString

public void writeString(String data)
Write Java String as XDR string. Definition of XDR string from RFC 4506:
    The standard defines a string of n (numbered 0 through n-1) ASCII
    bytes to be the number n encoded as an unsigned integer (as described
    above), and followed by the n bytes of the string.  Byte m of the
    string always precedes byte m+1 of the string, and byte 0 of the
    string always follows the string's length.  If n is not a multiple of
    four, then the n bytes are followed by enough (0 to 3) residual zero
    bytes, r, to make the total byte count a multiple of four.  Counted
    byte strings are declared as follows:
 
          string object;
       or
          string object<>;
 
    The constant m denotes an upper bound of the number of bytes that a
    string may contain.  If m is not specified, as in the second
    declaration, it is assumed to be (2**32) - 1, the maximum length.
    The constant m would normally be found in a protocol specification.
    For example, a filing protocol may state that a file name can be no
    longer than 255 bytes, as follows:
 
          string filename<255>;
 
             0     1     2     3     4     5   ...
          +-----+-----+-----+-----+-----+-----+...+-----+-----+...+-----+
          |        length n       |byte0|byte1|...| n-1 |  0  |...|  0  |
          +-----+-----+-----+-----+-----+-----+...+-----+-----+...+-----+
          |<-------4 bytes------->|<------n bytes------>|<---r bytes--->|
                                  |<----n+r (where (n+r) mod 4 = 0)---->|
                                                                   STRING
    It is an error to encode a length greater than the maximum described
    in the specification.
 


readString

public String readString()

dump

public void dump(PrintStream out)

getBytes

public byte[] getBytes()

append

public static byte[] append(byte[] bytes,
                            byte[] bytesToAdd)

getVariableOpque

public static byte[] getVariableOpque(byte[] data)

fragmentSize

public static int fragmentSize(byte[] mark)

isLastFragment

public static boolean isLastFragment(byte[] mark)

verifyLength

public static boolean verifyLength(XDR xdr,
                                   int len)
check if the rest of data has more than bytes


writeMessageTcp

public static org.jboss.netty.buffer.ChannelBuffer writeMessageTcp(XDR request,
                                                                   boolean last)
Write an XDR message to a TCP ChannelBuffer


writeMessageUdp

public static org.jboss.netty.buffer.ChannelBuffer writeMessageUdp(XDR response)
Write an XDR message to a UDP ChannelBuffer



Copyright © 2009 The Apache Software Foundation