当前位置: 首页>>代码示例>>C#>>正文


C# ByteBuffer.Shrink方法代码示例

本文整理汇总了C#中System.ByteBuffer.Shrink方法的典型用法代码示例。如果您正苦于以下问题:C# ByteBuffer.Shrink方法的具体用法?C# ByteBuffer.Shrink怎么用?C# ByteBuffer.Shrink使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.ByteBuffer的用法示例。


在下文中一共展示了ByteBuffer.Shrink方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: WriteList

        /// <summary>
        /// Writes a list value to a buffer.
        /// </summary>
        /// <param name="buffer">The buffer to write.</param>
        /// <param name="value">The list value.</param>
        /// <param name="smallEncoding">if true, try using small encoding if possible.</param>
        public static void WriteList(ByteBuffer buffer, IList value, bool smallEncoding)
        {
            if (value == null)
            {
                AmqpBitConverter.WriteUByte(buffer, FormatCode.Null);
            }
            else
            {
                // trim tailing nulls
                int last = value.Count - 1;
                while (last >= 0 && value[last] == null)
                {
                    --last;
                }

                if (last < 0 && smallEncoding)
                {
                    AmqpBitConverter.WriteUByte(buffer, FormatCode.List0);
                }
                else
                {
                    int pos = buffer.WritePos;
                    AmqpBitConverter.WriteUByte(buffer, 0);
                    AmqpBitConverter.WriteUInt(buffer, 0);
                    AmqpBitConverter.WriteUInt(buffer, 0);

                    for (int i = 0; i <= last; ++i)
                    {
                        Encoder.WriteObject(buffer, value[i], smallEncoding);
                    }

                    int size = buffer.WritePos - pos - 9;
                    int count = last + 1;

                    if (smallEncoding && size < byte.MaxValue && count <= byte.MaxValue)
                    {
                        buffer.Buffer[pos] = FormatCode.List8;
                        buffer.Buffer[pos + 1] = (byte)(size + 1);
                        buffer.Buffer[pos + 2] = (byte)count;
                        Array.Copy(buffer.Buffer, pos + 9, buffer.Buffer, pos + 3, size);
                        buffer.Shrink(6);
                    }
                    else
                    {
                        buffer.Buffer[pos] = FormatCode.List32;
                        AmqpBitConverter.WriteInt(buffer.Buffer, pos + 1, size + 4);
                        AmqpBitConverter.WriteInt(buffer.Buffer, pos + 5, count);
                    }
                }
            }
        }
开发者ID:timhermann,项目名称:amqpnetlite,代码行数:57,代码来源:Encoder.cs

示例2: WriteMap

        /// <summary>
        /// Writes a map value to a buffer.
        /// </summary>
        /// <param name="buffer">The buffer to write.</param>
        /// <param name="value">The map value.</param>
        /// <param name="smallEncoding">if true, try using small encoding if possible.</param>
        public static void WriteMap(ByteBuffer buffer, Map value, bool smallEncoding)
        {
            if (value == null)
            {
                AmqpBitConverter.WriteUByte(buffer, FormatCode.Null);
            }
            else
            {
                int pos = buffer.WritePos;
                AmqpBitConverter.WriteUByte(buffer, 0);
                AmqpBitConverter.WriteUInt(buffer, 0);
                AmqpBitConverter.WriteUInt(buffer, 0);

                foreach (var key in value.Keys)
                {
                    Encoder.WriteObject(buffer, key);
                    Encoder.WriteObject(buffer, value[key]);
                }

                int size = buffer.WritePos - pos - 9;
                int count = value.Count * 2;

                if (smallEncoding && size < byte.MaxValue && count <= byte.MaxValue)
                {
                    buffer.Buffer[pos] = FormatCode.Map8;
                    buffer.Buffer[pos + 1] = (byte)(size + 1);
                    buffer.Buffer[pos + 2] = (byte)count;
                    Array.Copy(buffer.Buffer, pos + 9, buffer.Buffer, pos + 3, size);
                    buffer.Shrink(6);
                }
                else
                {
                    buffer.Buffer[pos] = FormatCode.Map32;
                    AmqpBitConverter.WriteInt(buffer.Buffer, pos + 1, size + 4);
                    AmqpBitConverter.WriteInt(buffer.Buffer, pos + 5, count);
                }
            }
        }
开发者ID:timhermann,项目名称:amqpnetlite,代码行数:44,代码来源:Encoder.cs

示例3: WriteArray

        /// <summary>
        /// Writes an array value to a buffer.
        /// </summary>
        /// <param name="buffer">The buffer to write.</param>
        /// <param name="value">The array value.</param>
        public static void WriteArray(ByteBuffer buffer, Array value)
        {
            if (value == null)
            {
                AmqpBitConverter.WriteUByte(buffer, FormatCode.Null);
            }
            else
            {
                int count = value.Length;
                Fx.Assert(count > 0, "must have at least 1 element in array");
                int pos = buffer.WritePos;
                AmqpBitConverter.WriteUByte(buffer, 0);
                AmqpBitConverter.WriteUInt(buffer, 0);
                AmqpBitConverter.WriteUInt(buffer, 0);

                for (int i = 0; i < count; ++i)
                {
                    object item = value.GetValue(i);
                    if (i == 0)
                    {
                        Encoder.WriteObject(buffer, item, false);
                    }
                    else
                    {
                        int lastPos = buffer.WritePos - 1;
                        byte lastByte = buffer.Buffer[lastPos];
                        buffer.Shrink(1);
                        Encoder.WriteObject(buffer, item, false);
                        buffer.Buffer[lastPos] = lastByte;
                    }
                }

                int size = buffer.WritePos - pos - 9;

                if (size < byte.MaxValue && count <= byte.MaxValue)
                {
                    buffer.Buffer[pos] = FormatCode.Array8;
                    buffer.Buffer[pos + 1] = (byte)(size + 1);
                    buffer.Buffer[pos + 2] = (byte)count;
                    Array.Copy(buffer.Buffer, pos + 9, buffer.Buffer, pos + 3, size);
                    buffer.Shrink(6);
                }
                else
                {
                    buffer.Buffer[pos] = FormatCode.Array32;
                    AmqpBitConverter.WriteInt(buffer.Buffer, pos + 1, size + 4);
                    AmqpBitConverter.WriteInt(buffer.Buffer, pos + 5, count);
                }
            }
        }
开发者ID:timhermann,项目名称:amqpnetlite,代码行数:55,代码来源:Encoder.cs


注:本文中的System.ByteBuffer.Shrink方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。