Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.IO;
- using System.Text;
- namespace KiwiIO
- {
- //feel free to browse around, check everything out :D big ass code.
- public class FileIO
- {
- #region Fields
- private String fileName;
- private FileStream handle;
- private Stream stream;
- public Endian endian
- {
- set
- {
- this.endian = value;
- }
- get
- {
- return this.endian;
- }
- }
- public Int64 Position
- {
- set
- {
- try
- {
- this.stream.Position = value;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- get
- {
- return this.stream.Position;
- }
- }
- public Int64 Length
- {
- get
- {
- return this.stream.Position;
- }
- }
- public Int32 ReadTimeout
- {
- set
- {
- try
- {
- this.stream.ReadTimeout = value;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- get
- {
- return this.stream.ReadTimeout;
- }
- }
- public Int32 WriteTimeout
- {
- set
- {
- try
- {
- this.stream.WriteTimeout = value;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- get
- {
- return this.stream.WriteTimeout;
- }
- }
- #endregion
- #region Instansiation
- public FileIO()
- {
- this.stream = null;
- this.handle = null;
- this.endian = Endian.Little;
- this.fileName = null;
- }
- public FileIO(FileStream handle, Endian endian)
- {
- this.fileName = handle.Name;
- this.handle = handle;
- this.stream = handle;
- this.endian = endian;
- }
- public FileIO(Stream stream, Endian endian)
- {
- this.handle = (FileStream)stream;
- this.fileName = this.handle.Name;
- this.stream = stream;
- this.endian = endian;
- }
- public FileIO(string fileName, Endian endian)
- {
- this.fileName = fileName;
- this.handle = new FileStream(this.fileName, FileMode.Open, FileAccess.ReadWrite);
- this.stream = this.handle;
- this.endian = endian;
- }
- #endregion
- #region Misc Methods
- public Int64 FindASCIIString(String str)
- {
- try
- {
- byte[] bytes = ASCIIEncoding.ASCII.GetBytes(str);
- long address = FindBytes(bytes);
- return address;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public Int64 FindUnicodeString(String str)
- {
- try
- {
- char[] strc = str.ToCharArray();
- char[] tempStrc = new char[(strc.Length * 2)];
- for (int x = 0; x < tempStrc.Length; x++)
- if (x != tempStrc.Length - 1)
- tempStrc[x] = '\0';
- for (int x = 0; x < tempStrc.Length; x += 2)
- {
- if (x < tempStrc.Length - 1)
- tempStrc[x] = strc[x / 2];
- }
- str = new String(tempStrc);
- byte[] bytes = ASCIIEncoding.ASCII.GetBytes(str);
- long address = FindBytes(bytes);
- return address;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public Int64 FindBytes(Byte[] value)
- {
- try
- {
- Byte[] FBytes = File.ReadAllBytes(this.fileName);
- Int64 i, j;
- Int64 bufferlen = FBytes.Length;
- Int64 valuelen = value.Length;
- for (i = 0; i < bufferlen; i++)
- {
- for (j = 0; j < valuelen; j++)
- {
- if (FBytes[i + j] != value[j])
- break;
- else if ((j + 1) == valuelen)
- return i;
- }
- }
- }
- catch (Exception ex)
- {
- throw ex;
- }
- return -1;
- }
- public void SetStream(Stream stream)
- {
- this.stream = stream;
- }
- private void SwapByteSex(byte[] bytes)
- {
- try
- {
- Array.Reverse(bytes);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public void Seek(Int64 offset)
- {
- this.Seek(offset, SeekOrigin.Current);
- }
- public void Seek(Int64 offset, SeekOrigin origin)
- {
- try
- {
- this.stream.Seek(offset, origin);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public void Close()
- {
- this.stream.Close();
- }
- public void Dispose()
- {
- this.stream.Dispose();
- }
- public void Flush()
- {
- this.stream.Flush();
- }
- public void SetLength(long length)
- {
- try
- {
- this.stream.SetLength(length);
- }
- catch (Exception x)
- {
- throw x;
- }
- }
- #endregion
- #region Reader
- public bool ReadBoolean()
- {
- try
- {
- return Conversions.BytesToBoolean(this.ReadByte());
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public byte ReadByte()
- {
- try
- {
- return this.ReadBytes(1)[0];
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public byte[] ReadBytes(int count)
- {
- try
- {
- byte[] bytes = new byte[count];
- this.stream.Read(bytes, 0, count);
- if (this.endian == Endian.Big)
- SwapByteSex(bytes);
- return bytes;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public byte[] ReadBytes(int count, Endian _endian)
- {
- try
- {
- byte[] bytes = new byte[count];
- this.stream.Read(bytes, 0, count);
- if (_endian == Endian.Big)
- SwapByteSex(bytes);
- return bytes;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public double ReadDouble()
- {
- try
- {
- byte[] bytes = new byte[8];
- this.stream.Read(bytes, 0, 8);
- return Conversions.BytesToDouble(bytes);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public short ReadInt16()
- {
- try
- {
- byte[] bytes = new byte[2];
- this.stream.Read(bytes, 0, 2);
- return Conversions.BytesToInt16(bytes);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public int ReadInt24()
- {
- try
- {
- byte[] bytes = new byte[3];
- this.stream.Read(bytes, 0, 3);
- return Conversions.BytesToInt24(bytes);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public int ReadInt32()
- {
- try
- {
- byte[] bytes = new byte[4];
- this.stream.Read(bytes, 0, 4);
- return Conversions.BytesToInt32(bytes);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public long ReadInt40()
- {
- try
- {
- byte[] bytes = new byte[5];
- this.stream.Read(bytes, 0, 5);
- return Conversions.BytesToInt40(bytes);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public long ReadInt48()
- {
- try
- {
- byte[] bytes = new byte[6];
- this.stream.Read(bytes, 0, 6);
- return Conversions.BytesToInt48(bytes);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public long ReadInt56()
- {
- try
- {
- byte[] bytes = new byte[7];
- this.stream.Read(bytes, 0, 7);
- return Conversions.BytesToInt56(bytes);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public long ReadInt64()
- {
- try
- {
- byte[] bytes = new byte[8];
- this.stream.Read(bytes, 0, 8);
- return Conversions.BytesToInt64(bytes);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public float ReadSingle()
- {
- try
- {
- byte[] bytes = new byte[4];
- this.stream.Read(bytes, 0, 4);
- return Conversions.BytesToSingle(bytes);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public ushort ReadUInt16()
- {
- try
- {
- byte[] bytes = new byte[2];
- this.stream.Read(bytes, 0, 2);
- return Conversions.BytesToUInt16(bytes);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public uint ReadUInt32()
- {
- try
- {
- byte[] bytes = new byte[4];
- this.stream.Read(bytes, 0, 4);
- return Conversions.BytesToUInt32(bytes);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public ulong ReadUInt64()
- {
- try
- {
- byte[] bytes = new byte[8];
- this.stream.Read(bytes, 0, 8);
- return Conversions.BytesToUInt64(bytes);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- #endregion
- #region Writer
- public void Write(byte[] value)
- {
- try
- {
- if (this.endian == Endian.Big)
- {
- this.SwapByteSex(value);
- }
- this.stream.Write(value, 0, value.Length);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public void Write(byte[] value, Endian _endian)
- {
- try
- {
- if (_endian == Endian.Big)
- {
- this.SwapByteSex(value);
- }
- this.stream.Write(value, 0, value.Length);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public void Write(byte[] value, int count)
- {
- try
- {
- if (this.endian == Endian.Big)
- {
- this.SwapByteSex(value);
- }
- this.stream.Write(value, 0, count);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public void Write(byte[] value, Endian _endian, int offset)
- {
- try
- {
- if (_endian == Endian.Big)
- {
- this.SwapByteSex(value);
- }
- this.stream.Write(value, offset, value.Length);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public void Write(byte[] value, int count, Endian _endian)
- {
- try
- {
- if (_endian == Endian.Big)
- {
- this.SwapByteSex(value);
- }
- this.stream.Write(value, 0, count);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public void Write(byte[] value, int offset, int count, Endian _endian)
- {
- try
- {
- if (_endian == Endian.Big)
- {
- this.SwapByteSex(value);
- }
- this.stream.Write(value, offset, count);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public void WriteASCIIString(string value)
- {
- try
- {
- byte[] bytes = Conversions.StringToBytes(value);
- this.Write(bytes);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public void Write(bool value)
- {
- try
- {
- byte[] bytes = Conversions.BooleanToBytes(value);
- this.Write(bytes);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public void WriteByte(byte value)
- {
- try
- {
- this.stream.WriteByte(value);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public void Write(char value)
- {
- try
- {
- byte _byte = Convert.ToByte(value);
- this.WriteByte(_byte);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public void Write(double value)
- {
- try
- {
- byte[] bytes = Conversions.DoubleToBytes(value);
- this.Write(bytes);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public void Write(short value)
- {
- try
- {
- byte[] bytes = Conversions.Int16ToBytes(value);
- this.Write(bytes);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public void Write(int value)
- {
- try
- {
- byte[] bytes = Conversions.Int32ToBytes(value);
- this.Write(bytes);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public void WriteInt40(long value)
- {
- try
- {
- byte[] bytes = Conversions.Int40ToBytes(value);
- this.Write(bytes);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public void Write(long value)
- {
- try
- {
- byte[] bytes = Conversions.Int64ToBytes(value);
- this.Write(bytes);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public void Write(sbyte value)
- {
- try
- {
- byte bytes = Convert.ToByte(value);
- this.WriteByte(bytes);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public void Write(float value)
- {
- try
- {
- byte[] bytes = Conversions.SingleToBytes(value);
- this.Write(bytes);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public void Write(ushort value)
- {
- try
- {
- byte[] bytes = Conversions.UInt16ToBytes(value);
- this.Write(bytes);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public void Write(uint value)
- {
- try
- {
- byte[] bytes = Conversions.UInt32ToBytes(value);
- this.Write(bytes);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public void Write(ulong value)
- {
- try
- {
- byte[] bytes = Conversions.UInt64ToBytes(value);
- this.Write(bytes);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public void WriteUnicodeString(String str)
- {
- try
- {
- int Length = str.Length;
- for (int i = 0; i < Length; i++)
- {
- this.Write(Conversions.CharToBytes(str[i]));
- if (Length - 1 > i)
- this.Write(Conversions.CharToBytes('\0'));
- }
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- #endregion
- #region Converter
- internal static class Conversions
- {
- #region BytesToType
- public static Int16 BytesToInt16(Byte[] bytes)
- {
- if (bytes.Length == 2)
- {
- try
- {
- Int16 intt = BitConverter.ToInt16(bytes, 0);
- return intt;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- else
- throw new System.InvalidOperationException("Invalid value size.");
- }
- public static UInt16 BytesToUInt16(Byte[] bytes)
- {
- if (bytes.Length == 2)
- {
- try
- {
- UInt16 intt = BitConverter.ToUInt16(bytes, 0);
- return intt;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- else
- throw new System.InvalidOperationException("Invalid value size.");
- }
- public static Int32 BytesToInt24(Byte[] bytes) //Byte[] must be 3 bytes in size
- {
- if (bytes.Length == 3)
- {
- try
- {
- return ((bytes[2] << 16
- | bytes[1] << 8
- | bytes[0]));
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- else
- throw new System.InvalidOperationException("Invalid value size.");
- }
- public static Int32 BytesToInt32(Byte[] bytes)
- {
- if (bytes.Length == 4)
- {
- try
- {
- Int32 intt = BitConverter.ToInt32(bytes, 0);
- return intt;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- else
- throw new System.InvalidOperationException("Invalid value size.");
- }
- public static UInt32 BytesToUInt32(Byte[] bytes)
- {
- if (bytes.Length == 4)
- {
- try
- {
- UInt32 intt = BitConverter.ToUInt32(bytes, 0);
- return intt;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- else
- throw new System.InvalidOperationException("Invalid value size.");
- }
- public static Int64 BytesToInt40(Byte[] bytes) //Byte[] must be 5 bytes in size
- {
- if (bytes.Length == 5)
- {
- try
- {
- return ((bytes[4] << 32
- | bytes[3] << 24
- | bytes[2] << 16
- | bytes[1] << 8
- | bytes[0]));
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- else
- throw new System.InvalidOperationException("Invalid value size.");
- }
- public static Int64 BytesToInt48(Byte[] bytes) //Byte[] must be 6 bytes in size
- {
- if (bytes.Length == 6)
- {
- try
- {
- return ((bytes[5] << 40
- | bytes[4] << 32
- | bytes[3] << 24
- | bytes[2] << 16
- | bytes[1] << 8
- | bytes[0]));
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- else
- throw new System.InvalidOperationException("Invalid value size.");
- }
- public static Int64 BytesToInt56(Byte[] bytes) //Byte[] must be 7 bytes in size
- {
- if (bytes.Length == 7)
- {
- try
- {
- return ((bytes[6] << 48
- | bytes[5] << 40
- | bytes[4] << 32
- | bytes[3] << 24
- | bytes[2] << 16
- | bytes[1] << 8
- | bytes[0]));
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- else
- throw new System.InvalidOperationException("Invalid value size.");
- }
- public static Int64 BytesToInt64(Byte[] bytes)
- {
- if (bytes.Length == 8)
- {
- try
- {
- Int64 intt = BitConverter.ToInt64(bytes, 0);
- return intt;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- else
- throw new System.InvalidOperationException("Invalid value size.");
- }
- public static UInt64 BytesToUInt64(Byte[] bytes)
- {
- if (bytes.Length == 8)
- {
- try
- {
- UInt64 intt = BitConverter.ToUInt64(bytes, 0);
- return intt;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- else
- throw new System.InvalidOperationException("Invalid value size.");
- }
- public static Double BytesToDouble(Byte[] bytes)
- {
- if (bytes.Length == 8)
- {
- try
- {
- Double dub = BitConverter.ToDouble(bytes, 0);
- return dub;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- else
- throw new System.InvalidOperationException("Invalid value size.");
- }
- public static Single BytesToSingle(Byte[] bytes)
- {
- if (bytes.Length == 4)
- {
- Single dub = BitConverter.ToSingle(bytes, 0);
- return dub;
- }
- else
- throw new System.InvalidOperationException("Invalid value size.");
- }
- public static Boolean BytesToBoolean(Byte _byte)
- {
- try
- {
- Boolean boolz = BitConverter.ToBoolean(new byte[] { _byte }, 0);
- return boolz;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public static String BytesToString(Byte[] bytes)
- {
- try
- {
- String str = BitConverter.ToString(bytes);
- return str;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- #endregion
- #region TypeToBytes
- public static byte[] Int16ToBytes(Int16 value)
- {
- try
- {
- byte[] bytes = BitConverter.GetBytes(value);
- return bytes;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public static byte[] UInt16ToBytes(UInt16 value)
- {
- try
- {
- byte[] bytes = BitConverter.GetBytes(value);
- return bytes;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public static byte[] Int24ToBytes(Int32 value)
- {
- if (8388607 >= value && -8388607 <= value)
- {
- try
- {
- Byte[] bytes = BitConverter.GetBytes(value);
- Byte[] newBytes = { bytes[0], bytes[1], bytes[2] };
- return newBytes;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- else
- throw new System.InvalidOperationException("Invalid value size.");
- }
- public static byte[] Int32ToBytes(Int32 value)
- {
- try
- {
- byte[] bytes = BitConverter.GetBytes(value);
- return bytes;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public static byte[] UInt32ToBytes(UInt32 value)
- {
- try
- {
- byte[] bytes = BitConverter.GetBytes(value);
- return bytes;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public static byte[] Int40ToBytes(Int64 value)
- {
- if (549755813887 >= value && -549755813887 <= value)
- {
- try
- {
- Byte[] bytes = BitConverter.GetBytes(value);
- Byte[] newBytes = { bytes[0], bytes[1], bytes[2], bytes[3], bytes[4] };
- return newBytes;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- else
- throw new System.InvalidOperationException("Invalid value size.");
- }
- public static byte[] Int48ToBytes(Int64 value)
- {
- if (140737488355327 >= value && -140737488355327 <= value)
- {
- try
- {
- Byte[] bytes = BitConverter.GetBytes(value);
- Byte[] newBytes = { bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5] };
- return newBytes;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- else
- throw new System.InvalidOperationException("Invalid value size.");
- }
- public static byte[] Int56ToBytes(Int64 value)
- {
- if (36028797018963967 >= value && -36028797018963967 <= value)
- {
- try
- {
- Byte[] bytes = BitConverter.GetBytes(value);
- Byte[] newBytes = { bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6] };
- return newBytes;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- else
- throw new System.InvalidOperationException("Invalid value size.");
- }
- public static byte[] Int64ToBytes(Int64 value)
- {
- try
- {
- byte[] bytes = BitConverter.GetBytes(value);
- return bytes;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public static byte[] UInt64ToBytes(UInt64 value)
- {
- try
- {
- byte[] bytes = BitConverter.GetBytes(value);
- return bytes;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public static byte[] DoubleToBytes(Double value)
- {
- try
- {
- byte[] bytes = BitConverter.GetBytes(value);
- return bytes;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public static byte[] SingleToBytes(Single value)
- {
- try
- {
- byte[] bytes = BitConverter.GetBytes(value);
- return bytes;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public static byte[] BooleanToBytes(Boolean value)
- {
- try
- {
- byte[] bytes = BitConverter.GetBytes(value);
- return bytes;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public static byte[] CharToBytes(Char value)
- {
- try
- {
- byte[] bytes = BitConverter.GetBytes(value);
- return bytes;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public static byte[] StringToBytes(String value)
- {
- try
- {
- char[] chars = value.ToCharArray();
- byte[] bytes = new byte[chars.Length];
- int counter = 0;
- byte[] temp;
- foreach (char c in chars)
- {
- temp = BitConverter.GetBytes(c);
- bytes[counter] = temp[0];
- counter++;
- }
- return bytes;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- #endregion
- }
- #endregion
- #region Special Writer
- public void Write(Object value, SpecialInts _intType)
- {
- try
- {
- if (_intType == SpecialInts.Int24)
- {
- byte[] bytes = Conversions.Int24ToBytes((Int32)value);
- this.Write(bytes);
- }
- else if (_intType == SpecialInts.Int40)
- {
- byte[] bytes = Conversions.Int48ToBytes((Int64)value);
- this.Write(bytes);
- }
- else if (_intType == SpecialInts.Int48)
- {
- byte[] bytes = Conversions.Int48ToBytes((Int64)value);
- this.Write(bytes);
- }
- else if (_intType == SpecialInts.Int56)
- {
- byte[] bytes = Conversions.Int56ToBytes((Int64)value);
- this.Write(bytes);
- }
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- #endregion
- }
- public enum Endian
- {
- Big,
- Little
- }
- public enum SpecialInts
- {
- Int24,
- Int40,
- Int48,
- Int56
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement