Example #1
0
        /// <summary>
        /// Writes an unsigned 16-bit integer to the stream; supported wire-types: Variant, Fixed32, Fixed64
        /// </summary>
        public static void WriteUInt32(uint value, ProtoWriter writer, ref State state)
        {
            if (writer == null)
            {
                throw new ArgumentNullException(nameof(writer));
            }
            switch (writer.WireType)
            {
            case WireType.Fixed32:
                writer.ImplWriteFixed32(ref state, value);
                writer.AdvanceAndReset(4);
                return;

            case WireType.Fixed64:
                writer.ImplWriteFixed64(ref state, value);
                writer.AdvanceAndReset(8);
                return;

            case WireType.Variant:
                int bytes = writer.ImplWriteVarint32(ref state, value);
                writer.AdvanceAndReset(bytes);
                return;

            default:
                ThrowException(writer);
                break;
            }
        }
Example #2
0
        /// <summary>
        /// Writes a signed 64-bit integer to the stream; supported wire-types: Variant, Fixed32, Fixed64, SignedVariant
        /// </summary>
        public static void WriteInt64(long value, ProtoWriter writer, ref State state)
        {
            if (writer == null)
            {
                throw new ArgumentNullException(nameof(writer));
            }
            switch (writer.WireType)
            {
            case WireType.Fixed64:
                writer.ImplWriteFixed64(ref state, (ulong)value);
                writer.AdvanceAndReset(8);
                return;

            case WireType.Variant:
                writer.AdvanceAndReset(writer.ImplWriteVarint64(ref state, (ulong)value));
                return;

            case WireType.SignedVariant:
                writer.AdvanceAndReset(writer.ImplWriteVarint64(ref state, Zig(value)));
                return;

            case WireType.Fixed32:
                writer.ImplWriteFixed32(ref state, checked ((uint)(int)value));
                writer.AdvanceAndReset(4);
                return;

            default:
                ThrowException(writer);
                break;
            }
        }
Example #3
0
        /// <summary>
        /// Writes a double-precision number to the stream; supported wire-types: Fixed32, Fixed64
        /// </summary>
        public static void WriteDouble(double value, ProtoWriter writer, ref State state)
        {
            if (writer == null)
            {
                throw new ArgumentNullException(nameof(writer));
            }
            switch (writer.WireType)
            {
            case WireType.Fixed32:
                float f = (float)value;
                if (float.IsInfinity(f) && !double.IsInfinity(value))
                {
                    throw new OverflowException();
                }
                WriteSingle(f, writer, ref state);
                return;

            case WireType.Fixed64:
#if FEAT_SAFE
                writer.ImplWriteFixed64(ref state, (ulong)BitConverter.DoubleToInt64Bits(value));
#else
                unsafe { writer.ImplWriteFixed64(ref state, *(ulong *)&value); }
#endif
                writer.AdvanceAndReset(8);
                return;

            default:
                ThrowException(writer);
                return;
            }
        }
Example #4
0
        /// <summary>
        /// Writes a single-precision number to the stream; supported wire-types: Fixed32, Fixed64
        /// </summary>
        public static void WriteSingle(float value, ProtoWriter writer, ref State state)
        {
            if (writer == null)
            {
                throw new ArgumentNullException(nameof(writer));
            }
            switch (writer.WireType)
            {
            case WireType.Fixed32:
#if FEAT_SAFE
                writer.ImplWriteFixed32(ref state, BitConverter.ToUInt32(BitConverter.GetBytes(value), 0));
#else
                unsafe { writer.ImplWriteFixed32(ref state, *(uint *)&value); }
#endif
                writer.AdvanceAndReset(4);
                return;

            case WireType.Fixed64:
                WriteDouble(value, writer, ref state);
                return;

            default:
                ThrowException(writer);
                break;
            }
        }
Example #5
0
        /// <summary>
        /// Writes a string to the stream; supported wire-types: String
        /// </summary>
        public static void WriteString(string value, ProtoWriter writer, ref State state)
        {
            switch (writer.WireType)
            {
            case WireType.String:
                if (string.IsNullOrEmpty(value))
                {
                    writer.AdvanceAndReset(writer.ImplWriteVarint32(ref state, 0));
                }
                else
                {
                    var len = UTF8.GetByteCount(value);
                    writer.AdvanceAndReset(writer.ImplWriteVarint32(ref state, (uint)len) + len);
                    writer.ImplWriteString(ref state, value, len);
                }
                break;

            default:
                ThrowException(writer);
                break;
            }
        }
Example #6
0
        /// <summary>
        /// Writes a byte-array to the stream; supported wire-types: String
        /// </summary>
        public static void WriteBytes(System.Buffers.ReadOnlySequence <byte> data, ProtoWriter writer, ref State state)
        {
            int length = checked ((int)data.Length);

            switch (writer.WireType)
            {
            case WireType.Fixed32:
                if (length != 4)
                {
                    throw new ArgumentException(nameof(length));
                }
                writer.ImplWriteBytes(ref state, data);
                writer.AdvanceAndReset(4);
                return;

            case WireType.Fixed64:
                if (length != 8)
                {
                    throw new ArgumentException(nameof(length));
                }
                writer.ImplWriteBytes(ref state, data);
                writer.AdvanceAndReset(8);
                return;

            case WireType.String:
                writer.AdvanceAndReset(writer.ImplWriteVarint32(ref state, (uint)length) + length);
                if (length == 0)
                {
                    return;
                }
                writer.ImplWriteBytes(ref state, data);
                break;

            default:
                ThrowException(writer);
                break;
            }
        }
Example #7
0
        /// <summary>
        /// Writes a byte-array to the stream; supported wire-types: String
        /// </summary>
        public static void WriteBytes(byte[] data, int offset, int length, ProtoWriter writer, ref State state)
        {
            switch (writer.WireType)
            {
            case WireType.Fixed32:
                if (length != 4)
                {
                    throw new ArgumentException(nameof(length));
                }
                writer.ImplWriteBytes(ref state, data, offset, 4);
                writer.AdvanceAndReset(4);
                return;

            case WireType.Fixed64:
                if (length != 8)
                {
                    throw new ArgumentException(nameof(length));
                }
                writer.ImplWriteBytes(ref state, data, offset, 8);
                writer.AdvanceAndReset(8);
                return;

            case WireType.String:
                writer.AdvanceAndReset(writer.ImplWriteVarint32(ref state, (uint)length) + length);
                if (length == 0)
                {
                    return;
                }
                writer.ImplWriteBytes(ref state, data, offset, length);
                break;

            default:
                ThrowException(writer);
                break;
            }
        }