Exemple #1
0
        /// <summary>
        /// Write String with \0 at end
        /// </summary>
        public void WriteCString(string value)
        {
            if (value.IndexOf('\0') > -1)
            {
                throw LiteException.InvalidNullCharInString();
            }

            var bytesCount = Encoding.UTF8.GetByteCount(value);
            var available  = _current.Count - _currentPosition; // avaiable in current segment

            // can write direct in current segment (use < because need +1 \0)
            if (bytesCount < available)
            {
                Encoding.UTF8.GetBytes(value, 0, value.Length, _current.Array, _current.Offset + _currentPosition);

                _current[_currentPosition + bytesCount] = 0x00;

                this.MoveForward(bytesCount + 1); // +1 to '\0'
            }
            else
            {
                var buffer = BufferPool.Rent(bytesCount);

                Encoding.UTF8.GetBytes(value, 0, value.Length, buffer, 0);

                this.Write(buffer, 0, bytesCount);

                _current[_currentPosition] = 0x00;

                this.MoveForward(1);

                BufferPool.Return(buffer);
            }
        }