Reuse of C# Decoding Objects

The sample above showed the BER decoding of a single message. In a typical application, a loop would be involved to decode a series of messages. While it would be possible to use the code shown above in a loop, it would not be the most efficient way to decode the messages. Objects should be reused where possible to avoid the overhead of excessive memory allocations and garbage collection.

A single decode buffer object can be used to process a stream of messages. If the decode message buffer is created using an input stream object that contains a series of messages (for example, a file containing multiple records or a communications device), all that needs to be done is the continuous invocation of the BER decode method for the given message type.

Nothing special needs to be done to reuse the generated type object for decoding. The decoder will automatically all the internal Init() method before decoding to make sure all items are reset to their starting state.

In the example above, all that would need to be done to decode a series of personnel records is the inclusion of a loop after the PersonnelRecord object was created in step 2:

   for (;;) {
      personnelRecord.Decode (decodeBuffer);

      if (trace) {
         System.Console.Out.WriteLine ("Decode was successful");
         personnelRecord.Print ("personnelRecord");
      }
   }