/// <summary>
        /// Writes a string in UTF-8 encoding.
        /// </summary>
        /// <param name="text">The string to write.</param>
        /// <returns>The number of bytes written.</returns>
        public unsafe int WriteVanillaString(string text)
        {
            if (string.IsNullOrEmpty(text))
            {
                return(0);
            }

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

            if (size < requiredSize)
            {
                Next = new ManagedBinaryMemoryWriterSegment(writer, requiredSize + 1024);
                Next.Write(text);
                return(requiredSize);
            }

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

            size     -= requiredSize;
            position += requiredSize;

            return(requiredSize);
        }
        /// <summary>
        /// Writes a byte.
        /// </summary>
        /// <param name="data">The byte to write.</param>
        public void Write(byte data)
        {
            if (size < 1)
            {
                Next = new ManagedBinaryMemoryWriterSegment(writer, 1024);
                Next.Write(data);
                return;
            }

            this.data[position++] = data;
            size--;
        }
        /// <summary>
        /// Writes a datetime.
        /// </summary>
        /// <param name="data">The datetime to write.</param>
        public unsafe void Write(DateTime data)
        {
            if (size < 8)
            {
                Next = new ManagedBinaryMemoryWriterSegment(writer, 1024);
                Next.Write(data);
                return;
            }

            fixed(byte *bData = this.data)
            * (long *)(bData + position) = data.Ticks;

            position += 8;
            size     -= 8;
        }
        /// <summary>
        /// Writes a float.
        /// </summary>
        /// <param name="data">The float to write.</param>
        public unsafe void Write(float data)
        {
            if (size < 4)
            {
                Next = new ManagedBinaryMemoryWriterSegment(writer, 1024);
                Next.Write(data);
                return;
            }

            fixed(byte *bData = this.data)
            * (float *)(bData + position) = data;

            position += 4;
            size     -= 4;
        }
        /// <summary>
        /// Writes a signed byte.
        /// </summary>
        /// <param name="data">The signed byte to write.</param>
        public unsafe void Write(sbyte data)
        {
            if (size < 1)
            {
                Next = new ManagedBinaryMemoryWriterSegment(writer, 1024);
                Next.Write(data);
                return;
            }

            fixed(byte *bData = this.data)
            * (sbyte *)(bData + position) = data;

            position++;
            size--;
        }
        /// <summary>
        /// Writes a char.
        /// </summary>
        /// <param name="data">The char to write.</param>
        public void Write(char data)
        {
            if (data < 128)
            {
                if (size < 1)
                {
                    Next = new ManagedBinaryMemoryWriterSegment(writer, 1024);
                    Next.Write(data);
                    return;
                }

                this.data[position++] = (byte)data;

                size--;

                return;
            }

            if (data < 2048)
            {
                if (size < 2)
                {
                    Next = new ManagedBinaryMemoryWriterSegment(writer, 1024);
                    Next.Write(data);
                    return;
                }

                this.data[position++] = (byte)((data >> 6) | 0b11000000);
                this.data[position++] = (byte)((data & 0b00111111) | 0b10000000);

                size -= 2;

                return;
            }

            if (size < 3)
            {
                Next = new ManagedBinaryMemoryWriterSegment(writer, 1024);
                Next.Write(data);
                return;
            }

            this.data[position++] = (byte)((data >> 12) | 0b11100000);
            this.data[position++] = (byte)(((data >> 6) & 0b00111111) | 0b10000000);
            this.data[position++] = (byte)((data & 0b00111111) | 0b10000000);

            size -= 3;
        }
        /// <summary>
        /// Writes a 3 byte integer.
        /// </summary>
        /// <param name="data">The integer to write.</param>
        public unsafe void WriteUInt24(int data)
        {
            if (size < 3)
            {
                Next = new ManagedBinaryMemoryWriterSegment(writer, 1024);
                Next.Write(data);
                return;
            }

            this.data[position++] = (byte)(data / 65536);

            fixed(byte *bData = this.data)
            * (ushort *)(bData + position) = (ushort)data;

            position += 2;
            size     -= 3;
        }
        /// <summary>
        /// Writes a decimal.
        /// </summary>
        /// <param name="data">The decimal to write.</param>
        public unsafe void Write(decimal data)
        {
            if (size < 16)
            {
                Next = new ManagedBinaryMemoryWriterSegment(writer, 1024);
                Next.Write(data);
                return;
            }

            int[] values = decimal.GetBits(data);

            fixed(byte *bData = this.data)
            {
                *(int *)(bData + position)      = values[0];
                *(int *)(bData + position + 4)  = values[1];
                *(int *)(bData + position + 8)  = values[2];
                *(int *)(bData + position + 12) = values[3];
            }

            size     -= 16;
            position += 16;
        }
        /// <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)
                {
                    Next = new ManagedBinaryMemoryWriterSegment(writer, 1024);
                    Next.Write(text);
                    return;
                }

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

                return;
            }

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

            if (length < 128)
            {
                if (size < length + 1)
                {
                    Next = new ManagedBinaryMemoryWriterSegment(writer, 1024);
                    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)
                {
                    Next = new ManagedBinaryMemoryWriterSegment(writer, 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)
                {
                    Next = new ManagedBinaryMemoryWriterSegment(writer, 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)
                {
                    Next = new ManagedBinaryMemoryWriterSegment(writer, 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)
                {
                    Next = new ManagedBinaryMemoryWriterSegment(writer, 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;
        }
 public void Write(string text)
 {
     currentSegment.Write(text);
 }