Public feed
#377304
//
// Java support functions
//
using System;
/// <summary>
/// Contains conversion support elements such as classes, interfaces and static methods.
/// </summary>
public class SupportClass
{
/*******************************/
/// <summary>
/// Provides support functions to create read-write random acces files and write functions
/// </summary>
public class RandomAccessFileSupport
{
/// <summary>
/// Creates a new random acces stream with read-write or read rights
/// </summary>
/// <param name="fileName">A relative or absolute path for the file to open</param>
/// <param name="mode">Mode to open the file in</param>
/// <returns>The new System.IO.FileStream</returns>
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;
}
/// <summary>
/// Creates a new random acces stream with read-write or read rights
/// </summary>
/// <param name="fileName">File infomation for the file to open</param>
/// <param name="mode">Mode to open the file in</param>
/// <returns>The new System.IO.FileStream</returns>
public static System.IO.FileStream CreateRandomAccessFile(System.IO.FileInfo fileName, System.String mode)
{
return CreateRandomAccessFile(fileName.FullName, mode);
}
/// <summary>
/// Writes the data to the specified file stream
/// </summary>
/// <param name="data">Data to write</param>
/// <param name="fileStream">File to write to</param>
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++]);
}
/// <summary>
/// Writes the received string to the file stream
/// </summary>
/// <param name="data">String of information to write</param>
/// <param name="fileStream">File to write to</param>
public static void WriteChars(System.String data,System.IO.FileStream fileStream)
{
WriteBytes(data, fileStream);
}
/// <summary>
/// Writes the received data to the file stream
/// </summary>
/// <param name="sByteArray">Data to write</param>
/// <param name="fileStream">File to write to</param>
public static void WriteRandomFile(sbyte[] sByteArray,System.IO.FileStream fileStream)
{
byte[] byteArray = ToByteArray(sByteArray);
fileStream.Write(byteArray, 0, byteArray.Length);
}
}
/*******************************/
/// <summary>
/// Converts an array of sbytes to an array of bytes
/// </summary>
/// <param name="sbyteArray">The array of sbytes to be converted</param>
/// <returns>The new array of bytes</returns>
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;
}
/// <summary>
/// Converts a string to an array of bytes
/// </summary>
/// <param name="sourceString">The string to be converted</param>
/// <returns>The new array of bytes</returns>
public static byte[] ToByteArray(System.String sourceString)
{
return System.Text.UTF8Encoding.UTF8.GetBytes(sourceString);
}
/// <summary>
/// Converts a array of object-type instances to a byte-type array.
/// </summary>
/// <param name="tempObjectArray">Array to convert.</param>
/// <returns>An array of byte type elements.</returns>
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;
}
/*******************************/
/// <summary>Reads a number of characters from the current source Stream and writes the data to the target array at the specified index.</summary>
/// <param name="sourceStream">The source Stream to read from.</param>
/// <param name="target">Contains the array of characteres read from the source Stream.</param>
/// <param name="start">The starting index of the target array.</param>
/// <param name="count">The maximum number of characters to read from the source Stream.</param>
/// <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>
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;
}
/// <summary>Reads a number of characters from the current source TextReader and writes the data to the target array at the specified index.</summary>
/// <param name="sourceTextReader">The source TextReader to read from</param>
/// <param name="target">Contains the array of characteres read from the source TextReader.</param>
/// <param name="start">The starting index of the target array.</param>
/// <param name="count">The maximum number of characters to read from the source TextReader.</param>
/// <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>
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<start+bytesRead; index++)
target[index] = (sbyte)charArray[index];
return bytesRead;
}
/*******************************/
/// <summary>
/// This class manages array operations.
/// </summary>
public class ArraySupport
{
/// <summary>
/// Compares the entire members of one array whith the other one.
/// </summary>
/// <param name="array1">The array to be compared.</param>
/// <param name="array2">The array to be compared with.</param>
/// <returns>True if both arrays are equals otherwise it returns false.</returns>
/// <remarks>Two arrays are equal if they contains the same elements in the same order.</remarks>
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;
}
/// <summary>
/// Fills the array with an specific value from an specific index to an specific index.
/// </summary>
/// <param name="array">The array to be filled.</param>
/// <param name="fromindex">The first index to be filled.</param>
/// <param name="toindex">The last index to be filled.</param>
/// <param name="val">The value to fill the array with.</param>
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);
}
/// <summary>
/// Fills the array with an specific value.
/// </summary>
/// <param name="array">The array to be filled.</param>
/// <param name="val">The value to fill the array with.</param>
public static void Fill(System.Array array, System.Object val)
{
Fill(array, 0, array.Length, val);
}
}
/*******************************/
/// <summary>
/// Checks if the giving File instance is a directory or file, and returns his Length
/// </summary>
/// <param name="file">The File instance to check</param>
/// <returns>The length of the file</returns>
public static long FileLength(System.IO.FileInfo file)
{
if (file.Exists)
return file.Length;
else
return 0;
}
}