Procedure for Calling MDER Encode Methods

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 output stream into which the value will be encoded.

  2. Invoke the encode method.

  3. Close the output stream.

The first step is the creation of an encode output stream, an Asn1MderOutputStream. There is a single constructor which accepts a System.IO.Stream. As usual, you may use a buffered output stream, file output stream, byte array output stream, etc., or some combination thereof.

The second step is the invocation of the encode method. The calling arguments were described earlier.

Finally, close the output stream.

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

   // Note: personnelRecord object was previously populated with data

   Asn1MderOutputStream encodeStream = null;

   try {
       // Step 1: Create an encode output stream.
       encodeStream = new Asn1MderOutputStream (File.OpenWrite(filename));

      // Step 2: Invoke the encode method.

       personnelRecord.Encode (encodeStream, /*useCachedLength=*/false);

       if (trace) {
           System.Console.Out.WriteLine ("Encoding was successful");
       }
   }
   catch (Exception e) {
      System.Console.Out.WriteLine (e.Message);
      Asn1Util.WriteStackTrace(e, Console.Error);
      return;
   }
   finally {
      try {
         if (encodeStream != null) encodeStream.Close ();
      }
      catch (Exception e) {}
   }