示例#1
0
            public override void Flush()
            {
                // _pos represents the size

                try
                {
                    if (_data != null)
                    {
                        Debug.Assert(_stream.OldEncoding);
                        Debug.Assert(_pos <= _data.Length);
                        _stream.WriteSize(_pos); // 1 byte size length
                        _stream.WriteByteSpan(_data.AsSpan(0, _pos));
                    }
                    else
                    {
                        // Patch previously-written dummy value.

                        if (_stream.OldEncoding)
                        {
                            Debug.Assert(_pos >= 255);
                            Span <byte> data = stackalloc byte[5];
                            data[0] = 255;
                            OutputStream.WriteInt(_pos, data.Slice(1, 4));
                            _stream.RewriteByteSpan(data, _startPos);
                        }
                        else
                        {
                            Span <byte> data = stackalloc byte[OutputStream.DefaultSizeLength];
                            OutputStream.WriteFixedLengthSize20(_pos, data);
                            _stream.RewriteByteSpan(data, _startPos);
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw new IOException("could not flush stream", ex);
                }
            }
示例#2
0
        private protected void WriteBinaryContext(OutputStream ostr)
        {
            Debug.Assert(Protocol == Protocol.Ice2);
            Debug.Assert(ostr.Encoding == Encoding.V20);

            int sizeLength =
                OutputStream.GetSizeLength20(InitialBinaryContext.Count + (_binaryContextOverride?.Count ?? 0));

            int size = 0;

            OutputStream.Position start = ostr.StartFixedLengthSize(sizeLength);

            // First write the overrides, then the InitialBinaryContext entries that were not overridden.

            if (_binaryContextOverride is Dictionary <int, Action <OutputStream> > binaryContextOverride)
            {
                foreach ((int key, Action <OutputStream> action) in binaryContextOverride)
                {
                    ostr.WriteVarInt(key);
                    OutputStream.Position startValue = ostr.StartFixedLengthSize(2);
                    action(ostr);
                    ostr.EndFixedLengthSize(startValue, 2);
                    size++;
                }
            }
            foreach ((int key, ReadOnlyMemory <byte> value) in InitialBinaryContext)
            {
                if (_binaryContextOverride == null || !_binaryContextOverride.ContainsKey(key))
                {
                    ostr.WriteVarInt(key);
                    ostr.WriteSize(value.Length);
                    ostr.WriteByteSpan(value.Span);
                    size++;
                }
            }
            ostr.RewriteFixedLengthSize20(size, start, sizeLength);
        }