There are two kind of message encoding, depend on whether stream or buffer oriented:
This kind of message encoding is pretty simple.
The following is an example of encoding message GetRequest
in our MyHTTP example into output stream:
GetRequest getRequest = ... OutputStream out = ... getRequest.ber_encode(out);
This assume that the generation of **_encode()/***_decode() methods is turn on. When the generation of **_encode()/***_decode() methods is turn off, one can use the generic encode()/decode() methods from the ASN.1 Java Runtime Library as illustrated here:
GetRequest getRequest = ... OutputStream out = ... GetRequest.TYPE.encode(getRequest, EncodingRules.BASIC_ENCODING_RULES, GetRequest.CONVERTER, out);
To encode message using buffer, a buffer must be created first.
The buffer can be created using Buffer's factory method allocate(int length, byte encodingRules)
:
Buffer buffer = Buffer.allocate(BufferOptions.BUF_SIZE, EncodingRules.BASIC_ENCODING_RULES).autoExpand(BufferOptions.BUF_INC);invoking autoExpand() method to make this buffer expandable. Then invoke the generic encode method
encode(Object object, Buffer buffer, AsnConverter converter)
:
GetRequest.TYPE.encode(getRequest, buffer, GetRequest.CONVERTER);The encoding result can be retrieved by:
byte[] encoding = buffer.array();