Beispiel #1
0
        private static ulong ReadRawVarInt64(ref MemorySpan span, int maxSize)
        {
            List <byte> bytes = new List <byte>();

            ulong result = 0;
            int   j      = 0;
            int   b0;

            do
            {
                b0 = span.ReadByte();
                bytes.Add((byte)b0);
                if (b0 < 0)
                {
                    throw new EndOfStreamException("Not enough bytes for VarInt");
                }

                result |= (ulong)(b0 & 0x7f) << j++ *7;

                if (j > maxSize)
                {
                    throw new OverflowException("VarInt too big");
                }
            } while ((b0 & 0x80) == 0x80);

            byte[] byteArray = bytes.ToArray();

            return(result);
        }
 public BinaryStream(byte[] buffer, bool reset = true)
 {
     this.buffer = new MemorySpan(buffer);
     if (reset)
     {
         this.Reset();
     }
 }
Beispiel #3
0
        private static void WriteRawVarInt64(ref MemorySpan span, ulong value)
        {
            while ((value & 0xFFFFFFFFFFFFFF80) != 0)
            {
                span.WriteByte((byte)((value & 0x7F) | 0x80));
                value >>= 7;
            }

            span.WriteByte((byte)value);
        }
Beispiel #4
0
        private static void WriteRawVarInt32(ref MemorySpan span, uint value)
        {
            while ((value & -128) != 0)
            {
                span.WriteByte((byte)((value & 0x7F) | 0x80));
                value >>= 7;
            }

            span.WriteByte((byte)value);
        }
        public static ushort ReadLShort(ref MemorySpan span)
        {
            byte[] bytes = span.ReadBytes(2);

            if (!BitConverter.IsLittleEndian)
            {
                Array.Reverse(bytes);
            }

            return(BitConverter.ToUInt16(bytes, 0));
        }
        public static void WriteUShort(ref MemorySpan span, ushort value)
        {
            byte[] bytes = BitConverter.GetBytes(value);

            if (BitConverter.IsLittleEndian)
            {
                Array.Reverse(bytes);
            }

            span.WriteBytes(bytes);
        }
 public static void WriteString(ref MemorySpan span, string value)
 {
     byte[] buf = Encoding.UTF8.GetBytes(value);
     if (string.IsNullOrEmpty(value))
     {
         WriteUVarInt(ref span, 0);
         return;
     }
     WriteUVarInt(ref span, (uint)buf.Length);
     WriteBytes(ref span, buf);
 }
        public static void WriteFixedString(ref MemorySpan span, string value)
        {
            if (string.IsNullOrEmpty(value))
            {
                WriteShort(ref span, 0);
                return;
            }

            WriteShort(ref span, (short)value.Length);
            WriteBytes(ref span, Encoding.UTF8.GetBytes(value));
        }
        public static void WriteLDouble(ref MemorySpan span, double value)
        {
            byte[] bytes = BitConverter.GetBytes(value);

            if (!BitConverter.IsLittleEndian)
            {
                Array.Reverse(bytes);
            }

            span.WriteBytes(bytes);
        }
        public static double ReadLDouble(ref MemorySpan span)
        {
            byte[] bytes = span.ReadBytes(8);

            if (!BitConverter.IsLittleEndian)
            {
                Array.Reverse(bytes);
            }

            return(BitConverter.ToDouble(bytes, 0));
        }
        public static float ReadLFloat(ref MemorySpan span)
        {
            byte[] bytes = span.ReadBytes(4);

            if (!BitConverter.IsLittleEndian)
            {
                Array.Reverse(bytes);
            }

            return(BitConverter.ToSingle(bytes, 0));
        }
        public static ulong ReadLLong(ref MemorySpan span)
        {
            byte[] bytes = span.ReadBytes(8);

            if (!BitConverter.IsLittleEndian)
            {
                Array.Reverse(bytes);
            }

            return(BitConverter.ToUInt64(bytes, 0));
        }
        public static uint ReadLInt(ref MemorySpan span)
        {
            byte[] bytes = span.ReadBytes(4);

            if (!BitConverter.IsLittleEndian)
            {
                Array.Reverse(bytes);
            }

            return(BitConverter.ToUInt32(bytes, 0));
        }
        public static string ReadString(ref MemorySpan span)
        {
            uint len = ReadUVarInt(ref span);

            if (len == 0)
            {
                return("");
            }

            byte[] b = ReadBytes(ref span, (int)len);
            return(Encoding.UTF8.GetString(b));
        }
        public static string ReadFixedString(ref MemorySpan span)
        {
            short len = ReadShort(ref span);

            if (len <= 0)
            {
                return("");
            }

            byte[] b = ReadBytes(ref span, len);
            return(Encoding.UTF8.GetString(b));
        }
        public static void WriteLTriad(ref MemorySpan span, int value)
        {
            if (value > Int24_Max)
            {
                throw new OverflowException("Not Int24 Value!");
            }

            byte[] bytes = new byte[3]
            {
                (byte)value,
                ((byte)(value >> 8)),
                ((byte)(value >> 16))
            };

            span.WriteBytes(bytes);
        }
        public static byte[][] SplitBytes(MemorySpan span, int length)
        {
            List <byte[]> splits = new List <byte[]>();

            while (span.Offset < span.Length)
            {
                if ((span.Length - span.Offset) >= length)
                {
                    splits.Add(span.ReadBytes(length));
                }
                else
                {
                    splits.Add(span.ReadBytes());
                }
            }

            return(splits.ToArray());
        }
Beispiel #18
0
        private static uint ReadRawVarInt32(ref MemorySpan span, int maxSize)
        {
            uint result = 0;
            int  j      = 0;
            int  b0;

            do
            {
                b0 = span.ReadByte();
                if (b0 < 0)
                {
                    throw new EndOfStreamException("Not enough bytes for VarInt");
                }

                result |= (uint)(b0 & 0x7f) << j++ *7;

                if (j > maxSize)
                {
                    throw new OverflowException("VarInt too big");
                }
            } while ((b0 & 0x80) == 0x80);

            return(result);
        }
Beispiel #19
0
 public static void WriteSInt32(ref MemorySpan span, int value)
 {
     WriteRawVarInt32(ref span, EncodeZigZag32(value));
 }
Beispiel #20
0
 public static int ReadInt32(ref MemorySpan span)
 {
     return((int)ReadRawVarInt32(ref span, 5));
 }
Beispiel #21
0
 public static ulong ReadUInt64(ref MemorySpan span)
 {
     return(ReadRawVarInt64(ref span, 10));
 }
Beispiel #22
0
 public static long ReadSInt64(ref MemorySpan span)
 {
     return(DecodeZigZag64(ReadRawVarInt64(ref span, 10)));
 }
Beispiel #23
0
 public static void WriteUInt64(ref MemorySpan span, ulong value)
 {
     WriteRawVarInt64(ref span, value);
 }
Beispiel #24
0
 public static void WriteSInt64(ref MemorySpan span, long value)
 {
     WriteRawVarInt64(ref span, EncodeZigZag64(value));
 }
Beispiel #25
0
 public static long ReadInt64(ref MemorySpan span)
 {
     return((long)ReadRawVarInt64(ref span, 10));
 }
Beispiel #26
0
 public static void WriteInt64(ref MemorySpan span, long value)
 {
     WriteRawVarInt64(ref span, (ulong)value);
 }
Beispiel #27
0
 public static uint ReadUInt32(ref MemorySpan span)
 {
     return(ReadRawVarInt32(ref span, 5));
 }
Beispiel #28
0
 public static void WriteUInt32(ref MemorySpan span, uint value)
 {
     WriteRawVarInt32(ref span, value);
 }
Beispiel #29
0
 public void SetBuffer(byte[] buffer)
 {
     this.buffer = new MemorySpan(buffer);
     this.Reset();
 }
Beispiel #30
0
 public static int ReadSInt32(ref MemorySpan span)
 {
     return(DecodeZigZag32(ReadRawVarInt32(ref span, 5)));
 }