// // Java support functions // using System; /// /// Contains conversion support elements such as classes, interfaces and static methods. /// public class SupportClass { /*******************************/ /// /// Provides support functions to create read-write random acces files and write functions /// public class RandomAccessFileSupport { /// /// Creates a new random acces stream with read-write or read rights /// /// A relative or absolute path for the file to open /// Mode to open the file in /// The new System.IO.FileStream public static System.IO.FileStream CreateRandomAccessFile(System.String fileName, System.String mode) { System.IO.FileStream newFile = null; if (mode.CompareTo("rw") == 0) newFile = new System.IO.FileStream(fileName, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite); else if (mode.CompareTo("r") == 0 ) newFile = new System.IO.FileStream(fileName, System.IO.FileMode.Open, System.IO.FileAccess.Read); else throw new System.ArgumentException(); return newFile; } /// /// Creates a new random acces stream with read-write or read rights /// /// File infomation for the file to open /// Mode to open the file in /// The new System.IO.FileStream public static System.IO.FileStream CreateRandomAccessFile(System.IO.FileInfo fileName, System.String mode) { return CreateRandomAccessFile(fileName.FullName, mode); } /// /// Writes the data to the specified file stream /// /// Data to write /// File to write to public static void WriteBytes(System.String data,System.IO.FileStream fileStream) { int index = 0; int length = data.Length; while(index < length) fileStream.WriteByte((byte)data[index++]); } /// /// Writes the received string to the file stream /// /// String of information to write /// File to write to public static void WriteChars(System.String data,System.IO.FileStream fileStream) { WriteBytes(data, fileStream); } /// /// Writes the received data to the file stream /// /// Data to write /// File to write to public static void WriteRandomFile(sbyte[] sByteArray,System.IO.FileStream fileStream) { byte[] byteArray = ToByteArray(sByteArray); fileStream.Write(byteArray, 0, byteArray.Length); } } /*******************************/ /// /// Converts an array of sbytes to an array of bytes /// /// The array of sbytes to be converted /// The new array of bytes public static byte[] ToByteArray(sbyte[] sbyteArray) { byte[] byteArray = null; if (sbyteArray != null) { byteArray = new byte[sbyteArray.Length]; for(int index=0; index < sbyteArray.Length; index++) byteArray[index] = (byte) sbyteArray[index]; } return byteArray; } /// /// Converts a string to an array of bytes /// /// The string to be converted /// The new array of bytes public static byte[] ToByteArray(System.String sourceString) { return System.Text.UTF8Encoding.UTF8.GetBytes(sourceString); } /// /// Converts a array of object-type instances to a byte-type array. /// /// Array to convert. /// An array of byte type elements. public static byte[] ToByteArray(System.Object[] tempObjectArray) { byte[] byteArray = null; if (tempObjectArray != null) { byteArray = new byte[tempObjectArray.Length]; for (int index = 0; index < tempObjectArray.Length; index++) byteArray[index] = (byte)tempObjectArray[index]; } return byteArray; } /*******************************/ /// Reads a number of characters from the current source Stream and writes the data to the target array at the specified index. /// The source Stream to read from. /// Contains the array of characteres read from the source Stream. /// The starting index of the target array. /// The maximum number of characters to read from the source Stream. /// 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. public static System.Int32 ReadInput(System.IO.Stream sourceStream, sbyte[] target, int start, int count) { // Returns 0 bytes if not enough space in target if (target.Length == 0) return 0; byte[] receiver = new byte[target.Length]; int bytesRead = sourceStream.Read(receiver, start, count); // Returns -1 if EOF if (bytesRead == 0) return -1; for(int i = start; i < start + bytesRead; i++) target[i] = (sbyte)receiver[i]; return bytesRead; } /// Reads a number of characters from the current source TextReader and writes the data to the target array at the specified index. /// The source TextReader to read from /// Contains the array of characteres read from the source TextReader. /// The starting index of the target array. /// The maximum number of characters to read from the source TextReader. /// 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. public static System.Int32 ReadInput(System.IO.TextReader sourceTextReader, sbyte[] target, int start, int count) { // Returns 0 bytes if not enough space in target if (target.Length == 0) return 0; char[] charArray = new char[target.Length]; int bytesRead = sourceTextReader.Read(charArray, start, count); // Returns -1 if EOF if (bytesRead == 0) return -1; for(int index=start; index /// This class manages array operations. /// public class ArraySupport { /// /// Compares the entire members of one array whith the other one. /// /// The array to be compared. /// The array to be compared with. /// True if both arrays are equals otherwise it returns false. /// Two arrays are equal if they contains the same elements in the same order. public static bool Equals(System.Array array1, System.Array array2) { bool result = false; if ((array1 == null) && (array2 == null)) result = true; else if ((array1 != null) && (array2 != null)) { if (array1.Length == array2.Length) { int length = array1.Length; result = true; for (int index = 0; index < length; index++) { if (!(array1.GetValue(index).Equals(array2.GetValue(index)))) { result = false; break; } } } } return result; } /// /// Fills the array with an specific value from an specific index to an specific index. /// /// The array to be filled. /// The first index to be filled. /// The last index to be filled. /// The value to fill the array with. public static void Fill(System.Array array, System.Int32 fromindex, System.Int32 toindex, System.Object val) { System.Object Temp_Object = val; System.Type elementtype = array.GetType().GetElementType(); if (elementtype != val.GetType()) Temp_Object = System.Convert.ChangeType(val, elementtype); if (array.Length == 0) throw (new System.NullReferenceException()); if (fromindex > toindex) throw (new System.ArgumentException()); if ((fromindex < 0) || ((System.Array)array).Length < toindex) throw (new System.IndexOutOfRangeException()); for (int index = (fromindex > 0) ? fromindex-- : fromindex; index < toindex; index++) array.SetValue(Temp_Object, index); } /// /// Fills the array with an specific value. /// /// The array to be filled. /// The value to fill the array with. public static void Fill(System.Array array, System.Object val) { Fill(array, 0, array.Length, val); } } /*******************************/ /// /// Checks if the giving File instance is a directory or file, and returns his Length /// /// The File instance to check /// The length of the file public static long FileLength(System.IO.FileInfo file) { if (file.Exists) return file.Length; else return 0; } }