{"id":60,"date":"2020-06-10T04:49:21","date_gmt":"2020-06-10T04:49:21","guid":{"rendered":"https:\/\/michaeljohnsteiner.com\/?p=60"},"modified":"2020-06-11T04:39:53","modified_gmt":"2020-06-11T04:39:53","slug":"reader-cs","status":"publish","type":"post","link":"https:\/\/michaeljohnsteiner.com\/index.php\/2020\/06\/10\/reader-cs\/","title":{"rendered":"Reader.cs"},"content":{"rendered":"\n<p>Simplified Binary Reader<\/p>\n\n\n\n<p>Example Code:<\/p>\n\n\n\n<figure class=\"wp-block-embed-wordpress wp-block-embed is-type-wp-embed is-provider-michaeljohnsteiner-com\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"Ttej77GLXL\"><a href=\"https:\/\/michaeljohnsteiner.com\/index.php\/2020\/06\/05\/chisquaredimagecomparison-cs\/\">ChiSquaredImageComparison.cs<\/a><\/blockquote><iframe title=\"&#8220;ChiSquaredImageComparison.cs&#8221; &#8212; MichaelJohnSteiner.com\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" src=\"https:\/\/michaeljohnsteiner.com\/index.php\/2020\/06\/05\/chisquaredimagecomparison-cs\/embed\/#?secret=Ttej77GLXL\" data-secret=\"Ttej77GLXL\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">using System;\nusing System.IO;\nusing System.Numerics;\nusing System.Text;\n\/\/\/ &lt;inheritdoc \/>\n\/\/\/ &lt;summary>\n\/\/\/     Alternate Binary Reader created to ease usability and complexity of coding.\n\/\/\/ &lt;\/summary>\npublic class Reader : IDisposable\n{\n    \/\/\/ &lt;inheritdoc \/>\n    \/\/\/ &lt;summary>\n    \/\/\/     Initializes a new instance of the Reader class based on a specified path name, and defaulting to UTF-8 encoding.\n    \/\/\/ &lt;\/summary>\n    public Reader(string path) : this(path, new UTF8Encoding(false, true))\n    {\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Initializes a new instance of the Reader class based on a specified path name, a specified encoding.\n    \/\/\/ &lt;\/summary>\n    public Reader(string path, Encoding encoding)\n    {\n        if (string.IsNullOrEmpty(path))\n            throw new Exception($\"Path {path} cannot be null or empty.\");\n        try\n        {\n            File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.Normal);\n            BaseStream = new FileStream(path, FileMode.Open, FileAccess.Read);\n            if (BaseStream?.CanRead == true)\n                BaseReader = new BinaryReader(BaseStream, encoding);\n            else throw new Exception($\"The FileStream for path:{path} is null.\");\n        }\n        catch (Exception e)\n        {\n            throw new Exception($\"Error: {e.Message}\");\n        }\n    }\n    public Reader(Stream strm) : this(strm, new UTF8Encoding(false, true))\n    {\n    }\n    public Reader(Stream strm, Encoding encoding)\n    {\n        try\n        {\n            if (strm?.CanRead == true)\n                BaseReader = new BinaryReader(strm, encoding);\n            else throw new Exception(\"The Stream is null.\");\n        }\n        catch (Exception e)\n        {\n            throw new Exception($\"Error: {e.Message}\");\n        }\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Expose the underlying BinaryReader\n    \/\/\/ &lt;\/summary>\n    public BinaryReader BaseReader { get; }\n    \/\/\/ &lt;summary>\n    \/\/\/     Expose the underlying FileStream\n    \/\/\/ &lt;\/summary>\n    public FileStream BaseStream { get; }\n    \/\/\/ &lt;summary>\n    \/\/\/     Each time a primitive is written there is one additional integer written to signify its type.\n    \/\/\/     This can be used to compute the final size of the stream bytes needed.\n    \/\/\/ &lt;\/summary>\n    public static int PrimitiveOverHead => sizeof(int);\n    \/\/\/ &lt;summary>\n    \/\/\/     Each time a array is written there are two additional integers written one to signify its type and the other its\n    \/\/\/     size.\n    \/\/\/     This can be used to compute the final size of the stream bytes needed.\n    \/\/\/ &lt;\/summary>\n    public static int ArrayOverHead => sizeof(int) * 2;\n    public void Dispose()\n    {\n        BaseStream?.Dispose();\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     Sets the position to offset relative to origin within the stream.\n    \/\/\/ &lt;\/summary>\n    public long Seek(int offset, SeekOrigin origin)\n    {\n        return BaseStream.Seek(offset, origin);\n    }\n    \/\/\/ &lt;summary>\n    \/\/\/     A wrapper for PeekChar of BinaryReader underling class.\n    \/\/\/ &lt;\/summary>\n    public int PeekChar()\n    {\n        return BaseReader.PeekChar();\n    }\n    public bool ReadBool()\n    {\n        if (BaseReader.PeekChar() == -1)\n            throw new Exception(\"Primary boolean value does not exist or can not be read.\");\n        var t = BaseReader.ReadInt32();\n        if (t != (int) Rwtypes.Readerwritertypes.Boolean)\n            throw new Exception($\"Boolean value read requested '{Rwtypes.GetType(t)}' value found.\");\n        return BaseReader.ReadBoolean();\n    }\n    public char ReadChar()\n    {\n        if (BaseReader.PeekChar() == -1)\n            throw new Exception(\"Primary Char value does not exist or can not be read.\");\n        var t = BaseReader.ReadInt32();\n        if (t != (int) Rwtypes.Readerwritertypes.Character)\n            throw new Exception($\"Character value read requested '{Rwtypes.GetType(t)}' value found.\");\n        return BaseReader.ReadChar();\n    }\n    public byte ReadByte()\n    {\n        if (BaseReader.PeekChar() == -1)\n            throw new Exception(\"Primary Byte value does not exist or can not be read.\");\n        var t = BaseReader.ReadInt32();\n        if (t != (int) Rwtypes.Readerwritertypes.Byte)\n            throw new Exception($\"Byte value read requested '{Rwtypes.GetType(t)}' value found.\");\n        return BaseReader.ReadByte();\n    }\n    public sbyte ReadSByte()\n    {\n        if (BaseReader.PeekChar() == -1)\n            throw new Exception(\"Primary short byte value does not exist or can not be read.\");\n        var t = BaseReader.ReadInt32();\n        if (t != (int) Rwtypes.Readerwritertypes.ShortByte)\n            throw new Exception($\"Short Byte value read requested '{Rwtypes.GetType(t)}' value found.\");\n        return BaseReader.ReadSByte();\n    }\n    public short ReadShort()\n    {\n        if (BaseReader.PeekChar() == -1)\n            throw new Exception(\"Primary Short value does not exist or can not be read.\");\n        var t = BaseReader.ReadInt32();\n        if (t != (int) Rwtypes.Readerwritertypes.Short)\n            throw new Exception($\"Short value read requested '{Rwtypes.GetType(t)}' value found.\");\n        return BaseReader.ReadInt16();\n    }\n    public ushort ReadUShort()\n    {\n        if (BaseReader.PeekChar() == -1)\n            throw new Exception(\"Primary Unsigned Short value does not exist or can not be read.\");\n        var t = BaseReader.ReadInt32();\n        if (t != (int) Rwtypes.Readerwritertypes.UnsignedShort)\n            throw new Exception($\"Unsigned Short value read requested '{Rwtypes.GetType(t)}' value found.\");\n        return BaseReader.ReadUInt16();\n    }\n    public int ReadInt()\n    {\n        if (BaseReader.PeekChar() == -1)\n            throw new Exception(\"Primary Integer value does not exist or can not be read.\");\n        var t = BaseReader.ReadInt32();\n        if (t != (int) Rwtypes.Readerwritertypes.Integer)\n            throw new Exception($\"Integer value read requested '{Rwtypes.GetType(t)}' value found.\");\n        return BaseReader.ReadInt32();\n    }\n    public uint ReadUInt()\n    {\n        if (BaseReader.PeekChar() == -1)\n            throw new Exception(\"Primary Unsigned Integer value does not exist or can not be read.\");\n        var t = BaseReader.ReadInt32();\n        if (t != (int) Rwtypes.Readerwritertypes.UnsignedInteger)\n            throw new Exception($\"Unsigned Integer value read requested '{Rwtypes.GetType(t)}' value found.\");\n        return BaseReader.ReadUInt32();\n    }\n    public long ReadLong()\n    {\n        if (BaseReader.PeekChar() == -1)\n            throw new Exception(\"Primary Long value does not exist or can not be read.\");\n        var t = BaseReader.ReadInt32();\n        if (t != (int) Rwtypes.Readerwritertypes.Long)\n            throw new Exception($\"Long value read requested '{Rwtypes.GetType(t)}' value found.\");\n        return BaseReader.ReadInt64();\n    }\n    public ulong ReadULong()\n    {\n        if (BaseReader.PeekChar() == -1)\n            throw new Exception(\"Primary Unsigned Long value does not exist or can not be read.\");\n        var t = BaseReader.ReadInt32();\n        if (t != (int) Rwtypes.Readerwritertypes.UnsignedLong)\n            throw new Exception($\"Unsigned Long value read requested '{Rwtypes.GetType(t)}' value found.\");\n        return BaseReader.ReadUInt64();\n    }\n    public string ReadString()\n    {\n        if (BaseReader.PeekChar() == -1)\n            throw new Exception(\"Primary String value does not exist or can not be read.\");\n        var t = BaseReader.ReadInt32();\n        if (t != (int) Rwtypes.Readerwritertypes.String)\n            throw new Exception($\"String value read requested '{Rwtypes.GetType(t)}' value found.\");\n        return BaseReader.ReadString();\n    }\n    public float ReadFloat()\n    {\n        if (BaseReader.PeekChar() == -1)\n            throw new Exception(\"Primary Float value does not exist or can not be read.\");\n        var t = BaseReader.ReadInt32();\n        if (t != (int) Rwtypes.Readerwritertypes.Float)\n            throw new Exception($\"Float value read requested '{Rwtypes.GetType(t)}' value found.\");\n        return BaseReader.ReadSingle();\n    }\n    public double ReadDouble()\n    {\n        if (BaseReader.PeekChar() == -1)\n            throw new Exception(\"Primary Double value does not exist or can not be read.\");\n        var t = BaseReader.ReadInt32();\n        if (t != (int) Rwtypes.Readerwritertypes.Double)\n            throw new Exception($\"Double value read requested '{Rwtypes.GetType(t)}' value found.\");\n        return BaseReader.ReadDouble();\n    }\n    public decimal ReadDecimal()\n    {\n        if (BaseReader.PeekChar() == -1)\n            throw new Exception(\"Primary Decimal value does not exist or can not be read.\");\n        var t = BaseReader.ReadInt32();\n        if (t != (int) Rwtypes.Readerwritertypes.Decimal)\n            throw new Exception($\"Decimal value read requested '{Rwtypes.GetType(t)}' value found.\");\n        return BaseReader.ReadDecimal();\n    }\n    public BigInteger ReadBigInteger()\n    {\n        if (BaseReader.PeekChar() == -1)\n            throw new Exception(\"Primary BigInteger value does not exist or can not be read.\");\n        var t = BaseReader.ReadInt32();\n        if (t != (int) Rwtypes.Readerwritertypes.BigInteger)\n            throw new Exception($\"BigInteger value read requested '{Rwtypes.GetType(t)}' value found.\");\n        return new BigInteger(ReadBytes());\n    }\n    public BigRational ReadBigRational()\n    {\n        if (BaseReader.PeekChar() == -1)\n            throw new Exception(\"Primary BigRational value does not exist or can not be read.\");\n        var t = BaseReader.ReadInt32();\n        if (t != (int) Rwtypes.Readerwritertypes.BigRational)\n            throw new Exception($\"BigRational value read requested '{Rwtypes.GetType(t)}' value found.\");\n        var Num = new BigInteger(ReadBytes());\n        var Den = new BigInteger(ReadBytes());\n        return new BigRational(Num, Den);\n    }\n    public bool[] ReadBools()\n    {\n        if (BaseReader.PeekChar() == -1)\n            throw new Exception(\"Primary boolean array value does not exist or can not be read.\");\n        var t = BaseReader.ReadInt32();\n        if (t != (int) Rwtypes.Readerwritertypes.BooleanArray)\n            throw new Exception($\"Boolean Array value read requested '{Rwtypes.GetType(t)}' value found.\");\n        var len = BaseReader.ReadInt32();\n        var arr = new bool[len];\n        for (var i = 0; i &lt; len; ++i)\n            arr[i] = BaseReader.ReadBoolean();\n        return arr;\n    }\n    public char[] ReadChars()\n    {\n        if (BaseReader.PeekChar() == -1)\n            throw new Exception(\"Primary char array value does not exist or can not be read.\");\n        var t = BaseReader.ReadInt32();\n        if (t != (int) Rwtypes.Readerwritertypes.CharacterArray)\n            throw new Exception($\"Character array value read requested '{Rwtypes.GetType(t)}' value found.\");\n        var len = BaseReader.ReadInt32();\n        var arr = new char[len];\n        for (var i = 0; i &lt; len; ++i)\n            arr[i] = BaseReader.ReadChar();\n        return arr;\n    }\n    public byte[] ReadBytes()\n    {\n        if (BaseReader.PeekChar() == -1)\n            throw new Exception(\"Primary byte array value does not exist or can not be read.\");\n        var t = BaseReader.ReadInt32();\n        if (t != (int) Rwtypes.Readerwritertypes.ByteArray)\n            throw new Exception($\"Byte Array value read requested '{Rwtypes.GetType(t)}' value found.\");\n        var len = BaseReader.ReadInt32();\n        var arr = new byte[len];\n        for (var i = 0; i &lt; len; ++i)\n            arr[i] = BaseReader.ReadByte();\n        return arr;\n    }\n    public sbyte[] ReadSBytes()\n    {\n        if (BaseReader.PeekChar() == -1)\n            throw new Exception(\"Primary sbyte array value does not exist or can not be read.\");\n        var t = BaseReader.ReadInt32();\n        if (t != (int) Rwtypes.Readerwritertypes.ShortByteArray)\n            throw new Exception($\"Short Byte Array value read requested '{Rwtypes.GetType(t)}' value found.\");\n        var len = BaseReader.ReadInt32();\n        var arr = new sbyte[len];\n        for (var i = 0; i &lt; len; ++i)\n            arr[i] = BaseReader.ReadSByte();\n        return arr;\n    }\n    public short[] ReadShorts()\n    {\n        if (BaseReader.PeekChar() == -1)\n            throw new Exception(\"Primary short array value does not exist or can not be read.\");\n        var t = BaseReader.ReadInt32();\n        if (t != (int) Rwtypes.Readerwritertypes.ShortArray)\n            throw new Exception($\"Short Array value read requested '{Rwtypes.GetType(t)}' value found.\");\n        var len = BaseReader.ReadInt32();\n        var arr = new short[len];\n        for (var i = 0; i &lt; len; ++i)\n            arr[i] = BaseReader.ReadInt16();\n        return arr;\n    }\n    public ushort[] ReadUShorts()\n    {\n        if (BaseReader.PeekChar() == -1)\n            throw new Exception(\"Primary unsigned short array value does not exist or can not be read.\");\n        var t = BaseReader.ReadInt32();\n        if (t != (int) Rwtypes.Readerwritertypes.UnsignedShortArray)\n            throw new Exception($\"Unsigned Short Array value read requested '{Rwtypes.GetType(t)}' value found.\");\n        var len = BaseReader.ReadInt32();\n        var arr = new ushort[len];\n        for (var i = 0; i &lt; len; ++i)\n            arr[i] = BaseReader.ReadUInt16();\n        return arr;\n    }\n    public int[] ReadInts()\n    {\n        if (BaseReader.PeekChar() == -1)\n            throw new Exception(\"Primary integer array value does not exist or can not be read.\");\n        var t = BaseReader.ReadInt32();\n        if (t != (int) Rwtypes.Readerwritertypes.IntegerArray)\n            throw new Exception($\"Integer Array value read requested '{Rwtypes.GetType(t)}' value found.\");\n        var len = BaseReader.ReadInt32();\n        var arr = new int[len];\n        for (var i = 0; i &lt; len; ++i)\n            arr[i] = BaseReader.ReadInt32();\n        return arr;\n    }\n    public uint[] ReadUInts()\n    {\n        if (BaseReader.PeekChar() == -1)\n            throw new Exception(\"Primary unsigned integer array value does not exist or can not be read.\");\n        var t = BaseReader.ReadInt32();\n        if (t != (int) Rwtypes.Readerwritertypes.UnsignedInteger)\n            throw new Exception($\"Unsigned Integer Array value read requested '{Rwtypes.GetType(t)}' value found.\");\n        var len = BaseReader.ReadInt32();\n        var arr = new uint[len];\n        for (var i = 0; i &lt; len; ++i)\n            arr[i] = BaseReader.ReadUInt32();\n        return arr;\n    }\n    public long[] ReadLongs()\n    {\n        if (BaseReader.PeekChar() == -1)\n            throw new Exception(\"Primary long array value does not exist or can not be read.\");\n        var t = BaseReader.ReadInt32();\n        if (t != (int) Rwtypes.Readerwritertypes.LongArray)\n            throw new Exception($\"Long Array value read requested '{Rwtypes.GetType(t)}' value found.\");\n        var len = BaseReader.ReadInt32();\n        var arr = new long[len];\n        for (var i = 0; i &lt; len; ++i)\n            arr[i] = BaseReader.ReadInt64();\n        return arr;\n    }\n    public ulong[] ReadULongs()\n    {\n        if (BaseReader.PeekChar() == -1)\n            throw new Exception(\"Primary unsigned long array value does not exist or can not be read.\");\n        var t = BaseReader.ReadInt32();\n        if (t != (int) Rwtypes.Readerwritertypes.UnsignedLongArray)\n            throw new Exception($\"Unsigned Long Array value read requested '{Rwtypes.GetType(t)}' value found.\");\n        var len = BaseReader.ReadInt32();\n        var arr = new ulong[len];\n        for (var i = 0; i &lt; len; ++i)\n            arr[i] = BaseReader.ReadUInt64();\n        return arr;\n    }\n    public string[] ReadStrings()\n    {\n        if (BaseReader.PeekChar() == -1)\n            throw new Exception(\"Primary string array value does not exist or can not be read.\");\n        var t = BaseReader.ReadInt32();\n        if (t != (int) Rwtypes.Readerwritertypes.StringArray)\n            throw new Exception($\"String Array value read requested '{Rwtypes.GetType(t)}' value found.\");\n        var len = BaseReader.ReadInt32();\n        var arr = new string[len];\n        for (var i = 0; i &lt; len; ++i)\n            arr[i] = BaseReader.ReadString();\n        return arr;\n    }\n    public float[] ReadFloats()\n    {\n        if (BaseReader.PeekChar() == -1)\n            throw new Exception(\"Primary float array value does not exist or can not be read.\");\n        var t = BaseReader.ReadInt32();\n        if (t != (int) Rwtypes.Readerwritertypes.FloatArray)\n            throw new Exception($\"Float Array value read requested '{Rwtypes.GetType(t)}' value found.\");\n        var len = BaseReader.ReadInt32();\n        var arr = new float[len];\n        for (var i = 0; i &lt; len; ++i)\n            arr[i] = BaseReader.ReadSingle();\n        return arr;\n    }\n    public double[] ReadDoubles()\n    {\n        if (BaseReader.PeekChar() == -1)\n            throw new Exception(\"Primary double array value does not exist or can not be read.\");\n        var t = BaseReader.ReadInt32();\n        if (t != (int) Rwtypes.Readerwritertypes.DoubleArray)\n            throw new Exception($\"Double Array value read requested '{Rwtypes.GetType(t)}' value found.\");\n        var len = BaseReader.ReadInt32();\n        var arr = new double[len];\n        for (var i = 0; i &lt; len; ++i)\n            arr[i] = BaseReader.ReadDouble();\n        return arr;\n    }\n    public decimal[] ReadDecimals()\n    {\n        if (BaseReader.PeekChar() == -1)\n            throw new Exception(\"Primary decimal array value does not exist or can not be read.\");\n        var t = BaseReader.ReadInt32();\n        if (t != (int) Rwtypes.Readerwritertypes.DecimalArray)\n            throw new Exception($\"Decimal Array value read requested '{Rwtypes.GetType(t)}' value found.\");\n        var len = BaseReader.ReadInt32();\n        var arr = new decimal[len];\n        for (var i = 0; i &lt; len; ++i)\n            arr[i] = BaseReader.ReadDecimal();\n        return arr;\n    }\n    public BigInteger[] ReadBigIntegers()\n    {\n        if (BaseReader.PeekChar() == -1)\n            throw new Exception(\"Primary BigInteger array value does not exist or can not be read.\");\n        var t = BaseReader.ReadInt32();\n        if (t != (int) Rwtypes.Readerwritertypes.BigIntegerArray)\n            throw new Exception($\"BigInteger Array value read requested '{Rwtypes.GetType(t)}' value found.\");\n        var len = BaseReader.ReadInt32();\n        var arr = new BigInteger[len];\n        for (var i = 0; i &lt; len; ++i)\n            arr[i] = ReadBigInteger();\n        return arr;\n    }\n    public BigRational[] ReadBigRationals()\n    {\n        if (BaseReader.PeekChar() == -1)\n            throw new Exception(\"Primary BigRational array value does not exist or can not be read.\");\n        var t = BaseReader.ReadInt32();\n        if (t != (int) Rwtypes.Readerwritertypes.BigRationalArray)\n            throw new Exception($\"BigRational Array value read requested '{Rwtypes.GetType(t)}' value found.\");\n        var len = BaseReader.ReadInt32();\n        var arr = new BigRational[len];\n        for (var i = 0; i &lt; len; ++i)\n            arr[i] = ReadBigRational();\n        return arr;\n    }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Simplified Binary Reader Example Code:<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[4,3],"_links":{"self":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/60"}],"collection":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/comments?post=60"}],"version-history":[{"count":3,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/60\/revisions"}],"predecessor-version":[{"id":67,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/60\/revisions\/67"}],"wp:attachment":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/media?parent=60"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/categories?post=60"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/tags?post=60"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}