TOC PREV NEXT INDEX


Reuse of C# Encoding Objects



The simple example above showed the procedure to encode a single record. But what if you had to decode a series of the same record over and over again? This is a common occurrence in a BER encoding application.

You would not want to recreate the data holder and message buffer objects on each pass of the loop. This would have an adverse effect on the performance of the application. What you would want to do is only create the objects a single time and then reuse them to encode each message instance.

It turns out that this is an easy thing to do. The public member variable access to the data holder object makes it easy to change the variables on each given pass. And the encode buffer object contains a Reset method for resetting the encode buffer for subsequent encodings. The use of this method has the advantage of not releasing any of the memory that had been accumulated to this point for previous encodings.

To show an example of object reuse, suppose we were going to encode a series of names. The ASN.1 type for the names would be as follows:

	Name ::= [APPLICATION 1] IMPLICIT SEQUENCE {
 
	   givenName	IA5String,
 
	   initial		IA5String,
 
	   familyName	IA5String
 
	}
 

The generated C# class would contain public member variables for each of the string objects:

	public Asn1IA5String givenName;
 
	public Asn1IA5String initial;
 
	public Asn1IA5String familyName;
 

The most efficient way to repopulate these variables within a loop would be simply to assign each of the new strings to be encoded directly to the public mValue member variables contained within the Asn1IA5String objects (i.e., the Name or Asn1IA5String objects should not be reconstructed each time).

A code snippet showing how this could be done is as follows:

// Step 1: Create Name and Asn1BerEncodeBuffer objects for use in 
 
// the loop..
 

 
Name name = new Name ("", "", "");  // creates empty string objects
 
Asn1BerEncodeBuffer encodeBuffer = new Asn1BerEncodeBuffer ();
 

 
for (;;) {
 

 
   // logic here to read name components from a DB or other medium
 

 
   ...
 

 
   // populate string variables (assume string1, 2, and 3 are string 
 
   // variables read from DB above)..
 

 
   name.givenName.mValue = string1;
 
   name.initial.mValue = string2;
 
   name.familyName.mValue = string3;
 

 
   // encode
 

 
   try {
 
      len = name.Encode (encodeBuffer, true);
 

 
      // do something with the encoded message component
 

 
      ...
 

 
      // reset encode buffer for next pass
 

 
      encodeBuffer.Reset ();
 
   }
 
   catch (Asn1Exception e) {
 
      // handle error ..
 
   }
 
}
 

 

 

Objective Systems, Inc.

55 Dowlin Forge Road
Exton, Pennsylvania 19341
http://www.obj-sys.com
Phone: (484) 875-9841
Toll-free: (877) 307-6855 (US only)
Fax: (484) 875-9830
info@obj-sys.com
TOC PREV NEXT INDEX

Copyright © 1997-2008 Objective Systems,Inc.
All Rights Reserved.


This document may be distributed in any form, electronic
or otherwise, provided that it is distributed in its entirety
and that the copyright and this notice are included.