Exemple #1
0
        private int DecodeContByteData(ArraySegment <byte> buf)
        {
            var offset = buf.Offset;
            var count  = buf.Count;

            var available = count;
            var need      = this._octetLength - this._bufferOffset;

            var toCopy = available >= need ? need : available;

            if (toCopy > 0)
            {
                Array.Copy(buf.Array, offset, this._stringBuffer, this._bufferOffset, toCopy);
                this._bufferOffset += toCopy;

                offset += toCopy;
                count  -= toCopy;
            }

            if (this._bufferOffset == this._octetLength)
            {
                var view = new ArraySegment <byte>(
                    this._stringBuffer, 0, this._octetLength
                    );
                if (this._huffman)
                {
                    this.Result = Huffman.Decode(view, _bufferPool);
                }
                else
                {
                    this.Result =
                        Encoding.ASCII.GetString(view.Array, view.Offset, view.Count);
                }
                this.Done = true;

                this.StringLength = this.Result.Length;
                this._state       = State.StartDecode;

                _bufferPool.Return(this._stringBuffer);
                this._stringBuffer = null;
            }

            return(offset - buf.Offset);
        }
Exemple #2
0
        /// <summary>
        /// 将给定的字符串编码到目标缓冲区中
        /// </summary>
        /// <param name="buf"></param>
        /// <param name="value"></param>
        /// <param name="valueByteLen"></param>
        /// <param name="huffman"></param>
        /// <returns></returns>
        public static int EncodeInto(
            ArraySegment <byte> buf,
            string value, int valueByteLen, HuffmanStrategy huffman)
        {
            var offset = buf.Offset;
            var free   = buf.Count;

            if (free < 1 + valueByteLen)
            {
                return(-1);
            }

            var encodedByteLen       = valueByteLen;
            var requiredHuffmanBytes = 0;
            var useHuffman           = huffman == HuffmanStrategy.Always;

            byte[] huffmanInputBuf = null;


            if (huffman == HuffmanStrategy.Always || huffman == HuffmanStrategy.IfSmaller)
            {
                huffmanInputBuf      = Encoding.ASCII.GetBytes(value);
                requiredHuffmanBytes = Huffman.EncodedLength(
                    new ArraySegment <byte>(huffmanInputBuf));
                if (huffman == HuffmanStrategy.IfSmaller && requiredHuffmanBytes < encodedByteLen)
                {
                    useHuffman = true;
                }
            }

            if (useHuffman)
            {
                encodedByteLen = requiredHuffmanBytes;
            }


            var prefixContent = useHuffman ? (byte)0x80 : (byte)0;
            var used          = IntEncoder.EncodeInto(
                new ArraySegment <byte>(buf.Array, offset, free),
                encodedByteLen, prefixContent, 7);

            if (used == -1)
            {
                return(-1);
            }
            offset += used;
            free   -= used;

            if (useHuffman)
            {
                if (free < requiredHuffmanBytes)
                {
                    return(-1);
                }

                used = Huffman.EncodeInto(
                    new ArraySegment <byte>(buf.Array, offset, free),
                    new ArraySegment <byte>(huffmanInputBuf));
                if (used == -1)
                {
                    return(-1);
                }
                offset += used;
            }
            else
            {
                if (free < valueByteLen)
                {
                    return(-1);
                }

                used = Encoding.ASCII.GetBytes(
                    value, 0, value.Length, buf.Array, offset);
                offset += used;
            }

            return(offset - buf.Offset);
        }