Procedure for Calling C# OER Encode Methods

The C# 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, as of this writing the value was set to 1024).

The second step is the invocation of the encode method, described earlier.

Finally, encode buffer methods can be called to access the encoded message component. The C# 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 MsgCopy property 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 Buffer property. This returns a reference to the actual message buffer into which the message was encoded. The MsgByteCnt property can then be used to get the message length in bytes or the MsgBitCnt property 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.Console.Out.WriteLine ("Encoding was successful");
         System.Console.Out.WriteLine ("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 System.IO.FileStream(
         filename, System.IO.FileMode.Create));

      // Generate a dump file for comparisons
      System.IO.StreamWriter messagedmp =
         new System.IO.StreamWriter( new System.IO.FileStream(
            "message.dmp", System.IO.FileMode.Create));
      messagedmp.AutoFlush = true;
      encodeBuffer.HexDump(messagedmp);

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

      byte[] buffer = encodeBuffer.Buffer;
      int msglen = encodeBuffer.MsgByteCnt;
   }
   catch (Exception e) {
      System.Console.Out.WriteLine (e.Message);
      Asn1Util.WriteStackTrace(e, Console.Error);
      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.