There are also two kind of message decoding, depend on whether stream or buffer oriented:
It's quit simple to decode message using stream.
The following is an example of decoding message GetRequest
in our MyHTTP example from input stream:
InputSteam in = ... GetRequest getRequest = GetRequest.ber_decode(in);
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:
InputSteam in = ... GetRequest getRequest = GetRequest.TYPE.decode(in, EncodingRules.BASIC_ENCODING_RULES, GetRequest.CONVERTER);
To decode message using buffer, a buffer must be created first.
The buffer can be created using Buffer's factory method wrap(byte[] array, byte encodingRules)
:
byte[] array = ... Buffer buffer = Buffer.wrap(array, EncodingRules.BASIC_ENCODING_RULES);and then invoke the generic encode method
decode(Buffer buffer, AsnConverter converter)
:
GetRequest getRequest = (GetRequest) GetRequest.TYPE.decode(buffer, GetRequest.CONVERTER);
package MyHTTP; import java.io.ByteArrayInputStream; import java.io.IOException; public class Test { public static void main(String[] args) throws IOException { byte bytes[] = { (byte) 0x30, (byte) 0x2D, (byte) 0x80, (byte) 0x01, (byte) 0xFF, (byte) 0x81, (byte) 0x01, (byte) 0x00, (byte) 0xA2, (byte) 0x04, (byte) 0x80, (byte) 0x02, (byte) 0x04, (byte) 0xC0, (byte) 0x83, (byte) 0x0E, (byte) 0x77, (byte) 0x77, (byte) 0x77, (byte) 0x2E, (byte) 0x61, (byte) 0x73, (byte) 0x6E, (byte) 0x6C, (byte) 0x61, (byte) 0x62, (byte) 0x2E, (byte) 0x63, (byte) 0x6F, (byte) 0x6D, (byte) 0x84, (byte) 0x0F, (byte) 0x32, (byte) 0x30, (byte) 0x31, (byte) 0x30, (byte) 0x30, (byte) 0x32, (byte) 0x30, (byte) 0x32, (byte) 0x31, (byte) 0x34, (byte) 0x30, (byte) 0x32, (byte) 0x30, (byte) 0x32, (byte) 0x5A }; ByteArrayInputStream in = new ByteArrayInputStream(bytes); GetRequest getRequest = GetRequest.ber_decode(in); getRequest.print(System.out); } }