Procedure for Calling Java BER Decode Methods

The general procedure to decode an ASN.1 BER message involves the following three steps:

  1. Create a decode message buffer object to describe the message to be decoded

  2. Invoke the decode method

  3. Process the decoded data values

The first step is the creation of a decode message buffer object. The Asn1BerDecodeBuffer object contains constructors that can either accept a message as a byte array or as an I/O input stream. The input stream option makes it possible to decode messages directly from other mediums other than a memory buffer (for example, a message can be decoded directly from a file).

The Asn1BerDecodeBuffer object contains a method called peekTag that can be used to determine the outer-level tag on a message. This can be used to determine the type of message received in applications that must deal with multiple message types.

The generated decode method can then be invoked to decode the message. The calling arguments were described earlier. As per the Java standard, this method must be invoked from within a try/catch block to catch the possible exceptions that may be thrown. Alternatively, the method from which the decode method is called can declare that it throws the exceptions leaving them to be dealt with at a higher level.

The final step is to process the data. All data is contained within public member variables so access is quite easy. And of course Java has the distinct advantage of not requiring any clean-up once you are done with the data. The garbage collector will collect the unused memory when it is no longer referenced.

A complete example showing how to invoke a decode method is as follows:

   try {

      // Step 1: create a decode message buffer object to describe the
      // message to be decoded. This example will use a file input
      // stream to decode a message directly from a binary file..

      // Create an input file stream object

      FileInputStream in = new FileInputStream (filename);

      // Create a decode buffer object

      Asn1BerDecodeBuffer decodeBuffer = new Asn1BerDecodeBuffer (in);

      // Step 2: create an object of the generated type and invoke the
      // decode method..

      PersonnelRecord personnelRecord = new PersonnelRecord ();
      personnelRecord.decode (decodeBuffer);

      // Step 3: process the data

      if (trace) {
         System.out.println ("Decode was successful");
         personnelRecord.print (System.out, "personnelRecord", 0);
      }
   }
   catch (Exception e) {
      System.out.println (e.getMessage());
      e.printStackTrace();
      return;
   }