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.SupportClass.cs
27 // Sunil Kumar (Sunilk@novell.com)
29 // (C) 2003 Novell, Inc (http://www.novell.com)
32 // Support classes replicate the functionality of the original code, but in some cases they are
33 // substantially different architecturally. Although every effort is made to preserve the
34 // original architecture of the application in the converted project, the user should be aware that
35 // the primary goal of these support classes is to replicate functionality, and that at times
36 // the architecture of the resulting solution may differ somewhat.
42 /// This interface should be implemented by any class whose instances are intended
43 /// to be executed by a thread.
45 public interface IThreadRunnable
48 /// This method has to be implemented in order that starting of the thread causes the object's
49 /// run method to be called in that separately executing thread.
55 public class Integer32 : System.Object
57 private System.Int32 _wintv;
59 public Integer32(System.Int32 ival)
64 public System.Int32 intValue
78 /// Contains conversion support elements such as classes, interfaces and static methods.
80 public class SupportClass
83 /// Receives a byte array and returns it transformed in an sbyte array
85 /// <param name="byteArray">Byte array to process</param>
86 /// <returns>The transformed array</returns>
87 [CLSCompliantAttribute(false)]
88 public static sbyte[] ToSByteArray(byte[] byteArray)
90 sbyte[] sbyteArray = new sbyte[byteArray.Length];
91 for(int index=0; index < byteArray.Length; index++)
92 sbyteArray[index] = (sbyte) byteArray[index];
95 /*******************************/
97 /// Converts an array of sbytes to an array of bytes
99 /// <param name="sbyteArray">The array of sbytes to be converted</param>
100 /// <returns>The new array of bytes</returns>
101 [CLSCompliantAttribute(false)]
102 public static byte[] ToByteArray(sbyte[] sbyteArray)
104 byte[] byteArray = new byte[sbyteArray.Length];
105 for(int index=0; index < sbyteArray.Length; index++)
106 byteArray[index] = (byte) sbyteArray[index];
111 /// Converts a string to an array of bytes
113 /// <param name="sourceString">The string to be converted</param>
114 /// <returns>The new array of bytes</returns>
115 public static byte[] ToByteArray(string sourceString)
117 byte[] byteArray = new byte[sourceString.Length];
118 for (int index=0; index < sourceString.Length; index++)
119 byteArray[index] = (byte) sourceString[index];
124 /// Converts a array of object-type instances to a byte-type array.
126 /// <param name="tempObjectArray">Array to convert.</param>
127 /// <returns>An array of byte type elements.</returns>
128 public static byte[] ToByteArray(object[] tempObjectArray)
130 byte[] byteArray = new byte[tempObjectArray.Length];
131 for (int index = 0; index < tempObjectArray.Length; index++)
132 byteArray[index] = (byte)tempObjectArray[index];
137 /*******************************/
138 /// <summary>Reads a number of characters from the current source Stream and writes the data to the target array at the specified index.</summary>
139 /// <param name="sourceStream">The source Stream to read from.</param>
140 /// <param name="target">Contains the array of characteres read from the source Stream.</param>
141 /// <param name="start">The starting index of the target array.</param>
142 /// <param name="count">The maximum number of characters to read from the source Stream.</param>
143 /// <returns>The number of characters read. The number will be less than or equal to count depending on the data available in the source Stream. Returns -1 if the end of the stream is reached.</returns>
144 [CLSCompliantAttribute(false)]
145 public static System.Int32 ReadInput(System.IO.Stream sourceStream, ref sbyte[] target, int start, int count)
147 // Returns 0 bytes if not enough space in target
148 if (target.Length == 0)
151 byte[] receiver = new byte[target.Length];
153 int startIndex=start;
154 int bytesToRead=count;
155 while( bytesToRead > 0 ) {
156 int n= sourceStream.Read(receiver, startIndex, bytesToRead);
167 for(int i = start; i < start + bytesRead; i++)
168 target[i] = (sbyte)receiver[i];
173 /// <summary>Reads a number of characters from the current source TextReader and writes the data to the target array at the specified index.</summary>
174 /// <param name="sourceTextReader">The source TextReader to read from</param>
175 /// <param name="target">Contains the array of characteres read from the source TextReader.</param>
176 /// <param name="start">The starting index of the target array.</param>
177 /// <param name="count">The maximum number of characters to read from the source TextReader.</param>
178 /// <returns>The number of characters read. The number will be less than or equal to count depending on the data available in the source TextReader. Returns -1 if the end of the stream is reached.</returns>
179 [CLSCompliantAttribute(false)]
180 public static System.Int32 ReadInput(System.IO.TextReader sourceTextReader, ref sbyte[] target, int start, int count)
182 // Returns 0 bytes if not enough space in target
183 if (target.Length == 0) return 0;
185 char[] charArray = new char[target.Length];
186 int bytesRead = sourceTextReader.Read(charArray, start, count);
189 if (bytesRead == 0) return -1;
191 for(int index=start; index<start+bytesRead; index++)
192 target[index] = (sbyte)charArray[index];
197 /*******************************/
199 /// This method returns the literal value received
201 /// <param name="literal">The literal to return</param>
202 /// <returns>The received value</returns>
203 public static long Identity(long literal)
209 /// This method returns the literal value received
211 /// <param name="literal">The literal to return</param>
212 /// <returns>The received value</returns>
213 [CLSCompliantAttribute(false)]
214 public static ulong Identity(ulong literal)
220 /// This method returns the literal value received
222 /// <param name="literal">The literal to return</param>
223 /// <returns>The received value</returns>
224 public static float Identity(float literal)
230 /// This method returns the literal value received
232 /// <param name="literal">The literal to return</param>
233 /// <returns>The received value</returns>
234 public static double Identity(double literal)
239 /*******************************/
241 /// The class performs token processing from strings
243 public class Tokenizer
245 //Element list identified
246 private System.Collections.ArrayList elements;
247 //Source string to use
248 private string source;
249 //The tokenizer uses the default delimiter set: the space character, the tab character, the newline character, and the carriage-return character
250 private string delimiters = " \t\n\r";
252 private bool returnDelims=false;
254 /// Initializes a new class instance with a specified string to process
256 /// <param name="source">String to tokenize</param>
257 public Tokenizer(string source)
259 this.elements = new System.Collections.ArrayList();
260 this.elements.AddRange(source.Split(this.delimiters.ToCharArray()));
261 this.RemoveEmptyStrings();
262 this.source = source;
266 /// Initializes a new class instance with a specified string to process
267 /// and the specified token delimiters to use
269 /// <param name="source">String to tokenize</param>
270 /// <param name="delimiters">String containing the delimiters</param>
271 public Tokenizer(string source, string delimiters)
273 this.elements = new System.Collections.ArrayList();
274 this.delimiters = delimiters;
275 this.elements.AddRange(source.Split(this.delimiters.ToCharArray()));
276 this.RemoveEmptyStrings();
277 this.source = source;
280 public Tokenizer(string source, string delimiters,bool retDel)
282 this.elements = new System.Collections.ArrayList();
283 this.delimiters = delimiters;
284 this.source = source;
285 this.returnDelims = retDel;
289 this.elements.AddRange(source.Split(this.delimiters.ToCharArray()));
290 this.RemoveEmptyStrings();
293 private void Tokenize()
295 string tempstr = this.source;
297 if (tempstr.IndexOfAny(this.delimiters.ToCharArray()) < 0 && tempstr.Length > 0)
299 this.elements.Add(tempstr);
301 else if (tempstr.IndexOfAny(this.delimiters.ToCharArray()) < 0 && tempstr.Length <= 0)
305 while (tempstr.IndexOfAny(this.delimiters.ToCharArray()) >= 0)
307 if(tempstr.IndexOfAny(this.delimiters.ToCharArray()) == 0)
309 if (tempstr.Length > 1 )
311 this.elements.Add(tempstr.Substring(0,1));
312 tempstr=tempstr.Substring(1);
319 toks = tempstr.Substring(0,tempstr.IndexOfAny(this.delimiters.ToCharArray()));
320 this.elements.Add(toks);
321 this.elements.Add(tempstr.Substring(toks.Length,1));
322 if ( tempstr.Length > (toks.Length + 1))
324 tempstr = tempstr.Substring(toks.Length + 1);
330 if (tempstr.Length > 0)
332 this.elements.Add(tempstr);
337 /// Current token count for the source string
343 return (this.elements.Count);
348 /// Determines if there are more tokens to return from the source string
350 /// <returns>True or false, depending if there are more tokens</returns>
351 public bool HasMoreTokens()
353 return (this.elements.Count > 0);
357 /// Returns the next token from the token list
359 /// <returns>The string value of the token</returns>
360 public string NextToken()
363 if (source == "") throw new System.Exception();
368 RemoveEmptyStrings();
369 result = (string) this.elements[0];
370 this.elements.RemoveAt(0);
375 this.elements = new System.Collections.ArrayList();
376 this.elements.AddRange(this.source.Split(delimiters.ToCharArray()));
377 RemoveEmptyStrings();
378 result = (string) this.elements[0];
379 this.elements.RemoveAt(0);
380 this.source = this.source.Remove(this.source.IndexOf(result),result.Length);
381 this.source = this.source.TrimStart(this.delimiters.ToCharArray());
388 /// Returns the next token from the source string, using the provided
391 /// <param name="delimiters">String containing the delimiters to use</param>
392 /// <returns>The string value of the token</returns>
393 public string NextToken(string delimiters)
395 this.delimiters = delimiters;
400 /// Removes all empty strings from the token list
402 private void RemoveEmptyStrings()
404 for (int index=0; index < this.elements.Count; index++)
405 if ((string)this.elements[index]== "")
407 this.elements.RemoveAt(index);
413 /*******************************/
415 /// Provides support for DateFormat
417 public class DateTimeFormatManager
419 static public DateTimeFormatHashTable manager = new DateTimeFormatHashTable();
422 /// Hashtable class to provide functionality for dateformat properties
424 public class DateTimeFormatHashTable :System.Collections.Hashtable
427 /// Sets the format for datetime.
429 /// <param name="format">DateTimeFormat instance to set the pattern</param>
430 /// <param name="newPattern">A string with the pattern format</param>
431 public void SetDateFormatPattern(System.Globalization.DateTimeFormatInfo format, System.String newPattern)
433 if (this[format] != null)
434 ((DateTimeFormatProperties) this[format]).DateFormatPattern = newPattern;
437 DateTimeFormatProperties tempProps = new DateTimeFormatProperties();
438 tempProps.DateFormatPattern = newPattern;
439 Add(format, tempProps);
444 /// Gets the current format pattern of the DateTimeFormat instance
446 /// <param name="format">The DateTimeFormat instance which the value will be obtained</param>
447 /// <returns>The string representing the current datetimeformat pattern</returns>
448 public string GetDateFormatPattern(System.Globalization.DateTimeFormatInfo format)
450 if (this[format] == null)
453 return ((DateTimeFormatProperties) this[format]).DateFormatPattern;
457 /// Sets the datetimeformat pattern to the giving format
459 /// <param name="format">The datetimeformat instance to set</param>
460 /// <param name="newPattern">The new datetimeformat pattern</param>
461 public void SetTimeFormatPattern(System.Globalization.DateTimeFormatInfo format, System.String newPattern)
463 if (this[format] != null)
464 ((DateTimeFormatProperties) this[format]).TimeFormatPattern = newPattern;
467 DateTimeFormatProperties tempProps = new DateTimeFormatProperties();
468 tempProps.TimeFormatPattern = newPattern;
469 Add(format, tempProps);
474 /// Gets the current format pattern of the DateTimeFormat instance
476 /// <param name="format">The DateTimeFormat instance which the value will be obtained</param>
477 /// <returns>The string representing the current datetimeformat pattern</returns>
478 public string GetTimeFormatPattern(System.Globalization.DateTimeFormatInfo format)
480 if (this[format] == null)
483 return ((DateTimeFormatProperties) this[format]).TimeFormatPattern;
487 /// Internal class to provides the DateFormat and TimeFormat pattern properties on .NET
489 class DateTimeFormatProperties
491 public string DateFormatPattern = "d-MMM-yy";
492 public string TimeFormatPattern = "h:mm:ss tt";
496 /*******************************/
498 /// Gets the DateTimeFormat instance and date instance to obtain the date with the format passed
500 /// <param name="format">The DateTimeFormat to obtain the time and date pattern</param>
501 /// <param name="date">The date instance used to get the date</param>
502 /// <returns>A string representing the date with the time and date patterns</returns>
503 public static string FormatDateTime(System.Globalization.DateTimeFormatInfo format, System.DateTime date)
505 string timePattern = DateTimeFormatManager.manager.GetTimeFormatPattern(format);
506 string datePattern = DateTimeFormatManager.manager.GetDateFormatPattern(format);
507 return date.ToString(datePattern + " " + timePattern, format);
510 /*******************************/
512 /// Adds a new key-and-value pair into the hash table
514 /// <param name="collection">The collection to work with</param>
515 /// <param name="key">Key used to obtain the value</param>
516 /// <param name="newValue">Value asociated with the key</param>
517 /// <returns>The old element associated with the key</returns>
518 public static System.Object PutElement(System.Collections.IDictionary collection, System.Object key, System.Object newValue)
520 System.Object element = collection[key];
521 collection[key] = newValue;
525 /*******************************/
527 /// This class contains static methods to manage arrays.
529 public class ArrayListSupport
532 /// Obtains an array containing all the elements of the collection.
534 /// <param name="collection">The collection from wich to obtain the elements.</param>
535 /// <param name="objects">The array containing all the elements of the collection.</param>
536 /// <returns>The array containing all the elements of the collection.</returns>
537 public static System.Object[] ToArray(System.Collections.ArrayList collection, System.Object[] objects)
540 System.Collections.IEnumerator tempEnumerator = collection.GetEnumerator();
541 while (tempEnumerator.MoveNext())
542 objects[index++] = tempEnumerator.Current;
548 /*******************************/
550 /// Removes the first occurrence of an specific object from an ArrayList instance.
552 /// <param name="arrayList">The ArrayList instance</param>
553 /// <param name="element">The element to remove</param>
554 /// <returns>True if item is found in the ArrayList; otherwise, false</returns>
555 public static System.Boolean VectorRemoveElement(System.Collections.IList arrayList, System.Object element)
557 System.Boolean containsItem = arrayList.Contains(element);
558 arrayList.Remove(element);
562 /*******************************/
564 /// Support class used to handle threads
566 public class ThreadClass : IThreadRunnable
569 /// The instance of System.Threading.Thread
571 private System.Threading.Thread threadField;
574 /// Initializes a new instance of the ThreadClass class
578 threadField = new System.Threading.Thread(new System.Threading.ThreadStart(Run));
582 /// Initializes a new instance of the Thread class.
584 /// <param name="Name">The name of the thread</param>
585 public ThreadClass(string Name)
587 threadField = new System.Threading.Thread(new System.Threading.ThreadStart(Run));
592 /// Initializes a new instance of the Thread class.
594 /// <param name="Start">A ThreadStart delegate that references the methods to be invoked when this thread begins executing</param>
595 public ThreadClass(System.Threading.ThreadStart Start)
597 threadField = new System.Threading.Thread(Start);
601 /// Initializes a new instance of the Thread class.
603 /// <param name="Start">A ThreadStart delegate that references the methods to be invoked when this thread begins executing</param>
604 /// <param name="Name">The name of the thread</param>
605 public ThreadClass(System.Threading.ThreadStart Start, string Name)
607 threadField = new System.Threading.Thread(Start);
612 /// This method has no functionality unless the method is overridden
614 public virtual void Run()
619 /// Causes the operating system to change the state of the current thread instance to ThreadState.Running
621 public virtual void Start()
627 /// Interrupts a thread that is in the WaitSleepJoin thread state
629 public virtual void Interrupt()
631 threadField.Interrupt();
635 /// Gets the current thread instance
637 public System.Threading.Thread Instance
650 /// Gets or sets the name of the thread
652 public System.String Name
656 return threadField.Name;
660 if (threadField.Name == null)
661 threadField.Name = value;
666 /// Gets or sets a value indicating the scheduling priority of a thread
668 public System.Threading.ThreadPriority Priority
672 return threadField.Priority;
676 threadField.Priority = value;
681 /// Gets a value indicating the execution status of the current thread
687 return threadField.IsAlive;
692 /// Gets or sets a value indicating whether or not a thread is a background thread.
694 public bool IsBackground
698 return threadField.IsBackground;
702 threadField.IsBackground = value;
707 /// Blocks the calling thread until a thread terminates
715 /// Blocks the calling thread until a thread terminates or the specified time elapses
717 /// <param name="MiliSeconds">Time of wait in milliseconds</param>
718 public void Join(long MiliSeconds)
722 threadField.Join(new System.TimeSpan(MiliSeconds * 10000));
727 /// Blocks the calling thread until a thread terminates or the specified time elapses
729 /// <param name="MiliSeconds">Time of wait in milliseconds</param>
730 /// <param name="NanoSeconds">Time of wait in nanoseconds</param>
731 public void Join(long MiliSeconds, int NanoSeconds)
735 threadField.Join(new System.TimeSpan(MiliSeconds * 10000 + NanoSeconds * 100));
740 /// Resumes a thread that has been suspended
744 threadField.Resume();
748 /// Raises a ThreadAbortException in the thread on which it is invoked,
749 /// to begin the process of terminating the thread. Calling this method
750 /// usually terminates the thread
758 /// Raises a ThreadAbortException in the thread on which it is invoked,
759 /// to begin the process of terminating the thread while also providing
760 /// exception information about the thread termination.
761 /// Calling this method usually terminates the thread.
763 /// <param name="stateInfo">An object that contains application-specific information, such as state, which can be used by the thread being aborted</param>
764 public void Abort(System.Object stateInfo)
768 threadField.Abort(stateInfo);
773 /// Suspends the thread, if the thread is already suspended it has no effect
775 public void Suspend()
777 threadField.Suspend();
781 /// Obtain a String that represents the current Object
783 /// <returns>A String that represents the current Object</returns>
784 public override System.String ToString()
786 return "Thread[" + Name + "," + Priority.ToString() + "," + "" + "]";
790 /// Gets the currently running thread
792 /// <returns>The currently running thread</returns>
793 public static ThreadClass Current()
795 ThreadClass CurrentThread = new ThreadClass();
796 CurrentThread.Instance = System.Threading.Thread.CurrentThread;
797 return CurrentThread;
802 /*******************************/
804 /// This class contains different methods to manage Collections.
806 public class CollectionSupport : System.Collections.CollectionBase
809 /// Creates an instance of the Collection by using an inherited constructor.
811 public CollectionSupport() : base()
816 /// Adds an specified element to the collection.
818 /// <param name="element">The element to be added.</param>
819 /// <returns>Returns true if the element was successfuly added. Otherwise returns false.</returns>
820 public virtual bool Add(System.Object element)
822 return (this.List.Add(element) != -1);
826 /// Adds all the elements contained in the specified collection.
828 /// <param name="collection">The collection used to extract the elements that will be added.</param>
829 /// <returns>Returns true if all the elements were successfuly added. Otherwise returns false.</returns>
830 public virtual bool AddAll(System.Collections.ICollection collection)
833 if (collection!=null)
835 System.Collections.IEnumerator tempEnumerator = new System.Collections.ArrayList(collection).GetEnumerator();
836 while (tempEnumerator.MoveNext())
838 if (tempEnumerator.Current != null)
839 result = this.Add(tempEnumerator.Current);
847 /// Adds all the elements contained in the specified support class collection.
849 /// <param name="collection">The collection used to extract the elements that will be added.</param>
850 /// <returns>Returns true if all the elements were successfuly added. Otherwise returns false.</returns>
851 public virtual bool AddAll(CollectionSupport collection)
853 return this.AddAll((System.Collections.ICollection)collection);
857 /// Verifies if the specified element is contained into the collection.
859 /// <param name="element"> The element that will be verified.</param>
860 /// <returns>Returns true if the element is contained in the collection. Otherwise returns false.</returns>
861 public virtual bool Contains(System.Object element)
863 return this.List.Contains(element);
867 /// Verifies if all the elements of the specified collection are contained into the current collection.
869 /// <param name="collection">The collection used to extract the elements that will be verified.</param>
870 /// <returns>Returns true if all the elements are contained in the collection. Otherwise returns false.</returns>
871 public virtual bool ContainsAll(System.Collections.ICollection collection)
874 System.Collections.IEnumerator tempEnumerator = new System.Collections.ArrayList(collection).GetEnumerator();
875 while (tempEnumerator.MoveNext())
876 if (!(result = this.Contains(tempEnumerator.Current)))
882 /// Verifies if all the elements of the specified collection are contained into the current collection.
884 /// <param name="collection">The collection used to extract the elements that will be verified.</param>
885 /// <returns>Returns true if all the elements are contained in the collection. Otherwise returns false.</returns>
886 public virtual bool ContainsAll(CollectionSupport collection)
888 return this.ContainsAll((System.Collections.ICollection) collection);
892 /// Verifies if the collection is empty.
894 /// <returns>Returns true if the collection is empty. Otherwise returns false.</returns>
895 public virtual bool IsEmpty()
897 return (this.Count == 0);
901 /// Removes an specified element from the collection.
903 /// <param name="element">The element to be removed.</param>
904 /// <returns>Returns true if the element was successfuly removed. Otherwise returns false.</returns>
905 public virtual bool Remove(System.Object element)
908 if (this.Contains(element))
910 this.List.Remove(element);
917 /// Removes all the elements contained into the specified collection.
919 /// <param name="collection">The collection used to extract the elements that will be removed.</param>
920 /// <returns>Returns true if all the elements were successfuly removed. Otherwise returns false.</returns>
921 public virtual bool RemoveAll(System.Collections.ICollection collection)
924 System.Collections.IEnumerator tempEnumerator = new System.Collections.ArrayList(collection).GetEnumerator();
925 while (tempEnumerator.MoveNext())
927 if (this.Contains(tempEnumerator.Current))
928 result = this.Remove(tempEnumerator.Current);
934 /// Removes all the elements contained into the specified collection.
936 /// <param name="collection">The collection used to extract the elements that will be removed.</param>
937 /// <returns>Returns true if all the elements were successfuly removed. Otherwise returns false.</returns>
938 public virtual bool RemoveAll(CollectionSupport collection)
940 return this.RemoveAll((System.Collections.ICollection) collection);
944 /// Removes all the elements that aren't contained into the specified collection.
946 /// <param name="collection">The collection used to verify the elements that will be retained.</param>
947 /// <returns>Returns true if all the elements were successfully removed. Otherwise returns false.</returns>
948 public virtual bool RetainAll(System.Collections.ICollection collection)
951 System.Collections.IEnumerator tempEnumerator = this.GetEnumerator();
952 CollectionSupport tempCollection = new CollectionSupport();
953 tempCollection.AddAll(collection);
954 while (tempEnumerator.MoveNext())
955 if (!tempCollection.Contains(tempEnumerator.Current))
957 result = this.Remove(tempEnumerator.Current);
961 tempEnumerator = this.GetEnumerator();
968 /// Removes all the elements that aren't contained into the specified collection.
970 /// <param name="collection">The collection used to verify the elements that will be retained.</param>
971 /// <returns>Returns true if all the elements were successfully removed. Otherwise returns false.</returns>
972 public virtual bool RetainAll(CollectionSupport collection)
974 return this.RetainAll((System.Collections.ICollection) collection);
978 /// Obtains an array containing all the elements of the collection.
980 /// <returns>The array containing all the elements of the collection</returns>
981 public virtual System.Object[] ToArray()
984 System.Object[] objects = new System.Object[this.Count];
985 System.Collections.IEnumerator tempEnumerator = this.GetEnumerator();
986 while (tempEnumerator.MoveNext())
987 objects[index++] = tempEnumerator.Current;
992 /// Obtains an array containing all the elements of the collection.
994 /// <param name="objects">The array into which the elements of the collection will be stored.</param>
995 /// <returns>The array containing all the elements of the collection.</returns>
996 public virtual System.Object[] ToArray(System.Object[] objects)
999 System.Collections.IEnumerator tempEnumerator = this.GetEnumerator();
1000 while (tempEnumerator.MoveNext())
1001 objects[index++] = tempEnumerator.Current;
1006 /// Creates a CollectionSupport object with the contents specified in array.
1008 /// <param name="array">The array containing the elements used to populate the new CollectionSupport object.</param>
1009 /// <returns>A CollectionSupport object populated with the contents of array.</returns>
1010 public static CollectionSupport ToCollectionSupport(System.Object[] array)
1012 CollectionSupport tempCollectionSupport = new CollectionSupport();
1013 tempCollectionSupport.AddAll(array);
1014 return tempCollectionSupport;
1018 /*******************************/
1020 /// This class contains different methods to manage list collections.
1022 public class ListCollectionSupport : System.Collections.ArrayList
1025 /// Creates a new instance of the class ListCollectionSupport.
1027 public ListCollectionSupport() : base()
1032 /// Creates a new instance of the class ListCollectionSupport.
1034 /// <param name="collection">The collection to insert into the new object.</param>
1035 public ListCollectionSupport(System.Collections.ICollection collection) : base(collection)
1040 /// Creates a new instance of the class ListCollectionSupport with the specified capacity.
1042 /// <param name="capacity">The capacity of the new array.</param>
1043 public ListCollectionSupport(int capacity) : base(capacity)
1048 /// Adds an object to the end of the List.
1050 /// <param name="valueToInsert">The value to insert in the array list.</param>
1051 /// <returns>Returns true after adding the value.</returns>
1052 public virtual bool Add(System.Object valueToInsert)
1054 base.Insert(this.Count, valueToInsert);
1059 /// Adds all the elements contained into the specified collection, starting at the specified position.
1061 /// <param name="index">Position at which to add the first element from the specified collection.</param>
1062 /// <param name="list">The list used to extract the elements that will be added.</param>
1063 /// <returns>Returns true if all the elements were successfuly added. Otherwise returns false.</returns>
1064 public virtual bool AddAll(int index, System.Collections.IList list)
1066 bool result = false;
1069 System.Collections.IEnumerator tempEnumerator = new System.Collections.ArrayList(list).GetEnumerator();
1070 int tempIndex = index;
1071 while (tempEnumerator.MoveNext())
1073 base.Insert(tempIndex++, tempEnumerator.Current);
1081 /// Adds all the elements contained in the specified collection.
1083 /// <param name="collection">The collection used to extract the elements that will be added.</param>
1084 /// <returns>Returns true if all the elements were successfuly added. Otherwise returns false.</returns>
1085 public virtual bool AddAll(System.Collections.IList collection)
1087 return this.AddAll(this.Count,collection);
1091 /// Adds all the elements contained in the specified support class collection.
1093 /// <param name="collection">The collection used to extract the elements that will be added.</param>
1094 /// <returns>Returns true if all the elements were successfuly added. Otherwise returns false.</returns>
1095 public virtual bool AddAll(CollectionSupport collection)
1097 return this.AddAll(this.Count,collection);
1101 /// Adds all the elements contained into the specified support class collection, starting at the specified position.
1103 /// <param name="index">Position at which to add the first element from the specified collection.</param>
1104 /// <param name="list">The list used to extract the elements that will be added.</param>
1105 /// <returns>Returns true if all the elements were successfuly added. Otherwise returns false.</returns>
1106 public virtual bool AddAll(int index, CollectionSupport collection)
1108 return this.AddAll(index,(System.Collections.IList)collection);
1112 /// Creates a copy of the ListCollectionSupport.
1114 /// <returns> A copy of the ListCollectionSupport.</returns>
1115 public virtual System.Object ListCollectionClone()
1117 return MemberwiseClone();
1122 /// Returns an iterator of the collection.
1124 /// <returns>An IEnumerator.</returns>
1125 public virtual System.Collections.IEnumerator ListIterator()
1127 return base.GetEnumerator();
1131 /// Removes all the elements contained into the specified collection.
1133 /// <param name="collection">The collection used to extract the elements that will be removed.</param>
1134 /// <returns>Returns true if all the elements were successfuly removed. Otherwise returns false.</returns>
1135 public virtual bool RemoveAll(System.Collections.ICollection collection)
1137 bool result = false;
1138 System.Collections.IEnumerator tempEnumerator = new System.Collections.ArrayList(collection).GetEnumerator();
1139 while (tempEnumerator.MoveNext())
1142 if (base.Contains(tempEnumerator.Current))
1143 base.Remove(tempEnumerator.Current);
1149 /// Removes all the elements contained into the specified collection.
1151 /// <param name="collection">The collection used to extract the elements that will be removed.</param>
1152 /// <returns>Returns true if all the elements were successfuly removed. Otherwise returns false.</returns>
1153 public virtual bool RemoveAll(CollectionSupport collection)
1155 return this.RemoveAll((System.Collections.ICollection) collection);
1159 /// Removes the value in the specified index from the list.
1161 /// <param name="index">The index of the value to remove.</param>
1162 /// <returns>Returns the value removed.</returns>
1163 public virtual System.Object RemoveElement(int index)
1165 System.Object objectRemoved = this[index];
1166 this.RemoveAt(index);
1167 return objectRemoved;
1171 /// Removes an specified element from the collection.
1173 /// <param name="element">The element to be removed.</param>
1174 /// <returns>Returns true if the element was successfuly removed. Otherwise returns false.</returns>
1175 public virtual bool RemoveElement(System.Object element)
1178 bool result = false;
1179 if (this.Contains(element))
1181 base.Remove(element);
1188 /// Removes the first value from an array list.
1190 /// <returns>Returns the value removed.</returns>
1191 public virtual System.Object RemoveFirst()
1193 System.Object objectRemoved = this[0];
1195 return objectRemoved;
1199 /// Removes the last value from an array list.
1201 /// <returns>Returns the value removed.</returns>
1202 public virtual System.Object RemoveLast()
1204 System.Object objectRemoved = this[this.Count-1];
1205 base.RemoveAt(this.Count-1);
1206 return objectRemoved;
1210 /// Removes all the elements that aren't contained into the specified collection.
1212 /// <param name="collection">The collection used to verify the elements that will be retained.</param>
1213 /// <returns>Returns true if all the elements were successfully removed. Otherwise returns false.</returns>
1214 public virtual bool RetainAll(System.Collections.ICollection collection)
1216 bool result = false;
1217 System.Collections.IEnumerator tempEnumerator = this.GetEnumerator();
1218 ListCollectionSupport tempCollection = new ListCollectionSupport(collection);
1219 while (tempEnumerator.MoveNext())
1220 if (!tempCollection.Contains(tempEnumerator.Current))
1222 result = this.RemoveElement(tempEnumerator.Current);
1226 tempEnumerator = this.GetEnumerator();
1233 /// Removes all the elements that aren't contained into the specified collection.
1235 /// <param name="collection">The collection used to verify the elements that will be retained.</param>
1236 /// <returns>Returns true if all the elements were successfully removed. Otherwise returns false.</returns>
1237 public virtual bool RetainAll(CollectionSupport collection)
1239 return this.RetainAll((System.Collections.ICollection) collection);
1243 /// Verifies if all the elements of the specified collection are contained into the current collection.
1245 /// <param name="collection">The collection used to extract the elements that will be verified.</param>
1246 /// <returns>Returns true if all the elements are contained in the collection. Otherwise returns false.</returns>
1247 public virtual bool ContainsAll(System.Collections.ICollection collection)
1249 bool result = false;
1250 System.Collections.IEnumerator tempEnumerator = new System.Collections.ArrayList(collection).GetEnumerator();
1251 while (tempEnumerator.MoveNext())
1252 if(!(result = this.Contains(tempEnumerator.Current)))
1258 /// Verifies if all the elements of the specified collection are contained into the current collection.
1260 /// <param name="collection">The collection used to extract the elements that will be verified.</param>
1261 /// <returns>Returns true if all the elements are contained in the collection. Otherwise returns false.</returns>
1262 public virtual bool ContainsAll(CollectionSupport collection)
1264 return this.ContainsAll((System.Collections.ICollection) collection);
1268 /// Returns a new list containing a portion of the current list between a specified range.
1270 /// <param name="startIndex">The start index of the range.</param>
1271 /// <param name="endIndex">The end index of the range.</param>
1272 /// <returns>A ListCollectionSupport instance containing the specified elements.</returns>
1273 public virtual ListCollectionSupport SubList(int startIndex, int endIndex)
1276 System.Collections.IEnumerator tempEnumerator = this.GetEnumerator();
1277 ListCollectionSupport result = new ListCollectionSupport();
1278 for(index = startIndex; index < endIndex; index++)
1279 result.Add(this[index]);
1280 return (ListCollectionSupport)result;
1284 /// Obtains an array containing all the elements of the collection.
1286 /// <param name="objects">The array into which the elements of the collection will be stored.</param>
1287 /// <returns>The array containing all the elements of the collection.</returns>
1288 public virtual System.Object[] ToArray(System.Object[] objects)
1290 if (objects.Length < this.Count)
1291 objects = new System.Object[this.Count];
1293 System.Collections.IEnumerator tempEnumerator = this.GetEnumerator();
1294 while (tempEnumerator.MoveNext())
1295 objects[index++] = tempEnumerator.Current;
1300 /// Returns an iterator of the collection starting at the specified position.
1302 /// <param name="index">The position to set the iterator.</param>
1303 /// <returns>An IEnumerator at the specified position.</returns>
1304 public virtual System.Collections.IEnumerator ListIterator(int index)
1306 if ((index < 0) || (index > this.Count)) throw new System.IndexOutOfRangeException();
1307 System.Collections.IEnumerator tempEnumerator= this.GetEnumerator();
1311 while ((tempEnumerator.MoveNext()) && (i < index - 1))
1314 return tempEnumerator;
1318 /// Gets the last value from a list.
1320 /// <returns>Returns the last element of the list.</returns>
1321 public virtual System.Object GetLast()
1323 if (this.Count == 0) throw new System.ArgumentOutOfRangeException();
1326 return this[this.Count - 1];
1331 /// Return whether this list is empty.
1333 /// <returns>True if the list is empty, false if it isn't.</returns>
1334 public virtual bool IsEmpty()
1336 return (this.Count == 0);
1340 /// Replaces the element at the specified position in this list with the specified element.
1342 /// <param name="index">Index of element to replace.</param>
1343 /// <param name="element">Element to be stored at the specified position.</param>
1344 /// <returns>The element previously at the specified position.</returns>
1345 public virtual System.Object Set(int index, System.Object element)
1347 System.Object result = this[index];
1348 this[index] = element;
1353 /// Returns the element at the specified position in the list.
1355 /// <param name="index">Index of element to return.</param>
1356 /// <param name="element">Element to be stored at the specified position.</param>
1357 /// <returns>The element at the specified position in the list.</returns>
1358 public virtual System.Object Get(int index)
1364 /*******************************/
1366 /// This class manages array operations.
1368 public class ArraysSupport
1371 /// Compares the entire members of one array whith the other one.
1373 /// <param name="array1">The array to be compared.</param>
1374 /// <param name="array2">The array to be compared with.</param>
1375 /// <returns>True if both arrays are equals otherwise it returns false.</returns>
1376 /// <remarks>Two arrays are equal if they contains the same elements in the same order.</remarks>
1377 public static bool IsArrayEqual(System.Array array1, System.Array array2)
1379 if (array1.Length != array2.Length)
1381 for (int i = 0; i < array1.Length; i++)
1382 if (!(array1.GetValue(i).Equals(array2.GetValue(i))))
1388 /// Fills the array with an specific value from an specific index to an specific index.
1390 /// <param name="array">The array to be filled.</param>
1391 /// <param name="fromindex">The first index to be filled.</param>
1392 /// <param name="toindex">The last index to be filled.</param>
1393 /// <param name="val">The value to fill the array with.</param>
1394 public static void FillArray(System.Array array, System.Int32 fromindex,System.Int32 toindex, System.Object val)
1396 System.Object Temp_Object = val;
1397 System.Type elementtype = array.GetType().GetElementType();
1398 if (elementtype != val.GetType())
1399 Temp_Object = System.Convert.ChangeType(val, elementtype);
1400 if (array.Length == 0)
1401 throw (new System.NullReferenceException());
1402 if (fromindex > toindex)
1403 throw (new System.ArgumentException());
1404 if ((fromindex < 0) || ((System.Array)array).Length < toindex)
1405 throw (new System.IndexOutOfRangeException());
1406 for (int index = (fromindex > 0) ? fromindex-- : fromindex; index < toindex; index++)
1407 array.SetValue(Temp_Object, index);
1411 /// Fills the array with an specific value.
1413 /// <param name="array">The array to be filled.</param>
1414 /// <param name="val">The value to fill the array with.</param>
1415 public static void FillArray(System.Array array, System.Object val)
1417 FillArray(array, 0, array.Length, val);
1422 /*******************************/
1424 /// This class manages a set of elements.
1426 public class SetSupport : System.Collections.ArrayList
1429 /// Creates a new set.
1431 public SetSupport(): base()
1436 /// Creates a new set initialized with System.Collections.ICollection object
1438 /// <param name="collection">System.Collections.ICollection object to initialize the set object</param>
1439 public SetSupport(System.Collections.ICollection collection): base(collection)
1444 /// Creates a new set initialized with a specific capacity.
1446 /// <param name="capacity">value to set the capacity of the set object</param>
1447 public SetSupport(int capacity): base(capacity)
1452 /// Adds an element to the set.
1454 /// <param name="objectToAdd">The object to be added.</param>
1455 /// <returns>True if the object was added, false otherwise.</returns>
1456 public new virtual bool Add(object objectToAdd)
1458 if (this.Contains(objectToAdd))
1462 base.Add(objectToAdd);
1468 /// Adds all the elements contained in the specified collection.
1470 /// <param name="collection">The collection used to extract the elements that will be added.</param>
1471 /// <returns>Returns true if all the elements were successfuly added. Otherwise returns false.</returns>
1472 public virtual bool AddAll(System.Collections.ICollection collection)
1474 bool result = false;
1475 if (collection!=null)
1477 System.Collections.IEnumerator tempEnumerator = new System.Collections.ArrayList(collection).GetEnumerator();
1478 while (tempEnumerator.MoveNext())
1480 if (tempEnumerator.Current != null)
1481 result = this.Add(tempEnumerator.Current);
1488 /// Adds all the elements contained in the specified support class collection.
1490 /// <param name="collection">The collection used to extract the elements that will be added.</param>
1491 /// <returns>Returns true if all the elements were successfuly added. Otherwise returns false.</returns>
1492 public virtual bool AddAll(CollectionSupport collection)
1494 return this.AddAll((System.Collections.ICollection)collection);
1498 /// Verifies that all the elements of the specified collection are contained into the current collection.
1500 /// <param name="collection">The collection used to extract the elements that will be verified.</param>
1501 /// <returns>True if the collection contains all the given elements.</returns>
1502 public virtual bool ContainsAll(System.Collections.ICollection collection)
1504 bool result = false;
1505 System.Collections.IEnumerator tempEnumerator = collection.GetEnumerator();
1506 while (tempEnumerator.MoveNext())
1507 if (!(result = this.Contains(tempEnumerator.Current)))
1513 /// Verifies if all the elements of the specified collection are contained into the current collection.
1515 /// <param name="collection">The collection used to extract the elements that will be verified.</param>
1516 /// <returns>Returns true if all the elements are contained in the collection. Otherwise returns false.</returns>
1517 public virtual bool ContainsAll(CollectionSupport collection)
1519 return this.ContainsAll((System.Collections.ICollection) collection);
1523 /// Verifies if the collection is empty.
1525 /// <returns>True if the collection is empty, false otherwise.</returns>
1526 public virtual bool IsEmpty()
1528 return (this.Count == 0);
1532 /// Removes an element from the set.
1534 /// <param name="elementToRemove">The element to be removed.</param>
1535 /// <returns>True if the element was removed.</returns>
1536 public new virtual bool Remove(object elementToRemove)
1538 bool result = false;
1539 if (this.Contains(elementToRemove))
1541 base.Remove(elementToRemove);
1546 /// Removes all the elements contained in the specified collection.
1548 /// <param name="collection">The collection used to extract the elements that will be removed.</param>
1549 /// <returns>True if all the elements were successfuly removed, false otherwise.</returns>
1550 public virtual bool RemoveAll(System.Collections.ICollection collection)
1552 bool result = false;
1553 System.Collections.IEnumerator tempEnumerator = collection.GetEnumerator();
1554 while (tempEnumerator.MoveNext())
1556 if ((result == false) && (this.Contains(tempEnumerator.Current)))
1558 this.Remove(tempEnumerator.Current);
1564 /// Removes all the elements contained into the specified collection.
1566 /// <param name="collection">The collection used to extract the elements that will be removed.</param>
1567 /// <returns>Returns true if all the elements were successfuly removed. Otherwise returns false.</returns>
1568 public virtual bool RemoveAll(CollectionSupport collection)
1570 return this.RemoveAll((System.Collections.ICollection) collection);
1574 /// Removes all the elements that aren't contained in the specified collection.
1576 /// <param name="collection">The collection used to verify the elements that will be retained.</param>
1577 /// <returns>True if all the elements were successfully removed, false otherwise.</returns>
1578 public virtual bool RetainAll(System.Collections.ICollection collection)
1580 bool result = false;
1581 System.Collections.IEnumerator tempEnumerator = collection.GetEnumerator();
1582 SetSupport tempSet = (SetSupport)collection;
1583 while (tempEnumerator.MoveNext())
1584 if (!tempSet.Contains(tempEnumerator.Current))
1586 result = this.Remove(tempEnumerator.Current);
1587 tempEnumerator = this.GetEnumerator();
1593 /// Removes all the elements that aren't contained into the specified collection.
1595 /// <param name="collection">The collection used to verify the elements that will be retained.</param>
1596 /// <returns>Returns true if all the elements were successfully removed. Otherwise returns false.</returns>
1597 public virtual bool RetainAll(CollectionSupport collection)
1599 return this.RetainAll((System.Collections.ICollection) collection);
1603 /// Obtains an array containing all the elements of the collection.
1605 /// <returns>The array containing all the elements of the collection.</returns>
1606 public new virtual object[] ToArray()
1609 object[] tempObject= new object[this.Count];
1610 System.Collections.IEnumerator tempEnumerator = this.GetEnumerator();
1611 while (tempEnumerator.MoveNext())
1612 tempObject[index++] = tempEnumerator.Current;
1617 /// Obtains an array containing all the elements in the collection.
1619 /// <param name="objects">The array into which the elements of the collection will be stored.</param>
1620 /// <returns>The array containing all the elements of the collection.</returns>
1621 public virtual object[] ToArray(object[] objects)
1624 System.Collections.IEnumerator tempEnumerator = this.GetEnumerator();
1625 while (tempEnumerator.MoveNext())
1626 objects[index++] = tempEnumerator.Current;
1630 /*******************************/
1632 /// This class manages different operation with collections.
1634 public class AbstractSetSupport : SetSupport
1637 /// The constructor with no parameters to create an abstract set.
1639 public AbstractSetSupport()
1645 /*******************************/
1647 /// Removes the element with the specified key from a Hashtable instance.
1649 /// <param name="hashtable">The Hashtable instance</param>
1650 /// <param name="key">The key of the element to remove</param>
1651 /// <returns>The element removed</returns>
1652 public static System.Object HashtableRemove(System.Collections.Hashtable hashtable, System.Object key)
1654 System.Object element = hashtable[key];
1655 hashtable.Remove(key);
1659 /*******************************/
1661 /// Sets the size of the ArrayList. If the new size is greater than the current capacity, then new null items are added to the end of the ArrayList. If the new size is lower than the current size, then all elements after the new size are discarded
1663 /// <param name="arrayList">The ArrayList to be changed</param>
1664 /// <param name="newSize">The new ArrayList size</param>
1665 public static void SetSize(System.Collections.ArrayList arrayList, int newSize)
1667 if (newSize < 0) throw new System.ArgumentException();
1670 if (newSize < arrayList.Count)
1671 arrayList.RemoveRange(newSize,(arrayList.Count-newSize));
1673 while(newSize > arrayList.Count)
1674 arrayList.Add(null);
1678 /*******************************/
1680 /// Adds an element to the top end of a Stack instance.
1682 /// <param name="stack">The Stack instance</param>
1683 /// <param name="element">The element to add</param>
1684 /// <returns>The element added</returns>
1685 public static System.Object StackPush(System.Collections.Stack stack, System.Object element)
1687 stack.Push(element);
1691 /*******************************/
1693 /// Copies an array of chars obtained from a String into a specified array of chars
1695 /// <param name="sourceString">The String to get the chars from</param>
1696 /// <param name="sourceStart">Position of the String to start getting the chars</param>
1697 /// <param name="sourceEnd">Position of the String to end getting the chars</param>
1698 /// <param name="destinationArray">Array to return the chars</param>
1699 /// <param name="destinationStart">Position of the destination array of chars to start storing the chars</param>
1700 /// <returns>An array of chars</returns>
1701 public static void GetCharsFromString(string sourceString, int sourceStart, int sourceEnd, ref char[] destinationArray, int destinationStart)
1704 int destinationCounter;
1705 sourceCounter = sourceStart;
1706 destinationCounter = destinationStart;
1707 while (sourceCounter < sourceEnd)
1709 destinationArray[destinationCounter] = (char) sourceString[sourceCounter];
1711 destinationCounter++;
1715 /*******************************/
1717 /// Creates an output file stream to write to the file with the specified name.
1719 /// <param name="FileName">Name of the file to write.</param>
1720 /// <param name="Append">True in order to write to the end of the file, false otherwise.</param>
1721 /// <returns>New instance of FileStream with the proper file mode.</returns>
1722 public static System.IO.FileStream GetFileStream(string FileName, bool Append)
1725 return new System.IO.FileStream(FileName, System.IO.FileMode.Append);
1727 return new System.IO.FileStream(FileName, System.IO.FileMode.Create);
1731 /*******************************/
1733 /// Converts an array of sbytes to an array of chars
1735 /// <param name="sByteArray">The array of sbytes to convert</param>
1736 /// <returns>The new array of chars</returns>
1737 [CLSCompliantAttribute(false)]
1738 public static char[] ToCharArray(sbyte[] sByteArray)
1740 char[] charArray = new char[sByteArray.Length];
1741 sByteArray.CopyTo(charArray, 0);
1746 /// Converts an array of bytes to an array of chars
1748 /// <param name="byteArray">The array of bytes to convert</param>
1749 /// <returns>The new array of chars</returns>
1750 public static char[] ToCharArray(byte[] byteArray)
1752 char[] charArray = new char[byteArray.Length];
1753 byteArray.CopyTo(charArray, 0);
1757 /*******************************/
1759 /// Encapsulates the functionality of message digest algorithms such as SHA-1 or MD5.
1761 public class MessageDigestSupport
1763 private System.Security.Cryptography.HashAlgorithm algorithm;
1764 private byte[] data;
1765 private int position;
1766 private string algorithmName;
1769 /// The HashAlgorithm instance that provide the cryptographic hash algorithm
1771 public System.Security.Cryptography.HashAlgorithm Algorithm
1775 return this.algorithm;
1779 this.algorithm = value;
1799 /// The name of the cryptographic hash algorithm used in the instance
1801 public string AlgorithmName
1805 return this.algorithmName;
1810 /// Creates a message digest using the specified name to set Algorithm property.
1812 /// <param name="algorithm">The name of the algorithm to use</param>
1813 public MessageDigestSupport(System.String algorithm)
1815 if (algorithm.Equals("SHA-1"))
1817 this.algorithmName = "SHA";
1821 this.algorithmName = algorithm;
1823 this.Algorithm = (System.Security.Cryptography.HashAlgorithm) System.Security.Cryptography.CryptoConfig.CreateFromName(this.algorithmName);
1828 /// Computes the hash value for the internal data digest.
1830 /// <returns>The array of signed bytes with the resulting hash value</returns>
1831 [CLSCompliantAttribute(false)]
1832 public sbyte[] DigestData()
1834 sbyte[] result = ToSByteArray(this.Algorithm.ComputeHash(this.data));
1840 /// Performs and update on the digest with the specified array and then completes the digest
1843 /// <param name="newData">The array of bytes for final update to the digest</param>
1844 /// <returns>An array of signed bytes with the resulting hash value</returns>
1845 [CLSCompliantAttribute(false)]
1846 public sbyte[] DigestData(byte[] newData)
1848 this.Update(newData);
1849 return this.DigestData();
1853 /// Updates the digest data with the specified array of bytes by making an append
1854 /// operation in the internal array of data.
1856 /// <param name="newData">The array of bytes for the update operation</param>
1857 public void Update(byte[] newData)
1861 this.Data = newData;
1862 this.position = this.Data.Length - 1;
1866 byte[] oldData = this.Data;
1867 this.Data = new byte[newData.Length + position + 1];
1868 oldData.CopyTo(this.Data, 0);
1869 newData.CopyTo(this.Data, oldData.Length);
1871 this.position = this.Data.Length - 1;
1876 /// Updates the digest data with the input byte by calling the method Update with an array.
1878 /// <param name="newData">The input byte for the update</param>
1879 public void Update(byte newData)
1881 byte[] newDataArray = new byte[1];
1882 newDataArray[0] = newData;
1883 this.Update(newDataArray);
1887 /// Updates the specified count of bytes with the input array of bytes starting at the
1890 /// <param name="newData">The array of bytes for the update operation</param>
1891 /// <param name="offset">The initial position to start from in the array of bytes</param>
1892 /// <param name="count">The number of bytes fot the update</param>
1893 public void Update(byte[] newData, int offset, int count)
1895 byte[] newDataArray = new byte[count];
1896 System.Array.Copy(newData, offset, newDataArray, 0, count);
1897 this.Update(newDataArray);
1901 /// Resets the digest data to the initial state.
1910 /// Returns a string representation of the Message Digest
1912 /// <returns>A string representation of the object</returns>
1913 public override string ToString()
1915 return this.Algorithm.ToString();
1919 /// Generates a new instance of the MessageDigestSupport class using the specified algorithm
1921 /// <param name="algorithm">The name of the algorithm to use</param>
1922 /// <returns>A new instance of the MessageDigestSupport class</returns>
1923 public static MessageDigestSupport GetInstance(System.String algorithm)
1925 return new MessageDigestSupport(algorithm);
1929 /// Compares two arrays of signed bytes evaluating equivalence in digest data
1931 /// <param name="firstDigest">An array of signed bytes for comparison</param>
1932 /// <param name="secondDigest">An array of signed bytes for comparison</param>
1933 /// <returns>True if the input digest arrays are equal</returns>
1934 [CLSCompliantAttribute(false)]
1935 public static bool EquivalentDigest(System.SByte[] firstDigest, System.SByte[] secondDigest)
1937 bool result = false;
1938 if (firstDigest.Length == secondDigest.Length)
1942 while(result && index < firstDigest.Length)
1944 result = firstDigest[index] == secondDigest[index];
1953 /*******************************/
1955 /// This class uses a cryptographic Random Number Generator to provide support for
1956 /// strong pseudo-random number generation.
1958 public class SecureRandomSupport
1960 private System.Security.Cryptography.RNGCryptoServiceProvider generator;
1963 /// Initializes a new instance of the random number generator.
1965 public SecureRandomSupport()
1967 this.generator = new System.Security.Cryptography.RNGCryptoServiceProvider();
1971 /// Initializes a new instance of the random number generator with the given seed.
1973 /// <param name="seed">The initial seed for the generator</param>
1974 public SecureRandomSupport(byte[] seed)
1976 this.generator = new System.Security.Cryptography.RNGCryptoServiceProvider(seed);
1980 /// Returns an array of bytes with a sequence of cryptographically strong random values
1982 /// <param name="randomnumbersarray">The array of bytes to fill</param>
1983 [CLSCompliantAttribute(false)]
1984 public sbyte[] NextBytes(byte[] randomnumbersarray)
1986 this.generator.GetBytes(randomnumbersarray);
1987 return ToSByteArray(randomnumbersarray);
1991 /// Returns the given number of seed bytes generated for the first running of a new instance
1992 /// of the random number generator
1994 /// <param name="numberOfBytes">Number of seed bytes to generate</param>
1995 /// <returns>Seed bytes generated</returns>
1996 public static byte[] GetSeed(int numberOfBytes)
1998 System.Security.Cryptography.RNGCryptoServiceProvider generatedSeed = new System.Security.Cryptography.RNGCryptoServiceProvider();
1999 byte[] seeds = new byte[numberOfBytes];
2000 generatedSeed.GetBytes(seeds);
2005 /// Creates a new instance of the random number generator with the seed provided by the user
2007 /// <param name="newSeed">Seed to create a new random number generator</param>
2008 public void SetSeed(byte[] newSeed)
2010 this.generator = new System.Security.Cryptography.RNGCryptoServiceProvider(newSeed);
2014 /// Creates a new instance of the random number generator with the seed provided by the user
2016 /// <param name="newSeed">Seed to create a new random number generator</param>
2017 public void SetSeed(long newSeed)
2019 byte[] bytes = new byte[8];
2020 for (int index= 7; index > 0 ; index--)
2022 bytes[index] = (byte)(newSeed - (long)((newSeed >> 8) << 8));
2023 newSeed = (long)(newSeed >> 8);
2029 /*******************************/
2031 /// Interface used by classes which must be single threaded.
2033 public interface SingleThreadModel
2038 /*******************************/
2040 /// Creates an instance of a received Type.
2042 /// <param name="classType">The Type of the new class instance to return.</param>
2043 /// <returns>An Object containing the new instance.</returns>
2044 public static System.Object CreateNewInstance(System.Type classType)
2046 System.Object instance = null;
2047 System.Type[] constructor = new System.Type[]{};
2048 System.Reflection.ConstructorInfo[] constructors = null;
2050 constructors = classType.GetConstructors();
2052 if (constructors.Length == 0)
2053 throw new System.UnauthorizedAccessException();
2056 for(int i = 0; i < constructors.Length; i++)
2058 System.Reflection.ParameterInfo[] parameters = constructors[i].GetParameters();
2060 if (parameters.Length == 0)
2062 instance = classType.GetConstructor(constructor).Invoke(new System.Object[]{});
2065 else if (i == constructors.Length -1)
2066 throw new System.MethodAccessException();
2073 /*******************************/
2075 /// Writes the exception stack trace to the received stream
2077 /// <param name="throwable">Exception to obtain information from</param>
2078 /// <param name="stream">Output sream used to write to</param>
2079 public static void WriteStackTrace(System.Exception throwable, System.IO.TextWriter stream)
2081 stream.Write(throwable.StackTrace);
2085 /*******************************/
2087 /// Determines whether two Collections instances are equals.
2089 /// <param name="source">The first Collections to compare. </param>
2090 /// <param name="target">The second Collections to compare. </param>
2091 /// <returns>Return true if the first collection is the same instance as the second collection, otherwise return false.</returns>
2092 public static bool EqualsSupport(System.Collections.ICollection source, System.Collections.ICollection target )
2094 System.Collections.IEnumerator sourceEnumerator = ReverseStack(source);
2095 System.Collections.IEnumerator targetEnumerator = ReverseStack(target);
2097 if (source.Count != target.Count)
2099 while(sourceEnumerator.MoveNext() && targetEnumerator.MoveNext())
2100 if (!sourceEnumerator.Current.Equals(targetEnumerator.Current))
2106 /// Determines if a Collection is equal to the Object.
2108 /// <param name="source">The first Collections to compare.</param>
2109 /// <param name="target">The Object to compare.</param>
2110 /// <returns>Return true if the first collection contains the same values of the second Object, otherwise return false.</returns>
2111 public static bool EqualsSupport(System.Collections.ICollection source, System.Object target)
2113 if((target.GetType())!= (typeof(System.Collections.ICollection)))
2116 return EqualsSupport(source,(System.Collections.ICollection)target);
2120 /// Determines if a IDictionaryEnumerator is equal to the Object.
2122 /// <param name="source">The first IDictionaryEnumerator to compare.</param>
2123 /// <param name="target">The second Object to compare.</param>
2124 /// <returns>Return true if the first IDictionaryEnumerator contains the same values of the second Object, otherwise return false.</returns>
2125 public static bool EqualsSupport(System.Collections.IDictionaryEnumerator source, System.Object target)
2127 if((target.GetType())!= (typeof(System.Collections.IDictionaryEnumerator)))
2130 return EqualsSupport(source,(System.Collections.IDictionaryEnumerator)target);
2134 /// Determines whether two IDictionaryEnumerator instances are equals.
2136 /// <param name="source">The first IDictionaryEnumerator to compare.</param>
2137 /// <param name="target">The second IDictionaryEnumerator to compare.</param>
2138 /// <returns>Return true if the first IDictionaryEnumerator contains the same values as the second IDictionaryEnumerator, otherwise return false.</returns>
2139 public static bool EqualsSupport(System.Collections.IDictionaryEnumerator source, System.Collections.IDictionaryEnumerator target )
2141 while(source.MoveNext() && target.MoveNext())
2142 if (source.Key.Equals(target.Key))
2143 if(source.Value.Equals(target.Value))
2149 /// Reverses the Stack Collection received.
2151 /// <param name="collection">The collection to reverse.</param>
2152 /// <returns>The collection received in reverse order if it was a System.Collections.Stack type, otherwise it does
2153 /// nothing to the collection.</returns>
2154 public static System.Collections.IEnumerator ReverseStack(System.Collections.ICollection collection)
2156 if((collection.GetType()) == (typeof(System.Collections.Stack)))
2158 System.Collections.ArrayList collectionStack = new System.Collections.ArrayList(collection);
2159 collectionStack.Reverse();
2160 return collectionStack.GetEnumerator();
2163 return collection.GetEnumerator();