public void TestWriteInt16_negative()
 {
     byte[] bytes = new byte[2];
     NetworkOrderSerializer.WriteInt16(bytes, -0x1234);
     Check(bytes, new byte[] { 0xED, 0xCC });
 }
 public void TestWriteInt16_positive()
 {
     byte[] bytes = new byte[2];
     NetworkOrderSerializer.WriteInt16(bytes, 0x1234);
     Check(bytes, new byte[] { 0x12, 0x34 });
 }
Esempio n. 3
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);
            }
        }
        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 TestWriteInt16_negative()
 {
     byte[] bytes = new byte[2];
     NetworkOrderSerializer.WriteInt16(ref bytes.AsSpan().GetStart(), -0x1234);
     Check(bytes, new byte[] { 0xED, 0xCC });
 }