Procedure for Calling Java OER Encode Methods

The Java class variables corresponding to each of the ASN.1 types and method of population are the same as they were in the BER encoding case. See the section on BER encoding for instructions on how to populate the variables prior to encoding.

Once an object's member variables have been populated, the object's encode method can be invoked to encode the value. The general procedure to do this involves the following three steps:

  1. Create an encode message buffer object into which the value will be encoded

  2. Invoke the encode method

  3. Invoke encode message buffer methods to access the encoded message component

The first step is the creation of an encode message buffer object. For OER encoding, this is an object of the Asn1OerEncodeBuffer class. The following constructors are available for creating an OER encode buffer object:

   public Asn1OerEncodeBuffer ();

   public Asn1OerEncodeBuffer (int size);

The second form of the constructor includes a size argument for the initial size of the buffer. The default constructor sets the value to a reasonable mid-range value (see INITIAL_SIZE in Asn1EncodeBuffer.java, as of this writing the value was set to 1024).

The second step is the invocation of the encode method, described earlier. As the method throws java.io.IOException, your code must either handle that exception in a try/catch block or else declare that it throws it.

Finally, encode buffer methods can be called to access the encoded message component. The Java API provides an object called a ByteArrayInputStream that provides a way to look at the encoded component as a stream. The encode buffer object provides a method called getInputStream that returns a byte array input stream representing the message component. This is the preferred way to access the encoded component.

In addition to getInputStream there is a getMsgCopy function that will retrieve a copy of the generated message into a byte array object. This is somewhat slower because a copy needs to be done. Another option is the getBuffer method. This returns a reference to the actual message buffer into which the message was encoded. The getMsgByteCnt method can then be used to get the message length in bytes or the getMsgBitCnt method can be called to get the length in bits.

The encode buffer class also contains other methods for operating directly on the encoded component (for example, the write method can be used to write it to a file or other medium). And of course, one could derive their own special encode buffer class from this class to add more functionality. See the description of the Asn1OerEncodeBuffer class in the runtime section for a full description of the available methods.

A complete example showing how to invoke an OER encode method is as follows:

   // Note: personnelRecord object was previously populated with data

   // Step 1: Create a message buffer object. This object uses the
   // default size.

   Asn1OerEncodeBuffer encodeBuffer = new Asn1OerEncodeBuffer();

   // Step 2: Invoke the encode method. Note that it must be done
   // from within a try/catch block..

   try {
       personnelRecord.encode (encodeBuffer);

       if (trace) {
           System.out.println ("Encoding was successful");
           System.out.println ("Hex dump of encoded record:");
           encodeBuffer.hexDump ();
       }

       // Step 3: Access the encoded message component. In this
       // case, we use methods in the class to write the component
       // to a file and output a formatted dump to the message.dmp
       // file.

       // Write the encoded record to a file

       encodeBuffer.write (new FileOutputStream (filename));

       // Generate a dump file for comparisons

       encodeBuffer.hexDump
          (new PrintStream (new FileOutputStream ("message.dmp")));

       // We can also directly access the buffer as follows:

       byte[] buffer = encodeBuffer.getBuffer();
       int msglen = encodeBuffer.getMsgByteCnt();
   }
   catch (Exception e) {
       System.out.println (e.getMessage());
       e.printStackTrace();
       return;
   }

If you compare this example with the BER encoding example in Figure 2, you will see the encoding procedure is almost identical. This makes it very easy to switch encoding methods should the need arise. All you need to do is change the buffer class and slightly alter the invocation of the encode method.