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
40 [CLSCompliantAttribute(true)]
41 public abstract class Asn1Structured
:Asn1Object
43 private Asn1Object
[] content
;
45 private int contentIndex
= 0;
48 * Create a an Asn1 structured type with default size of 10
50 * @param the Asn1Identifier containing the tag for this structured type
52 protected internal Asn1Structured(Asn1Identifier id
):this(id
, 10)
58 * Create a an Asn1 structured type with the designated size
60 * @param id the Asn1Identifier containing the tag for this structured type
62 * @param size the size to allocate
64 protected internal Asn1Structured(Asn1Identifier id
, int size
):base(id
)
66 content
= new Asn1Object
[size
];
71 * Create a an Asn1 structured type with default size of 10
73 * @param id the Asn1Identifier containing the tag for this structured type
75 * @param content an array containing the content
77 * @param size the number of items of content in the array
79 protected internal Asn1Structured(Asn1Identifier id
, Asn1Object
[] newContent
, int size
):base(id
)
86 /// <summary> Encodes the contents of this Asn1Structured directly to an output
89 public override void encode(Asn1Encoder enc
, System
.IO
.Stream out_Renamed
)
91 enc
.encode(this, out_Renamed
);
95 /// <summary> Decode an Asn1Structured type from an InputStream.</summary>
96 [CLSCompliantAttribute(false)]
97 protected internal void decodeStructured(Asn1Decoder dec
, System
.IO
.Stream in_Renamed
, int len
)
99 int[] componentLen
= new int[1]; // collects length of component
103 add(dec
.decode(in_Renamed
, componentLen
));
104 len
-= componentLen
[0];
109 /// <summary> Returns an array containing the individual ASN.1 elements
110 /// of this Asn1Structed object.
113 /// <returns> an array of Asn1Objects
115 public Asn1Object
[] toArray()
117 Asn1Object
[] cloneArray
= new Asn1Object
[contentIndex
];
118 Array
.Copy((System
.Array
) content
, 0, (System
.Array
) cloneArray
, 0, contentIndex
);
122 /// <summary> Adds a new Asn1Object to the end of this Asn1Structured
126 /// <param name="value">The Asn1Object to add to this Asn1Structured
129 public void add(Asn1Object value_Renamed
)
131 if (contentIndex
== content
.Length
)
133 // Array too small, need to expand it, double length
134 int newSize
= contentIndex
+ contentIndex
;
135 Asn1Object
[] newArray
= new Asn1Object
[newSize
];
136 Array
.Copy((System
.Array
) content
, 0, (System
.Array
) newArray
, 0, contentIndex
);
139 content
[contentIndex
++] = value_Renamed
;
143 /// <summary> Replaces the Asn1Object in the specified index position of
144 /// this Asn1Structured object.
147 /// <param name="index">The index into the Asn1Structured object where
148 /// this new ANS1Object will be placed.
151 /// <param name="value">The Asn1Object to set in this Asn1Structured
154 public void set_Renamed(int index
, Asn1Object value_Renamed
)
156 if ((index
>= contentIndex
) || (index
< 0))
158 throw new System
.IndexOutOfRangeException("Asn1Structured: get: index " + index
+ ", size " + contentIndex
);
160 content
[index
] = value_Renamed
;
164 /// <summary> Gets a specific Asn1Object in this structred object.
167 /// <param name="index">The index of the Asn1Object to get from
168 /// this Asn1Structured object.
170 public Asn1Object
get_Renamed(int index
)
172 if ((index
>= contentIndex
) || (index
< 0))
174 throw new System
.IndexOutOfRangeException("Asn1Structured: set: index " + index
+ ", size " + contentIndex
);
176 return content
[index
];
179 /// <summary> Returns the number of Asn1Obejcts that have been encoded
180 /// into this Asn1Structured class.
187 /// <summary> Creates a String representation of this Asn1Structured.
191 /// <param name="type">the Type to put in the String representing this structured object
194 /// <returns> the String representation of this object.
196 [CLSCompliantAttribute(false)]
197 public virtual System
.String
toString(System
.String type
)
199 System
.Text
.StringBuilder sb
= new System
.Text
.StringBuilder();
203 for (int i
= 0; i
< contentIndex
; i
++)
205 sb
.Append(content
[i
]);
206 if (i
!= contentIndex
- 1)
211 return base.ToString() + sb
.ToString();