namespace SHNDecrypt
{
    using System;
    using System.IO;
    using System.Text;

    internal class BinaryReaderEx : BinaryReader
    {
        private static byte[] Buffer = new byte[0x100];
        private const int BufferLength = 0x100;

        public BinaryReaderEx(Stream input) : base(input)
        {
        }

        private string _ReadString(uint bytes)
        {
            string str = string.Empty;
            if (bytes > 0x100)
            {
                str = this.ReadString((uint) (bytes - 0x100));
            }
            this.Read(Buffer, 0, (int) bytes);
            string str2 = Encoding.GetEncoding(Program.eT).GetString(Buffer, 0, (int) bytes);
            return (str + str2);
        }

        public override string ReadString()
        {
            int count = 0;
            for (byte i = this.ReadByte(); i != 0; i = this.ReadByte())
            {
                Buffer[count++] = i;
                if (count >= 0x100)
                {
                    break;
                }
            }
            string str = Encoding.GetEncoding(Program.eT).GetString(Buffer, 0, count);
            if (count == 0x100)
            {
                str = str + this.ReadString();
            }
            return str;
        }

        public string ReadString(int bytes)
        {
            if (bytes > 0)
            {
                return this.ReadString((uint) bytes);
            }
            return string.Empty;
        }

        public string ReadString(uint bytes) => 
            this._ReadString(bytes).TrimEnd(new char[1])

        public long Length =>
            this.BaseStream.Length
    }
}

