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 |
|---|---|---|
|
protected void |
chars(String fieldname, int n)Adds a string field to the PDU. |
|
protected void |
chars(String s)Adds a string literal to the PDU. |
|
protected void |
charset(String cset)Specifies the character encoding of text in the PDU. |
|
Map<String, Object> |
decode(byte[] pdu)Decodes a PDU into a map of fieldname to field value. |
|
byte[] |
encode(Map<String, Object> fields)Encodes a PDU from a map of fieldname to field value. |
|
protected void |
filler(int len)Adds filler bytes to PDU. |
|
protected void |
format()Defines the format of the PDU. |
|
protected void |
int16(String fieldname)Adds a signed 16-bit integer field to the PDU. |
|
protected void |
int16(int x)Adds a signed 16-bit integer literal to the PDU. |
|
protected void |
int32(String fieldname)Adds a signed 32-bit integer field to the PDU. |
|
protected void |
int32(int x)Adds a signed 32-bit integer literal to the PDU. |
|
protected void |
int64(String fieldname)Adds a signed 64-bit integer field to the PDU. |
|
protected void |
int64(long x)Adds a signed 64-bit integer literal to the PDU. |
|
protected void |
int8(String fieldname)Adds a signed byte field to the PDU. |
|
protected void |
int8(int x)Adds a signed byte literal to the PDU. |
|
protected void |
length(int len)Specifies the length of the PDU. |
|
protected void |
order(ByteOrder bo)Specifies the byte ordering of multi-byte numbers. |
|
protected void |
padding(int x)Adds padding bytes at the end of the PDU. |
|
protected void |
uint16(String fieldname)Adds an unsigned 16-bit integer field to the PDU. |
|
protected void |
uint16(int x)Adds an unsigned 16-bit integer literal to the PDU. |
|
protected void |
uint32(String fieldname)Adds an unsigned 32-bit integer field to the PDU. |
|
protected void |
uint32(long x)Adds an unsigned 32-bit integer literal to the PDU. |
|
protected void |
uint8(String fieldname)Adds an unsigned byte field to the PDU. |
|
protected void |
uint8(int x)Adds an unsigned byte literal to the PDU. |
|
protected void |
varbytes(String fieldname)Adds a variable length byte byffer using wire encoding to the PDU. |
|
protected void |
varchars(String fieldname)Adds a variable length string using wire encoding to the PDU. |
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 byffer 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.