예제 #1
0
        /// <inheritdoc />
        protected internal override void Encode(IChannelHandlerContext context, IByteBuffer message, List <object> output)
        {
            int length         = message.ReadableBytes + this.lengthAdjustment;
            var lengthFieldLen = this.lengthFieldLength;

            if (this.lengthFieldIncludesLengthFieldLength)
            {
                length += lengthFieldLen;
            }

            uint nlen = unchecked ((uint)length);

            if (nlen > SharedConstants.TooBigOrNegative)
            {
                CThrowHelper.ThrowArgumentException_LessThanZero(length);
            }

            switch (lengthFieldLen)
            {
            case 1:
                if (nlen >= 256u)
                {
                    CThrowHelper.ThrowArgumentException_Byte(length);
                }
                output.Add(context.Allocator.Buffer(1).WriteByte((byte)length));
                break;

            case 2:
                if (nlen >= 65536u)
                {
                    CThrowHelper.ThrowArgumentException_Short(length);
                }
                output.Add(context.Allocator.Buffer(2).WriteShort((short)length));
                break;

            case 3:
                if (nlen >= 16777216u)
                {
                    CThrowHelper.ThrowArgumentException_Medium(length);
                }
                output.Add(context.Allocator.Buffer(3).WriteMedium(length));
                break;

            case 4:
                output.Add(context.Allocator.Buffer(4).WriteInt(length));
                break;

            case 8:
                output.Add(context.Allocator.Buffer(8).WriteLong(length));
                break;

            default:
                CThrowHelper.ThrowException_UnknownLen(); break;
            }

            output.Add(message.Retain());
        }