コード例 #1
0
        /// <summary>
        /// Writes a string in UTF-8 encoding with 7 bit encoded length prefix.
        /// </summary>
        /// <param name="text">The string to write.</param>
        public unsafe void Write(string text)
        {
            if (text == null)
            {
                if (size < 1)
                {
                    writer.flush(this);
                    next.Write(text);
                    return;
                }

                data[position++] = 0x00;
                size--;

                return;
            }

            int length = Encoding.UTF8.GetByteCount(text);

            if (length < 128)
            {
                if (size < length + 1)
                {
                    writer.flush(this);
                    next.Write(text);
                    return;
                }

                data[position++] = (byte)length;

                fixed(char *chars = text)
                fixed(byte *bData = data)
                Encoding.UTF8.GetBytes(chars, text.Length, bData + position, size - 1);

                size -= length + 1;
            }
            else if (length < 16384)
            {
                if (size < length + 2)
                {
                    writer.flush(this, length + 1024);
                    next.Write(text);
                    return;
                }

                data[position++] = (byte)(length | 0x80);
                data[position++] = (byte)(length >> 7);

                fixed(char *chars = text)
                fixed(byte *bData = data)
                Encoding.UTF8.GetBytes(chars, text.Length, bData + position, size - 2);

                size -= length + 2;
            }
            else if (length < 2097152)
            {
                if (size < length + 3)
                {
                    writer.flush(this, length + 1024);
                    next.Write(text);
                    return;
                }

                data[position++] = (byte)(length | 0x80);
                data[position++] = (byte)((length >> 7) | 0x80);
                data[position++] = (byte)(length >> 14);

                fixed(char *chars = text)
                fixed(byte *bData = data)
                Encoding.UTF8.GetBytes(chars, text.Length, bData + position, size - 3);

                size -= length + 3;
            }
            else if (length < 268435456)
            {
                if (size < length + 4)
                {
                    writer.flush(this, length + 1024);
                    next.Write(text);
                    return;
                }

                data[position++] = (byte)(length | 0x80);
                data[position++] = (byte)((length >> 7) | 0x80);
                data[position++] = (byte)((length >> 14) | 0x80);
                data[position++] = (byte)(length >> 21);

                fixed(char *chars = text)
                fixed(byte *bData = data)
                Encoding.UTF8.GetBytes(chars, text.Length, bData + position, size - 4);

                size -= length + 4;
            }
            else
            {
                if (size < length + 5)
                {
                    writer.flush(this, length + 5);
                    next.Write(text);
                    return;
                }

                data[position++] = (byte)(length | 0x80);
                data[position++] = (byte)((length >> 7) | 0x80);
                data[position++] = (byte)((length >> 14) | 0x80);
                data[position++] = (byte)((length >> 21) | 0x80);
                data[position++] = (byte)(length >> 28);

                fixed(char *chars = text)
                fixed(byte *bData = data)
                Encoding.UTF8.GetBytes(chars, text.Length, bData + position, size - 5);

                size -= length + 5;
            }

            position += length;
        }
コード例 #2
0
 public void Write(string text)
 {
     currentSegment.Write(text);
 }