コード例 #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);
        }
コード例 #2
0
 public BinaryStream(byte[] buffer, bool reset = true)
 {
     this.buffer = new MemorySpan(buffer);
     if (reset)
     {
         this.Reset();
     }
 }
コード例 #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);
        }
コード例 #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);
        }
コード例 #5
0
        public static ushort ReadLShort(ref MemorySpan span)
        {
            byte[] bytes = span.ReadBytes(2);

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

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

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

            span.WriteBytes(bytes);
        }
コード例 #7
0
 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);
 }
コード例 #8
0
        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));
        }
コード例 #9
0
        public static void WriteLDouble(ref MemorySpan span, double value)
        {
            byte[] bytes = BitConverter.GetBytes(value);

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

            span.WriteBytes(bytes);
        }
コード例 #10
0
        public static double ReadLDouble(ref MemorySpan span)
        {
            byte[] bytes = span.ReadBytes(8);

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

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

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

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

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

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

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

            return(BitConverter.ToUInt32(bytes, 0));
        }
コード例 #14
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));
        }
コード例 #15
0
        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));
        }
コード例 #16
0
        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);
        }
コード例 #17
0
        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());
        }
コード例 #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);
        }
コード例 #19
0
 public static void WriteSInt32(ref MemorySpan span, int value)
 {
     WriteRawVarInt32(ref span, EncodeZigZag32(value));
 }
コード例 #20
0
 public static int ReadInt32(ref MemorySpan span)
 {
     return((int)ReadRawVarInt32(ref span, 5));
 }
コード例 #21
0
 public static ulong ReadUInt64(ref MemorySpan span)
 {
     return(ReadRawVarInt64(ref span, 10));
 }
コード例 #22
0
 public static long ReadSInt64(ref MemorySpan span)
 {
     return(DecodeZigZag64(ReadRawVarInt64(ref span, 10)));
 }
コード例 #23
0
 public static void WriteUInt64(ref MemorySpan span, ulong value)
 {
     WriteRawVarInt64(ref span, value);
 }
コード例 #24
0
 public static void WriteSInt64(ref MemorySpan span, long value)
 {
     WriteRawVarInt64(ref span, EncodeZigZag64(value));
 }
コード例 #25
0
 public static long ReadInt64(ref MemorySpan span)
 {
     return((long)ReadRawVarInt64(ref span, 10));
 }
コード例 #26
0
 public static void WriteInt64(ref MemorySpan span, long value)
 {
     WriteRawVarInt64(ref span, (ulong)value);
 }
コード例 #27
0
 public static uint ReadUInt32(ref MemorySpan span)
 {
     return(ReadRawVarInt32(ref span, 5));
 }
コード例 #28
0
 public static void WriteUInt32(ref MemorySpan span, uint value)
 {
     WriteRawVarInt32(ref span, value);
 }
コード例 #29
0
 public void SetBuffer(byte[] buffer)
 {
     this.buffer = new MemorySpan(buffer);
     this.Reset();
 }
コード例 #30
0
 public static int ReadSInt32(ref MemorySpan span)
 {
     return(DecodeZigZag32(ReadRawVarInt32(ref span, 5)));
 }