Skip to content

Instantly share code, notes, and snippets.

@mjs3339
Last active April 7, 2018 01:19
Show Gist options
  • Save mjs3339/b7f60cc3d8b50025c74f6286012ca780 to your computer and use it in GitHub Desktop.
Save mjs3339/b7f60cc3d8b50025c74f6286012ca780 to your computer and use it in GitHub Desktop.
C# Alternate Binary Writer/Reader enhance usability and ease coding complexity.
public class Reader : IDisposable
{
/// <inheritdoc />
/// <summary>
/// Initializes a new instance of the Reader class based on a specified path name, and defaulting to UTF-8 encoding.
/// </summary>
public Reader(string path) : this(path, new UTF8Encoding(false, true))
{
}
/// <summary>
/// Initializes a new instance of the Reader class based on a specified path name, a specified encoding.
/// </summary>
public Reader(string path, Encoding encoding)
{
if (string.IsNullOrEmpty(path))
throw new Exception($"Path {path} cannot be null or empty.");
try
{
File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.Normal);
BaseStream = new FileStream(path, FileMode.Open, FileAccess.Read);
if (BaseStream?.CanRead == true)
BaseReader = new BinaryReader(BaseStream, encoding);
else throw new Exception($"The FileStream for path:{path} is null.");
}
catch (Exception e)
{
throw new Exception($"Error: {e.Message}");
}
}
public Reader(Stream strm) : this(strm, new UTF8Encoding(false, true))
{
}
public Reader(Stream strm, Encoding encoding)
{
try
{
if (strm?.CanRead == true)
BaseReader = new BinaryReader(strm, encoding);
else throw new Exception($"The Stream is null.");
}
catch (Exception e)
{
throw new Exception($"Error: {e.Message}");
}
}
/// <summary>
/// Expose the underlying BinaryReader
/// </summary>
public BinaryReader BaseReader { get; }
/// <summary>
/// Expose the underlying FileStream
/// </summary>
public FileStream BaseStream { get; }
/// <summary>
/// Each time a primitive is written there is one additional integer written to signify its type.
/// This can be used to compute the final size of the stream bytes needed.
/// </summary>
public static int PrimitiveOverHead
{
get { return sizeof(int); }
}
/// <summary>
/// Each time a array is written there are two additional integers written one to signify its type and the other its size.
/// This can be used to compute the final size of the stream bytes needed.
/// </summary>
public static int ArrayOverHead
{
get { return sizeof(int) * 2; }
}
public void Dispose()
{
BaseStream?.Dispose();
}
/// <summary>
/// Sets the position to offset relative to origin within the stream.
/// </summary>
public long Seek(int offset, SeekOrigin origin)
{
return BaseStream.Seek(offset, origin);
}
/// <summary>
/// A wrapper for PeekChar of BinaryReader underling class.
/// </summary>
public int PeekChar()
{
return BaseReader.PeekChar();
}
public bool ReadBool()
{
if (BaseReader.PeekChar() == -1)
throw new Exception("Primary boolean value does not exist or can not be read.");
var t = BaseReader.ReadInt32();
if (t != (int)Rwtypes.Readerwritertypes.Boolean)
throw new Exception($"Boolean value read requested '{Rwtypes.GetType(t)}' value found.");
return BaseReader.ReadBoolean();
}
public char ReadChar()
{
if (BaseReader.PeekChar() == -1)
throw new Exception("Primary Char value does not exist or can not be read.");
var t = BaseReader.ReadInt32();
if (t != (int)Rwtypes.Readerwritertypes.Character)
throw new Exception($"Character value read requested '{Rwtypes.GetType(t)}' value found.");
return BaseReader.ReadChar();
}
public byte ReadByte()
{
if (BaseReader.PeekChar() == -1)
throw new Exception("Primary Byte value does not exist or can not be read.");
var t = BaseReader.ReadInt32();
if (t != (int)Rwtypes.Readerwritertypes.Byte)
throw new Exception($"Byte value read requested '{Rwtypes.GetType(t)}' value found.");
return BaseReader.ReadByte();
}
public sbyte ReadSByte()
{
if (BaseReader.PeekChar() == -1)
throw new Exception("Primary short byte value does not exist or can not be read.");
var t = BaseReader.ReadInt32();
if (t != (int)Rwtypes.Readerwritertypes.ShortByte)
throw new Exception($"Short Byte value read requested '{Rwtypes.GetType(t)}' value found.");
return BaseReader.ReadSByte();
}
public short ReadShort()
{
if (BaseReader.PeekChar() == -1)
throw new Exception("Primary Short value does not exist or can not be read.");
var t = BaseReader.ReadInt32();
if (t != (int)Rwtypes.Readerwritertypes.Short)
throw new Exception($"Short value read requested '{Rwtypes.GetType(t)}' value found.");
return BaseReader.ReadInt16();
}
public ushort ReadUShort()
{
if (BaseReader.PeekChar() == -1)
throw new Exception($"Primary Unsigned Short value does not exist or can not be read.");
var t = BaseReader.ReadInt32();
if (t != (int)Rwtypes.Readerwritertypes.UnsignedShort)
throw new Exception($"Unsigned Short value read requested '{Rwtypes.GetType(t)}' value found.");
return BaseReader.ReadUInt16();
}
public int ReadInt()
{
if (BaseReader.PeekChar() == -1)
throw new Exception("Primary Integer value does not exist or can not be read.");
var t = BaseReader.ReadInt32();
if (t != (int)Rwtypes.Readerwritertypes.Integer)
throw new Exception($"Integer value read requested '{Rwtypes.GetType(t)}' value found.");
return BaseReader.ReadInt32();
}
public uint ReadUInt()
{
if (BaseReader.PeekChar() == -1)
throw new Exception("Primary Unsigned Integer value does not exist or can not be read.");
var t = BaseReader.ReadInt32();
if (t != (int)Rwtypes.Readerwritertypes.UnsignedInteger)
throw new Exception($"Unsigned Integer value read requested '{Rwtypes.GetType(t)}' value found.");
return BaseReader.ReadUInt32();
}
public long ReadLong()
{
if (BaseReader.PeekChar() == -1)
throw new Exception("Primary Long value does not exist or can not be read.");
var t = BaseReader.ReadInt32();
if (t != (int)Rwtypes.Readerwritertypes.Long)
throw new Exception($"Long value read requested '{Rwtypes.GetType(t)}' value found.");
return BaseReader.ReadInt64();
}
public ulong ReadULong()
{
if (BaseReader.PeekChar() == -1)
throw new Exception("Primary Unsigned Long value does not exist or can not be read.");
var t = BaseReader.ReadInt32();
if (t != (int)Rwtypes.Readerwritertypes.UnsignedLong)
throw new Exception($"Unsigned Long value read requested '{Rwtypes.GetType(t)}' value found.");
return BaseReader.ReadUInt64();
}
public string ReadString()
{
if (BaseReader.PeekChar() == -1)
throw new Exception("Primary String value does not exist or can not be read.");
var t = BaseReader.ReadInt32();
if (t != (int)Rwtypes.Readerwritertypes.String)
throw new Exception($"String value read requested '{Rwtypes.GetType(t)}' value found.");
return BaseReader.ReadString();
}
public float ReadFloat()
{
if (BaseReader.PeekChar() == -1)
throw new Exception("Primary Float value does not exist or can not be read.");
var t = BaseReader.ReadInt32();
if (t != (int)Rwtypes.Readerwritertypes.Float)
throw new Exception($"Float value read requested '{Rwtypes.GetType(t)}' value found.");
return BaseReader.ReadSingle();
}
public double ReadDouble()
{
if (BaseReader.PeekChar() == -1)
throw new Exception("Primary Double value does not exist or can not be read.");
var t = BaseReader.ReadInt32();
if (t != (int)Rwtypes.Readerwritertypes.Double)
throw new Exception($"Double value read requested '{Rwtypes.GetType(t)}' value found.");
return BaseReader.ReadDouble();
}
public decimal ReadDecimal()
{
if (BaseReader.PeekChar() == -1)
throw new Exception("Primary Decimal value does not exist or can not be read.");
var t = BaseReader.ReadInt32();
if (t != (int)Rwtypes.Readerwritertypes.Decimal)
throw new Exception($"Decimal value read requested '{Rwtypes.GetType(t)}' value found.");
return BaseReader.ReadDecimal();
}
public BigInteger ReadBigInteger()
{
if (BaseReader.PeekChar() == -1)
throw new Exception("Primary BigInteger value does not exist or can not be read.");
var t = BaseReader.ReadInt32();
if (t != (int)Rwtypes.Readerwritertypes.BigInteger)
throw new Exception($"BigInteger value read requested '{Rwtypes.GetType(t)}' value found.");
return new BigInteger(ReadBytes());
}
public BigRational ReadBigRational()
{
if (BaseReader.PeekChar() == -1)
throw new Exception("Primary BigRational value does not exist or can not be read.");
var t = BaseReader.ReadInt32();
if (t != (int)Rwtypes.Readerwritertypes.BigRational)
throw new Exception($"BigRational value read requested '{Rwtypes.GetType(t)}' value found.");
var Num = new BigInteger(ReadBytes());
var Den = new BigInteger(ReadBytes());
return new BigRational(Num, Den);
}
public bool[] ReadBools()
{
if (BaseReader.PeekChar() == -1)
throw new Exception("Primary boolean array value does not exist or can not be read.");
var t = BaseReader.ReadInt32();
if (t != (int)Rwtypes.Readerwritertypes.BooleanArray)
throw new Exception($"Boolean Array value read requested '{Rwtypes.GetType(t)}' value found.");
var len = BaseReader.ReadInt32();
var arr = new bool[len];
for (var i = 0; i < len; ++i)
arr[i] = BaseReader.ReadBoolean();
return arr;
}
public char[] ReadChars()
{
if (BaseReader.PeekChar() == -1)
throw new Exception("Primary char array value does not exist or can not be read.");
var t = BaseReader.ReadInt32();
if (t != (int)Rwtypes.Readerwritertypes.CharacterArray)
throw new Exception($"Character array value read requested '{Rwtypes.GetType(t)}' value found.");
var len = BaseReader.ReadInt32();
var arr = new char[len];
for (var i = 0; i < len; ++i)
arr[i] = BaseReader.ReadChar();
return arr;
}
public byte[] ReadBytes()
{
if (BaseReader.PeekChar() == -1)
throw new Exception("Primary byte array value does not exist or can not be read.");
var t = BaseReader.ReadInt32();
if (t != (int)Rwtypes.Readerwritertypes.ByteArray)
throw new Exception($"Byte Array value read requested '{Rwtypes.GetType(t)}' value found.");
var len = BaseReader.ReadInt32();
var arr = new byte[len];
for (var i = 0; i < len; ++i)
arr[i] = BaseReader.ReadByte();
return arr;
}
public sbyte[] ReadSBytes()
{
if (BaseReader.PeekChar() == -1)
throw new Exception("Primary sbyte array value does not exist or can not be read.");
var t = BaseReader.ReadInt32();
if (t != (int)Rwtypes.Readerwritertypes.ShortByteArray)
throw new Exception($"Short Byte Array value read requested '{Rwtypes.GetType(t)}' value found.");
var len = BaseReader.ReadInt32();
var arr = new sbyte[len];
for (var i = 0; i < len; ++i)
arr[i] = BaseReader.ReadSByte();
return arr;
}
public short[] ReadShorts()
{
if (BaseReader.PeekChar() == -1)
throw new Exception("Primary short array value does not exist or can not be read.");
var t = BaseReader.ReadInt32();
if (t != (int)Rwtypes.Readerwritertypes.ShortArray)
throw new Exception($"Short Array value read requested '{Rwtypes.GetType(t)}' value found.");
var len = BaseReader.ReadInt32();
var arr = new short[len];
for (var i = 0; i < len; ++i)
arr[i] = BaseReader.ReadInt16();
return arr;
}
public ushort[] ReadUShorts()
{
if (BaseReader.PeekChar() == -1)
throw new Exception("Primary unsigned short array value does not exist or can not be read.");
var t = BaseReader.ReadInt32();
if (t != (int)Rwtypes.Readerwritertypes.UnsignedShortArray)
throw new Exception($"Unsigned Short Array value read requested '{Rwtypes.GetType(t)}' value found.");
var len = BaseReader.ReadInt32();
var arr = new ushort[len];
for (var i = 0; i < len; ++i)
arr[i] = BaseReader.ReadUInt16();
return arr;
}
public int[] ReadInts()
{
if (BaseReader.PeekChar() == -1)
throw new Exception("Primary integer array value does not exist or can not be read.");
var t = BaseReader.ReadInt32();
if (t != (int)Rwtypes.Readerwritertypes.IntegerArray)
throw new Exception($"Integer Array value read requested '{Rwtypes.GetType(t)}' value found.");
var len = BaseReader.ReadInt32();
var arr = new int[len];
for (var i = 0; i < len; ++i)
arr[i] = BaseReader.ReadInt32();
return arr;
}
public uint[] ReadUInts()
{
if (BaseReader.PeekChar() == -1)
throw new Exception("Primary unsigned integer array value does not exist or can not be read.");
var t = BaseReader.ReadInt32();
if (t != (int)Rwtypes.Readerwritertypes.UnsignedInteger)
throw new Exception($"Unsigned Integer Array value read requested '{Rwtypes.GetType(t)}' value found.");
var len = BaseReader.ReadInt32();
var arr = new uint[len];
for (var i = 0; i < len; ++i)
arr[i] = BaseReader.ReadUInt32();
return arr;
}
public long[] ReadLongs()
{
if (BaseReader.PeekChar() == -1)
throw new Exception("Primary long array value does not exist or can not be read.");
var t = BaseReader.ReadInt32();
if (t != (int)Rwtypes.Readerwritertypes.LongArray)
throw new Exception($"Long Array value read requested '{Rwtypes.GetType(t)}' value found.");
var len = BaseReader.ReadInt32();
var arr = new long[len];
for (var i = 0; i < len; ++i)
arr[i] = BaseReader.ReadInt64();
return arr;
}
public ulong[] ReadULongs()
{
if (BaseReader.PeekChar() == -1)
throw new Exception("Primary unsigned long array value does not exist or can not be read.");
var t = BaseReader.ReadInt32();
if (t != (int)Rwtypes.Readerwritertypes.UnsignedLongArray)
throw new Exception($"Unsigned Long Array value read requested '{Rwtypes.GetType(t)}' value found.");
var len = BaseReader.ReadInt32();
var arr = new ulong[len];
for (var i = 0; i < len; ++i)
arr[i] = BaseReader.ReadUInt64();
return arr;
}
public string[] ReadStrings()
{
if (BaseReader.PeekChar() == -1)
throw new Exception("Primary string array value does not exist or can not be read.");
var t = BaseReader.ReadInt32();
if (t != (int)Rwtypes.Readerwritertypes.StringArray)
throw new Exception($"String Array value read requested '{Rwtypes.GetType(t)}' value found.");
var len = BaseReader.ReadInt32();
var arr = new string[len];
for (var i = 0; i < len; ++i)
arr[i] = BaseReader.ReadString();
return arr;
}
public float[] ReadFloats()
{
if (BaseReader.PeekChar() == -1)
throw new Exception("Primary float array value does not exist or can not be read.");
var t = BaseReader.ReadInt32();
if (t != (int)Rwtypes.Readerwritertypes.FloatArray)
throw new Exception($"Float Array value read requested '{Rwtypes.GetType(t)}' value found.");
var len = BaseReader.ReadInt32();
var arr = new float[len];
for (var i = 0; i < len; ++i)
arr[i] = BaseReader.ReadSingle();
return arr;
}
public double[] ReadDoubles()
{
if (BaseReader.PeekChar() == -1)
throw new Exception("Primary double array value does not exist or can not be read.");
var t = BaseReader.ReadInt32();
if (t != (int)Rwtypes.Readerwritertypes.DoubleArray)
throw new Exception($"Double Array value read requested '{Rwtypes.GetType(t)}' value found.");
var len = BaseReader.ReadInt32();
var arr = new double[len];
for (var i = 0; i < len; ++i)
arr[i] = BaseReader.ReadDouble();
return arr;
}
public decimal[] ReadDecimals()
{
if (BaseReader.PeekChar() == -1)
throw new Exception("Primary decimal array value does not exist or can not be read.");
var t = BaseReader.ReadInt32();
if (t != (int)Rwtypes.Readerwritertypes.DecimalArray)
throw new Exception($"Decimal Array value read requested '{Rwtypes.GetType(t)}' value found.");
var len = BaseReader.ReadInt32();
var arr = new decimal[len];
for (var i = 0; i < len; ++i)
arr[i] = BaseReader.ReadDecimal();
return arr;
}
public BigInteger[] ReadBigIntegers()
{
if (BaseReader.PeekChar() == -1)
throw new Exception("Primary BigInteger array value does not exist or can not be read.");
var t = BaseReader.ReadInt32();
if (t != (int)Rwtypes.Readerwritertypes.BigIntegerArray)
throw new Exception($"BigInteger Array value read requested '{Rwtypes.GetType(t)}' value found.");
var len = BaseReader.ReadInt32();
var arr = new BigInteger[len];
for (var i = 0; i < len; ++i)
arr[i] = ReadBigInteger();
return arr;
}
public BigRational[] ReadBigRationals()
{
if (BaseReader.PeekChar() == -1)
throw new Exception("Primary BigRational array value does not exist or can not be read.");
var t = BaseReader.ReadInt32();
if (t != (int)Rwtypes.Readerwritertypes.BigRationalArray)
throw new Exception($"BigRational Array value read requested '{Rwtypes.GetType(t)}' value found.");
var len = BaseReader.ReadInt32();
var arr = new BigRational[len];
for (var i = 0; i < len; ++i)
arr[i] = ReadBigRational();
return arr;
}
}
public class Writer : IDisposable
{
/// <inheritdoc />
/// <summary>
/// Initializes a new instance of the Writer class based on a specified path name, a default FileMode of Create, and
/// defaulting to UTF-8 encoding.
/// </summary>
public Writer(string path) : this(path, FileMode.Create, new UTF8Encoding(false, true))
{
}
/// <inheritdoc />
/// <summary>
/// Initializes a new instance of the Writer class based on a specified path name, a specified FileMode and defaulting
/// to UTF-8 encoding.
/// </summary>
public Writer(string path, FileMode fm) : this(path, fm, new UTF8Encoding(false, true))
{
}
/// <summary>
/// Initializes a new instance of the Writer class based on a specified path name, a specified FileMode and a specified
/// encoding.
/// </summary>
public Writer(string path, FileMode fm, Encoding encoding)
{
if (string.IsNullOrEmpty(path))
throw new Exception($"Path {path} cannot be null or empty.");
BaseMode = fm;
try
{
File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.Normal);
BaseStream = new FileStream(path, BaseMode, FileAccess.Write);
if (BaseStream?.CanWrite == true)
BaseWriter = new BinaryWriter(BaseStream, encoding, true);
else throw new Exception($"The FileStream for path:{path} is null.");
}
catch (Exception e)
{
throw new Exception($"Error: {e.Message}");
}
}
/// <summary>
/// Expose the underlying BinaryWriter
/// </summary>
public BinaryWriter BaseWriter { get; }
/// <summary>
/// Expose the underlying FileMode
/// </summary>
public FileMode BaseMode { get; private set; }
/// <summary>
/// Expose the underlying FileStream
/// </summary>
public FileStream BaseStream { get; }
/// <summary>
/// Sets the position to offset relative to origin within the stream.
/// </summary>
public long Seek(int offset, SeekOrigin origin)
{
return BaseStream.Seek(offset, origin);
}
public void WriteBool(bool value)
{
BaseWriter.Write((int)Rwtypes.Readerwritertypes.Boolean);
BaseWriter.Write(value);
}
public void WriteChar(char value)
{
BaseWriter.Write((int)Rwtypes.Readerwritertypes.Character);
BaseWriter.Write(value);
}
public void WriteByte(byte value)
{
BaseWriter.Write((int)Rwtypes.Readerwritertypes.Byte);
BaseWriter.Write(value);
}
public void WriteSByte(sbyte value)
{
BaseWriter.Write((int)Rwtypes.Readerwritertypes.ShortByte);
BaseWriter.Write(value);
}
public void WriteShort(short value)
{
BaseWriter.Write((int)Rwtypes.Readerwritertypes.Short);
BaseWriter.Write(value);
}
public void WriteUShort(ushort value)
{
BaseWriter.Write((int)Rwtypes.Readerwritertypes.UnsignedShort);
BaseWriter.Write(value);
}
public void WriteInt(int value)
{
BaseWriter.Write((int)Rwtypes.Readerwritertypes.Integer);
BaseWriter.Write(value);
}
public void WriteUInt(uint value)
{
BaseWriter.Write((int)Rwtypes.Readerwritertypes.UnsignedInteger);
BaseWriter.Write(value);
}
public void WriteLOng(long value)
{
BaseWriter.Write((int)Rwtypes.Readerwritertypes.Long);
BaseWriter.Write(value);
}
public void WriteULong(ulong value)
{
BaseWriter.Write((int)Rwtypes.Readerwritertypes.UnsignedLong);
BaseWriter.Write(value);
}
public void WriteString(string value)
{
BaseWriter.Write((int)Rwtypes.Readerwritertypes.String);
BaseWriter.Write(value);
}
public void WriteFloat(float value)
{
BaseWriter.Write((int)Rwtypes.Readerwritertypes.Float);
BaseWriter.Write(value);
}
public void WriteDouble(double value)
{
BaseWriter.Write((int)Rwtypes.Readerwritertypes.Double);
BaseWriter.Write(value);
}
public void WriteDecimal(decimal value)
{
BaseWriter.Write((int)Rwtypes.Readerwritertypes.Decimal);
BaseWriter.Write(value);
}
public void WriteBigInteger(BigInteger value)
{
BaseWriter.Write((int)Rwtypes.Readerwritertypes.BigInteger);
WriteBytes(value.ToByteArray());
}
public void WriteBigRational(BigRational value)
{
BaseWriter.Write((int)Rwtypes.Readerwritertypes.BigRational);
var Num = value.Numerator.ToByteArray();
var Den = value.Denominator.ToByteArray();
WriteBytes(Num);
WriteBytes(Den);
}
public void WriteBools(bool[] value)
{
BaseWriter.Write((int)Rwtypes.Readerwritertypes.BooleanArray);
var len = value.Length;
BaseWriter.Write(len);
foreach (var v in value)
BaseWriter.Write(v);
}
public void WriteChars(char[] value)
{
BaseWriter.Write((int)Rwtypes.Readerwritertypes.CharacterArray);
var len = value.Length;
BaseWriter.Write(len);
foreach (var v in value)
BaseWriter.Write(v);
}
public void WriteBytes(byte[] value)
{
BaseWriter.Write((int)Rwtypes.Readerwritertypes.ByteArray);
var len = value.Length;
BaseWriter.Write(len);
foreach (var v in value)
BaseWriter.Write(v);
}
public void WriteSBytes(sbyte[] value)
{
BaseWriter.Write((int)Rwtypes.Readerwritertypes.ShortByteArray);
var len = value.Length;
BaseWriter.Write(len);
foreach (var v in value)
BaseWriter.Write(v);
}
public void WriteShorts(short[] value)
{
BaseWriter.Write((int)Rwtypes.Readerwritertypes.ShortArray);
var len = value.Length;
BaseWriter.Write(len);
foreach (var v in value)
BaseWriter.Write(v);
}
public void WriteUShorts(ushort[] value)
{
BaseWriter.Write((int)Rwtypes.Readerwritertypes.UnsignedShortArray);
var len = value.Length;
BaseWriter.Write(len);
foreach (var v in value)
BaseWriter.Write(v);
}
public void WriteInts(int[] value)
{
BaseWriter.Write((int)Rwtypes.Readerwritertypes.IntegerArray);
var len = value.Length;
BaseWriter.Write(len);
foreach (var v in value)
BaseWriter.Write(v);
}
public void WriteUInts(uint[] value)
{
BaseWriter.Write((int)Rwtypes.Readerwritertypes.UnsignedIntegerArray);
var len = value.Length;
BaseWriter.Write(len);
foreach (var v in value)
BaseWriter.Write(v);
}
public void WriteLongs(long[] value)
{
BaseWriter.Write((int)Rwtypes.Readerwritertypes.LongArray);
var len = value.Length;
BaseWriter.Write(len);
foreach (var v in value)
BaseWriter.Write(v);
}
public void WriteULongs(ulong[] value)
{
BaseWriter.Write((int)Rwtypes.Readerwritertypes.UnsignedLongArray);
var len = value.Length;
BaseWriter.Write(len);
foreach (var v in value)
BaseWriter.Write(v);
}
public void WriteStrings(string[] value)
{
BaseWriter.Write((int)Rwtypes.Readerwritertypes.StringArray);
var len = value.Length;
BaseWriter.Write(len);
foreach (var v in value)
BaseWriter.Write(v);
}
public void WriteFloats(float[] value)
public class Writer : IDisposable
{
/// <inheritdoc />
/// <summary>
/// Initializes a new instance of the Writer class based on a specified path name, a default FileMode of Create, and
/// defaulting to UTF-8 encoding.
/// </summary>
public Writer(string path) : this(path, FileMode.Create, new UTF8Encoding(false, true))
{
}
/// <inheritdoc />
/// <summary>
/// Initializes a new instance of the Writer class based on a specified path name, a specified FileMode and defaulting
/// to UTF-8 encoding.
/// </summary>
public Writer(string path, FileMode fm) : this(path, fm, new UTF8Encoding(false, true))
{
}
/// <summary>
/// Initializes a new instance of the Writer class based on a specified path name, a specified FileMode and a specified
/// encoding.
/// </summary>
public Writer(string path, FileMode fm, Encoding encoding)
{
if (string.IsNullOrEmpty(path))
throw new Exception($"Path {path} cannot be null or empty.");
BaseMode = fm;
try
{
if (File.Exists(path))
File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.Normal);
BaseStream = new FileStream(path, BaseMode, FileAccess.Write);
if (BaseStream?.CanWrite == true)
BaseWriter = new BinaryWriter(BaseStream, encoding, true);
else throw new Exception($"The FileStream for path:{path} is null.");
}
catch (Exception e)
{
throw new Exception($"Error: {e.Message}");
}
}
public Writer(Stream stream) : this(stream, new UTF8Encoding(false, true))
{
}
public Writer(Stream strm, Encoding encoding)
{
try
{
if (strm?.CanWrite == true)
BaseWriter = new BinaryWriter(strm, encoding, true);
else throw new Exception($"The Stream is null.");
}
catch (Exception e)
{
throw new Exception($"Error: {e.Message}");
}
}
/// <summary>
/// Expose the underlying BinaryWriter
/// </summary>
public BinaryWriter BaseWriter { get; private set; }
/// <summary>
/// Expose the underlying FileMode
/// </summary>
public FileMode BaseMode { get; private set; }
/// <summary>
/// Expose the underlying FileStream
/// </summary>
public FileStream BaseStream { get; private set; }
/// <summary>
/// Each time a primitive is written there is one additional integer written to signify its type.
/// This can be used to compute the final size of the stream bytes needed.
/// </summary>
public static int PrimitiveOverHead
{
get { return sizeof(int); }
}
/// <summary>
/// Each time a array is written there are two additional integers written one to signify its type and the other its size.
/// This can be used to compute the final size of the stream bytes needed.
/// </summary>
public static int ArrayOverHead
{
get { return sizeof(int)*2; }
}
/// <summary>
/// Sets the position to offset relative to origin within the stream.
/// </summary>
public long Seek(int offset, SeekOrigin origin)
{
return BaseStream.Seek(offset, origin);
}
public void WriteBool(bool value)
{
BaseWriter.Write((int)Rwtypes.Readerwritertypes.Boolean);
BaseWriter.Write(value);
}
public void WriteChar(char value)
{
BaseWriter.Write((int)Rwtypes.Readerwritertypes.Character);
BaseWriter.Write(value);
}
public void WriteByte(byte value)
{
BaseWriter.Write((int)Rwtypes.Readerwritertypes.Byte);
BaseWriter.Write(value);
}
public void WriteSByte(sbyte value)
{
BaseWriter.Write((int)Rwtypes.Readerwritertypes.ShortByte);
BaseWriter.Write(value);
}
public void WriteShort(short value)
{
BaseWriter.Write((int)Rwtypes.Readerwritertypes.Short);
BaseWriter.Write(value);
}
public void WriteUShort(ushort value)
{
BaseWriter.Write((int)Rwtypes.Readerwritertypes.UnsignedShort);
BaseWriter.Write(value);
}
public void WriteInt(int value)
{
BaseWriter.Write((int)Rwtypes.Readerwritertypes.Integer);
BaseWriter.Write(value);
}
public void WriteUInt(uint value)
{
BaseWriter.Write((int)Rwtypes.Readerwritertypes.UnsignedInteger);
BaseWriter.Write(value);
}
public void WriteLOng(long value)
{
BaseWriter.Write((int)Rwtypes.Readerwritertypes.Long);
BaseWriter.Write(value);
}
public void WriteULong(ulong value)
{
BaseWriter.Write((int)Rwtypes.Readerwritertypes.UnsignedLong);
BaseWriter.Write(value);
}
public void WriteString(string value)
{
BaseWriter.Write((int)Rwtypes.Readerwritertypes.String);
BaseWriter.Write(value);
}
public void WriteFloat(float value)
{
BaseWriter.Write((int)Rwtypes.Readerwritertypes.Float);
BaseWriter.Write(value);
}
public void WriteDouble(double value)
{
BaseWriter.Write((int)Rwtypes.Readerwritertypes.Double);
BaseWriter.Write(value);
}
public void WriteDecimal(decimal value)
{
BaseWriter.Write((int)Rwtypes.Readerwritertypes.Decimal);
BaseWriter.Write(value);
}
public void WriteBigInteger(BigInteger value)
{
BaseWriter.Write((int)Rwtypes.Readerwritertypes.BigInteger);
WriteBytes(value.ToByteArray());
}
public void WriteBigRational(BigRational value)
{
BaseWriter.Write((int)Rwtypes.Readerwritertypes.BigRational);
var Num = value.Numerator.ToByteArray();
var Den = value.Denominator.ToByteArray();
WriteBytes(Num);
WriteBytes(Den);
}
public void WriteBools(bool[] value)
{
BaseWriter.Write((int)Rwtypes.Readerwritertypes.BooleanArray);
var len = value.Length;
BaseWriter.Write(len);
foreach (var v in value)
BaseWriter.Write(v);
}
public void WriteChars(char[] value)
{
BaseWriter.Write((int)Rwtypes.Readerwritertypes.CharacterArray);
var len = value.Length;
BaseWriter.Write(len);
foreach (var v in value)
BaseWriter.Write(v);
}
public void WriteBytes(byte[] value)
{
BaseWriter.Write((int)Rwtypes.Readerwritertypes.ByteArray);
var len = value.Length;
BaseWriter.Write(len);
foreach (var v in value)
BaseWriter.Write(v);
}
public void WriteSBytes(sbyte[] value)
{
BaseWriter.Write((int)Rwtypes.Readerwritertypes.ShortByteArray);
var len = value.Length;
BaseWriter.Write(len);
foreach (var v in value)
BaseWriter.Write(v);
}
public void WriteShorts(short[] value)
{
BaseWriter.Write((int)Rwtypes.Readerwritertypes.ShortArray);
var len = value.Length;
BaseWriter.Write(len);
foreach (var v in value)
BaseWriter.Write(v);
}
public void WriteUShorts(ushort[] value)
{
BaseWriter.Write((int)Rwtypes.Readerwritertypes.UnsignedShortArray);
var len = value.Length;
BaseWriter.Write(len);
foreach (var v in value)
BaseWriter.Write(v);
}
public void WriteInts(int[] value)
{
BaseWriter.Write((int)Rwtypes.Readerwritertypes.IntegerArray);
var len = value.Length;
BaseWriter.Write(len);
foreach (var v in value)
BaseWriter.Write(v);
}
public void WriteUInts(uint[] value)
{
BaseWriter.Write((int)Rwtypes.Readerwritertypes.UnsignedIntegerArray);
var len = value.Length;
BaseWriter.Write(len);
foreach (var v in value)
BaseWriter.Write(v);
}
public void WriteLongs(long[] value)
{
BaseWriter.Write((int)Rwtypes.Readerwritertypes.LongArray);
var len = value.Length;
BaseWriter.Write(len);
foreach (var v in value)
BaseWriter.Write(v);
}
public void WriteULongs(ulong[] value)
{
BaseWriter.Write((int)Rwtypes.Readerwritertypes.UnsignedLongArray);
var len = value.Length;
BaseWriter.Write(len);
foreach (var v in value)
BaseWriter.Write(v);
}
public void WriteStrings(string[] value)
{
BaseWriter.Write((int)Rwtypes.Readerwritertypes.StringArray);
var len = value.Length;
BaseWriter.Write(len);
foreach (var v in value)
BaseWriter.Write(v);
}
public void WriteFloats(float[] value)
{
BaseWriter.Write((int)Rwtypes.Readerwritertypes.FloatArray);
var len = value.Length;
BaseWriter.Write(len);
foreach (var v in value)
BaseWriter.Write(v);
}
public void WriteDoubles(double[] value)
{
BaseWriter.Write((int)Rwtypes.Readerwritertypes.DoubleArray);
var len = value.Length;
BaseWriter.Write(len);
foreach (var v in value)
BaseWriter.Write(v);
}
public void WriteDecimals(decimal[] value)
{
BaseWriter.Write((int)Rwtypes.Readerwritertypes.DecimalArray);
var len = value.Length;
BaseWriter.Write(len);
foreach (var v in value)
BaseWriter.Write(v);
}
public void WriteBigIntegers(BigInteger[] value)
{
BaseWriter.Write((int)Rwtypes.Readerwritertypes.BigIntegerArray);
var len = value.Length;
BaseWriter.Write(len);
foreach (var v in value)
WriteBigInteger(v);
}
public void WriteBigRationals(BigRational[] value)
{
BaseWriter.Write((int)Rwtypes.Readerwritertypes.BigRationalArray);
var len = value.Length;
BaseWriter.Write(len);
foreach (var v in value)
WriteBigRational(v);
}
public void Dispose()
{
if (BaseStream == null) return;
BaseStream.Flush();
BaseStream.Dispose();
}
}
public static class Rwtypes
{
public enum Readerwritertypes
{
Boolean,
Character,
Byte,
ShortByte,
Short,
UnsignedShort,
Integer,
UnsignedInteger,
Long,
UnsignedLong,
String,
Float,
Double,
Decimal,
BooleanArray,
CharacterArray,
ByteArray,
ShortByteArray,
ShortArray,
UnsignedShortArray,
IntegerArray,
UnsignedIntegerArray,
LongArray,
UnsignedLongArray,
StringArray,
FloatArray,
DoubleArray,
DecimalArray,
BigInteger,
BigIntegerArray,
BigRational,
BigRationalArray
}
public static string GetType(int t)
{
foreach (var v in ToDictionary<Readerwritertypes>())
if (v.Key == t)
return v.Value;
return $"{t} is not a valid type";
}
private static Dictionary<int, string> ToDictionary<T>()
{
var Dictionary = new Dictionary<int, string>();
foreach (var v in Enum.GetValues(typeof(T)))
Dictionary.Add(Convert.ToInt32(v), v.ToString());
return Dictionary;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment