Base class for easy encoding and decoding of PDUs. When implementing protocols, we often need to assemble and parse PDUs. To ease this task, we have a PDU utility class to help. We first define a class with our PDU format:
public class MyPDU extends PDU {
void format() {
length(16); // 16 byte PDU
order(ByteOrder.BIG_ENDIAN); // byte ordering is big endian
uint8("type"); // 1 byte field "type"
uint8(0x01); // literal byte 0x01
filler(2); // 2 filler bytes
uint16("data"); // 2 byte field "data" as unsigned short
padding(0xff); // padded with 0xff to make 16 bytes
}
}
We can then encode and decode PDUs easily using this class:
Map<String,Object> tx = new HashMap<>();
tx.put("type", new Integer(7));
tx.put("data", new Integer(42));
byte[] bytes = pdu.encode(tx);
Map<String,Object> rx = pdu.decode(bytes);
In Groovy, the syntax can be simplified considerably:
def pdu = PDU.withFormat {
length(16) // 16 byte PDU
order(ByteOrder.BIG_ENDIAN) // byte ordering is big endian
uint8('type') // 1 byte field 'type'
uint8(0x01) // literal byte 0x01
filler(2) // 2 filler bytes
uint16('data') // 2 byte field 'data' as unsigned short
padding(0xff) // padded with 0xff to make 16 bytes
}
bytes = pdu.encode([type: 7, data: 42])
data = pdu.decode(bytes)
| Type Params | Return Type | Name and description |
|---|---|---|
|
void |
chars(java.lang.String fieldname, int n)Adds a string field to the PDU. |
|
void |
chars(java.lang.String s)Adds a string literal to the PDU. |
|
void |
charset(java.lang.String cset)Specifies the character encoding of text in the PDU. |
|
java.util.Map<java.lang.String, java.lang.Object> |
decode(byte[] pdu)Decodes a PDU into a map of fieldname to field value. |
|
byte[] |
encode(java.util.Map<java.lang.String, ?> fields)Encodes a PDU from a map of fieldname to field value. |
|
void |
filler(int len)Adds filler bytes to PDU. |
|
void |
format()Defines the format of the PDU. |
|
void |
int16(java.lang.String fieldname)Adds a signed 16-bit integer field to the PDU. |
|
void |
int16(int x)Adds a signed 16-bit integer literal to the PDU. |
|
void |
int32(java.lang.String fieldname)Adds a signed 32-bit integer field to the PDU. |
|
void |
int32(int x)Adds a signed 32-bit integer literal to the PDU. |
|
void |
int64(java.lang.String fieldname)Adds a signed 64-bit integer field to the PDU. |
|
void |
int64(long x)Adds a signed 64-bit integer literal to the PDU. |
|
void |
int8(java.lang.String fieldname)Adds a signed byte field to the PDU. |
|
void |
int8(int x)Adds a signed byte literal to the PDU. |
|
void |
length(int len)Specifies the length of the PDU. |
|
void |
order(java.nio.ByteOrder bo)Specifies the byte ordering of multi-byte numbers. |
|
void |
padding(int x)Adds padding bytes at the end of the PDU. |
|
void |
uint16(java.lang.String fieldname)Adds an unsigned 16-bit integer field to the PDU. |
|
void |
uint16(int x)Adds an unsigned 16-bit integer literal to the PDU. |
|
void |
uint32(java.lang.String fieldname)Adds an unsigned 32-bit integer field to the PDU. |
|
void |
uint32(long x)Adds an unsigned 32-bit integer literal to the PDU. |
|
void |
uint8(java.lang.String fieldname)Adds an unsigned byte field to the PDU. |
|
void |
uint8(int x)Adds an unsigned byte literal to the PDU. |
|
void |
varbytes(java.lang.String fieldname)Adds a variable length byte buffer using wire encoding to the PDU. |
|
void |
varchars(java.lang.String fieldname)Adds a variable length string using wire encoding to the PDU. |
|
static PDU |
withFormat(groovy.lang.Closure c)Groovy builder method. |
| Methods inherited from class | Name |
|---|---|
class java.lang.Object |
java.lang.Object#wait(long, int), java.lang.Object#wait(long), java.lang.Object#wait(), java.lang.Object#equals(java.lang.Object), java.lang.Object#toString(), java.lang.Object#hashCode(), java.lang.Object#getClass(), java.lang.Object#notify(), java.lang.Object#notifyAll() |
Adds a string field to the PDU.
fieldname - name of the field for encoding/decoding.n - number of bytes in the string.Adds a string literal to the PDU.
s - value of the string literal.Specifies the character encoding of text in the PDU.
cset - character set name.Decodes a PDU into a map of fieldname to field value.
pdu - byte array representing the PDU.Encodes a PDU from a map of fieldname to field value.
fields - map of fieldname to field value.Adds filler bytes to PDU. Filler bytes are encoded as 0x00.
len - number of filler bytes to add.Defines the format of the PDU. This method should be overridden by a derived class and used to specify the format of the PDU. Example:
void format() {
length(16); // 16 byte PDU
order(ByteOrder.BIG_ENDIAN); // byte ordering
uint8("type"); // 1 byte field "type"
uint8(0x01) // literal byte 0x01
filler(2) // 2 filler bytes
uint16("data"); // 2 byte field "data" as unsigned short
padding(0xff); // padded with 0xff to make 16 bytes
}
Adds a signed 16-bit integer field to the PDU.
fieldname - name of the field for encoding/decoding.Adds a signed 16-bit integer literal to the PDU.
x - value of the literal.Adds a signed 32-bit integer field to the PDU.
fieldname - name of the field for encoding/decoding.Adds a signed 32-bit integer literal to the PDU.
x - value of the literal.Adds a signed 64-bit integer field to the PDU.
fieldname - name of the field for encoding/decoding.Adds a signed 64-bit integer literal to the PDU.
x - value of the literal.Adds a signed byte field to the PDU.
fieldname - name of the field for encoding/decoding.Adds a signed byte literal to the PDU.
x - value of the literal.Specifies the length of the PDU.
len - length of the PDU.Specifies the byte ordering of multi-byte numbers.
bo - byte ordering.Adds padding bytes at the end of the PDU. The number of padding bytes is automatically determined based on the specified length of the PDU.
x - value to use for padding.Adds an unsigned 16-bit integer field to the PDU.
fieldname - name of the field for encoding/decoding.Adds an unsigned 16-bit integer literal to the PDU.
x - value of the literal.Adds an unsigned 32-bit integer field to the PDU.
fieldname - name of the field for encoding/decoding.Adds an unsigned 32-bit integer literal to the PDU.
x - value of the literal.Adds an unsigned byte field to the PDU.
fieldname - name of the field for encoding/decoding.Adds an unsigned byte literal to the PDU.
x - value of the literal.Adds a variable length byte buffer using wire encoding to the PDU.
fieldname - name of the field for encoding/decoding.Adds a variable length string using wire encoding to the PDU.
fieldname - name of the field for encoding/decoding.Groovy builder method.