Added ifdef for TARGET_JVM.
[mono.git] / mcs / class / Novell.Directory.Ldap / Novell.Directory.Ldap.Asn1 / Asn1Structured.cs
1 /******************************************************************************
2 * The MIT License
3 * Copyright (c) 2003 Novell Inc.  www.novell.com
4
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:
11
12 * The above copyright notice and this permission notice shall be included in 
13 * all copies or substantial portions of the Software.
14
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
21 * SOFTWARE.
22 *******************************************************************************/
23 //
24 // Novell.Directory.Ldap.Asn1.Asn1Structured.cs
25 //
26 // Author:
27 //   Sunil Kumar (Sunilk@novell.com)
28 //
29 // (C) 2003 Novell, Inc (http://www.novell.com)
30 //
31
32 using System;
33
34 namespace Novell.Directory.Ldap.Asn1
35 {
36         
37         /// <summary> This class serves as the base type for all ASN.1
38         /// structured types.
39         /// </summary>
40 #if !TARGET_JVM 
41         [CLSCompliantAttribute(true)]
42 #endif
43         public abstract class Asn1Structured:Asn1Object
44         {
45                 private Asn1Object[] content;
46                 
47                 private int contentIndex = 0;
48                 
49                 /*
50                 * Create a an Asn1 structured type with default size of 10
51                 *
52                 * @param the Asn1Identifier containing the tag for this structured type
53                 */
54                 protected internal Asn1Structured(Asn1Identifier id):this(id, 10)
55                 {
56                         return ;
57                 }
58                 
59                 /*
60                 * Create a an Asn1 structured type with the designated size
61                 *
62                 * @param id the Asn1Identifier containing the tag for this structured type
63                 *
64                 * @param size the size to allocate
65                 */
66                 protected internal Asn1Structured(Asn1Identifier id, int size):base(id)
67                 {
68                         content = new Asn1Object[size];
69                         return ;
70                 }
71                 
72                 /*
73                 * Create a an Asn1 structured type with default size of 10
74                 *
75                 * @param id the Asn1Identifier containing the tag for this structured type
76                 *
77                 * @param content an array containing the content
78                 *
79                 * @param size the number of items of content in the array
80                 */
81                 protected internal Asn1Structured(Asn1Identifier id, Asn1Object[] newContent, int size):base(id)
82                 {
83                         content = newContent;
84                         contentIndex = size;
85                         return ;
86                 }
87                 
88                 /// <summary> Encodes the contents of this Asn1Structured directly to an output
89                 /// stream.
90                 /// </summary>
91                 public override void  encode(Asn1Encoder enc, System.IO.Stream out_Renamed)
92                 {
93                         enc.encode(this, out_Renamed);
94                         return ;
95                 }
96                 
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)
100                 {
101                         int[] componentLen = new int[1]; // collects length of component
102                         
103                         while (len > 0)
104                         {
105                                 add(dec.decode(in_Renamed, componentLen));
106                                 len -= componentLen[0];
107                         }
108                         return ;
109                 }
110                 
111                 /// <summary> Returns an array containing the individual ASN.1 elements
112                 /// of this Asn1Structed object.
113                 /// 
114                 /// </summary>
115                 /// <returns> an array of Asn1Objects
116                 /// </returns>
117                 public Asn1Object[] toArray()
118                 {
119                         Asn1Object[] cloneArray = new Asn1Object[contentIndex];
120                         Array.Copy((System.Array) content, 0, (System.Array) cloneArray, 0, contentIndex);
121                         return cloneArray;
122                 }
123                 
124                 /// <summary> Adds a new Asn1Object to the end of this Asn1Structured
125                 /// object.
126                 /// 
127                 /// </summary>
128                 /// <param name="value">The Asn1Object to add to this Asn1Structured
129                 /// object.
130                 /// </param>
131                 public void  add(Asn1Object value_Renamed)
132                 {
133                         if (contentIndex == content.Length)
134                         {
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);
139                                 content = newArray;
140                         }
141                         content[contentIndex++] = value_Renamed;
142                         return ;
143                 }
144                 
145                 /// <summary> Replaces the Asn1Object in the specified index position of
146                 /// this Asn1Structured object.
147                 /// 
148                 /// </summary>
149                 /// <param name="index">The index into the Asn1Structured object where
150                 /// this new ANS1Object will be placed.
151                 /// 
152                 /// </param>
153                 /// <param name="value">The Asn1Object to set in this Asn1Structured
154                 /// object.
155                 /// </param>
156                 public void  set_Renamed(int index, Asn1Object value_Renamed)
157                 {
158                         if ((index >= contentIndex) || (index < 0))
159                         {
160                                 throw new System.IndexOutOfRangeException("Asn1Structured: get: index " + index + ", size " + contentIndex);
161                         }
162                         content[index] = value_Renamed;
163                         return ;
164                 }
165                 
166                 /// <summary> Gets a specific Asn1Object in this structred object.
167                 /// 
168                 /// </summary>
169                 /// <param name="index">The index of the Asn1Object to get from
170                 /// this Asn1Structured object.
171                 /// </param>
172                 public Asn1Object get_Renamed(int index)
173                 {
174                         if ((index >= contentIndex) || (index < 0))
175                         {
176                                 throw new System.IndexOutOfRangeException("Asn1Structured: set: index " + index + ", size " + contentIndex);
177                         }
178                         return content[index];
179                 }
180                 
181                 /// <summary> Returns the number of Asn1Obejcts that have been encoded
182                 /// into this Asn1Structured class.
183                 /// </summary>
184                 public int size()
185                 {
186                         return contentIndex;
187                 }
188                 
189                 /// <summary> Creates a String representation of this Asn1Structured.
190                 /// object.
191                 /// 
192                 /// </summary>
193                 /// <param name="type">the Type to put in the String representing this structured object
194                 /// 
195                 /// </param>
196                 /// <returns> the String representation of this object.
197                 /// </returns>
198                 [CLSCompliantAttribute(false)]
199                 public virtual System.String toString(System.String type)
200                 {
201                         System.Text.StringBuilder sb = new System.Text.StringBuilder();
202                         
203                         sb.Append(type);
204                         
205                         for (int i = 0; i < contentIndex; i++)
206                         {
207                                 sb.Append(content[i]);
208                                 if (i != contentIndex - 1)
209                                         sb.Append(", ");
210                         }
211                         sb.Append(" }");
212                         
213                         return base.ToString() + sb.ToString();
214                 }
215         }
216 }