1 /******************************************************************************
3 * Copyright (c) 2003 Novell Inc. www.novell.com
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the Software), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 *******************************************************************************/
24 // Novell.Directory.Ldap.Asn1.Asn1Structured.cs
27 // Sunil Kumar (Sunilk@novell.com)
29 // (C) 2003 Novell, Inc (http://www.novell.com)
34 namespace Novell.Directory.Ldap.Asn1
37 /// <summary> This class serves as the base type for all ASN.1
41 [CLSCompliantAttribute(true)]
43 public abstract class Asn1Structured:Asn1Object
45 private Asn1Object[] content;
47 private int contentIndex = 0;
50 * Create a an Asn1 structured type with default size of 10
52 * @param the Asn1Identifier containing the tag for this structured type
54 protected internal Asn1Structured(Asn1Identifier id):this(id, 10)
60 * Create a an Asn1 structured type with the designated size
62 * @param id the Asn1Identifier containing the tag for this structured type
64 * @param size the size to allocate
66 protected internal Asn1Structured(Asn1Identifier id, int size):base(id)
68 content = new Asn1Object[size];
73 * Create a an Asn1 structured type with default size of 10
75 * @param id the Asn1Identifier containing the tag for this structured type
77 * @param content an array containing the content
79 * @param size the number of items of content in the array
81 protected internal Asn1Structured(Asn1Identifier id, Asn1Object[] newContent, int size):base(id)
88 /// <summary> Encodes the contents of this Asn1Structured directly to an output
91 public override void encode(Asn1Encoder enc, System.IO.Stream out_Renamed)
93 enc.encode(this, out_Renamed);
97 /// <summary> Decode an Asn1Structured type from an InputStream.</summary>
98 [CLSCompliantAttribute(false)]
99 protected internal void decodeStructured(Asn1Decoder dec, System.IO.Stream in_Renamed, int len)
101 int[] componentLen = new int[1]; // collects length of component
105 add(dec.decode(in_Renamed, componentLen));
106 len -= componentLen[0];
111 /// <summary> Returns an array containing the individual ASN.1 elements
112 /// of this Asn1Structed object.
115 /// <returns> an array of Asn1Objects
117 public Asn1Object[] toArray()
119 Asn1Object[] cloneArray = new Asn1Object[contentIndex];
120 Array.Copy((System.Array) content, 0, (System.Array) cloneArray, 0, contentIndex);
124 /// <summary> Adds a new Asn1Object to the end of this Asn1Structured
128 /// <param name="value">The Asn1Object to add to this Asn1Structured
131 public void add(Asn1Object value_Renamed)
133 if (contentIndex == content.Length)
135 // Array too small, need to expand it, double length
136 int newSize = contentIndex + contentIndex;
137 Asn1Object[] newArray = new Asn1Object[newSize];
138 Array.Copy((System.Array) content, 0, (System.Array) newArray, 0, contentIndex);
141 content[contentIndex++] = value_Renamed;
145 /// <summary> Replaces the Asn1Object in the specified index position of
146 /// this Asn1Structured object.
149 /// <param name="index">The index into the Asn1Structured object where
150 /// this new ANS1Object will be placed.
153 /// <param name="value">The Asn1Object to set in this Asn1Structured
156 public void set_Renamed(int index, Asn1Object value_Renamed)
158 if ((index >= contentIndex) || (index < 0))
160 throw new System.IndexOutOfRangeException("Asn1Structured: get: index " + index + ", size " + contentIndex);
162 content[index] = value_Renamed;
166 /// <summary> Gets a specific Asn1Object in this structred object.
169 /// <param name="index">The index of the Asn1Object to get from
170 /// this Asn1Structured object.
172 public Asn1Object get_Renamed(int index)
174 if ((index >= contentIndex) || (index < 0))
176 throw new System.IndexOutOfRangeException("Asn1Structured: set: index " + index + ", size " + contentIndex);
178 return content[index];
181 /// <summary> Returns the number of Asn1Obejcts that have been encoded
182 /// into this Asn1Structured class.
189 /// <summary> Creates a String representation of this Asn1Structured.
193 /// <param name="type">the Type to put in the String representing this structured object
196 /// <returns> the String representation of this object.
198 [CLSCompliantAttribute(false)]
199 public virtual System.String toString(System.String type)
201 System.Text.StringBuilder sb = new System.Text.StringBuilder();
205 for (int i = 0; i < contentIndex; i++)
207 sb.Append(content[i]);
208 if (i != contentIndex - 1)
213 return base.ToString() + sb.ToString();