public void TestWriteInt64_negative()
 {
     byte[] bytes = new byte[8];
     NetworkOrderSerializer.WriteInt64(bytes, -0x123456789ABCDEF0);
     Check(bytes, new byte[] { 0xED, 0xCB, 0xA9, 0x87, 0x65, 0x43, 0x21, 0x10 });
 }
        public static int WriteFieldValue(Memory <byte> memory, object value)
        {
            if (value == null)
            {
                memory.Span[0] = (byte)'V';
                return(1);
            }

            Memory <byte> slice = memory.Slice(1);

            switch (value)
            {
            case string val:
                memory.Span[0] = (byte)'S';
                if (MemoryMarshal.TryGetArray(memory.Slice(5, Encoding.UTF8.GetByteCount(val)), out ArraySegment <byte> segment))
                {
                    NetworkOrderSerializer.WriteUInt32(slice, (uint)segment.Count);
                    Encoding.UTF8.GetBytes(val, 0, val.Length, segment.Array, segment.Offset);
                    return(segment.Count + 5);
                }

                throw new WireFormattingException("Unable to get array segment from memory.");

            case byte[] val:
                memory.Span[0] = (byte)'S';
                return(1 + WriteLongstr(slice, val));

            case int val:
                memory.Span[0] = (byte)'I';
                NetworkOrderSerializer.WriteInt32(slice, val);
                return(5);

            case uint val:
                memory.Span[0] = (byte)'i';
                NetworkOrderSerializer.WriteUInt32(slice, val);
                return(5);

            case decimal val:
                memory.Span[0] = (byte)'D';
                return(1 + WriteDecimal(slice, val));

            case AmqpTimestamp val:
                memory.Span[0] = (byte)'T';
                return(1 + WriteTimestamp(slice, val));

            case IDictionary val:
                memory.Span[0] = (byte)'F';
                return(1 + WriteTable(slice, val));

            case IList val:
                memory.Span[0] = (byte)'A';
                return(1 + WriteArray(slice, val));

            case byte val:
                memory.Span[0] = (byte)'B';
                memory.Span[1] = val;
                return(2);

            case sbyte val:
                memory.Span[0] = (byte)'b';
                memory.Span[1] = (byte)val;
                return(2);

            case double val:
                memory.Span[0] = (byte)'d';
                NetworkOrderSerializer.WriteDouble(slice, val);
                return(9);

            case float val:
                memory.Span[0] = (byte)'f';
                NetworkOrderSerializer.WriteSingle(slice, val);
                return(5);

            case long val:
                memory.Span[0] = (byte)'l';
                NetworkOrderSerializer.WriteInt64(slice, val);
                return(9);

            case short val:
                memory.Span[0] = (byte)'s';
                NetworkOrderSerializer.WriteInt16(slice, val);
                return(3);

            case bool val:
                memory.Span[0] = (byte)'t';
                memory.Span[1] = (byte)(val ? 1 : 0);
                return(2);

            case BinaryTableValue val:
                memory.Span[0] = (byte)'x';
                return(1 + WriteLongstr(slice, val.Bytes));

            default:
                throw new WireFormattingException($"Value of type '{value.GetType().Name}' cannot appear as table value", value);
            }
        }
 public void TestWriteInt64_positive()
 {
     byte[] bytes = new byte[8];
     NetworkOrderSerializer.WriteInt64(bytes, 0x123456789ABCDEF0);
     Check(bytes, new byte[] { 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0 });
 }
예제 #4
0
        public static int WriteFieldValue(Span <byte> span, object value)
        {
            if (value is null)
            {
                span[0] = (byte)'V';
                return(1);
            }

            Span <byte> slice = span.Slice(1);

            switch (value)
            {
            case string val:
                span[0] = (byte)'S';
                return(1 + WriteLongstr(slice, val));

            case byte[] val:
                span[0] = (byte)'S';
                return(1 + WriteLongstr(slice, val));

            case int val:
                span[0] = (byte)'I';
                NetworkOrderSerializer.WriteInt32(slice, val);
                return(5);

            case uint val:
                span[0] = (byte)'i';
                NetworkOrderSerializer.WriteUInt32(slice, val);
                return(5);

            case decimal val:
                span[0] = (byte)'D';
                return(1 + WriteDecimal(slice, val));

            case AmqpTimestamp val:
                span[0] = (byte)'T';
                return(1 + WriteTimestamp(slice, val));

            case IDictionary val:
                span[0] = (byte)'F';
                return(1 + WriteTable(slice, val));

            case IList val:
                span[0] = (byte)'A';
                return(1 + WriteArray(slice, val));

            case byte val:
                span[0] = (byte)'B';
                span[1] = val;
                return(2);

            case sbyte val:
                span[0] = (byte)'b';
                span[1] = (byte)val;
                return(2);

            case double val:
                span[0] = (byte)'d';
                NetworkOrderSerializer.WriteDouble(slice, val);
                return(9);

            case float val:
                span[0] = (byte)'f';
                NetworkOrderSerializer.WriteSingle(slice, val);
                return(5);

            case long val:
                span[0] = (byte)'l';
                NetworkOrderSerializer.WriteInt64(slice, val);
                return(9);

            case short val:
                span[0] = (byte)'s';
                NetworkOrderSerializer.WriteInt16(slice, val);
                return(3);

            case bool val:
                span[0] = (byte)'t';
                span[1] = (byte)(val ? 1 : 0);
                return(2);

            case BinaryTableValue val:
                span[0] = (byte)'x';
                return(1 + WriteLongstr(slice, val.Bytes));

            default:
                throw new WireFormattingException($"Value of type '{value.GetType().Name}' cannot appear as table value", value);
            }
        }