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 java.io.OutputStream. 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 (new FileOutputStream (filename));

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

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

       if (trace) {
           System.out.println ("Encoding was successful");
       }
   }
   catch (Exception e) {
      System.out.println(e.getMessage());
      e.printStackTrace();
   }
   finally {
      try {
         if (encodeStream != null) encodeStream.close ();
      }
      catch (Exception e) {}
   }