public void Add(SerializedMetric serializedMetric)
        {
            var length = serializedMetric.CopyToChars(_charsBuffers);

            if (length < 0)
            {
                throw new InvalidOperationException($"The metric size exceeds the internal buffer capacity {_charsBuffers.Length}: {serializedMetric.ToString()}");
            }

            var byteCount = _encoding.GetByteCount(_charsBuffers, 0, length);

            if (byteCount > Capacity)
            {
                throw new InvalidOperationException($"The metric size exceeds the buffer capacity {Capacity}: {serializedMetric.ToString()}");
            }

            if (Length != 0)
            {
                byteCount += _separator.Length;
            }

            if (Length + byteCount > Capacity)
            {
                this.HandleBufferAndReset();
            }

            if (Length != 0)
            {
                Array.Copy(_separator, 0, _buffer, Length, _separator.Length);
                Length += _separator.Length;
            }

            // GetBytes requires the buffer to be big enough otherwise it throws, that is why we use GetByteCount.
            Length += _encoding.GetBytes(_charsBuffers, 0, length, _buffer, Length);
        }
Exemple #2
0
        public bool Add(SerializedMetric serializedMetric)
        {
            var length = serializedMetric.CopyToChars(_charsBuffers);

            if (length < 0)
            {
                return(false);
            }

            var byteCount = _encoding.GetByteCount(_charsBuffers, 0, length);

            if (byteCount > Capacity)
            {
                return(false);
            }

            if (Length != 0)
            {
                byteCount += _separator.Length;
            }

            if (Length + byteCount > Capacity)
            {
                this.HandleBufferAndReset();
            }

            if (Length != 0)
            {
                Array.Copy(_separator, 0, _buffer, Length, _separator.Length);
                Length += _separator.Length;
            }

            // GetBytes requires the buffer to be big enough otherwise it throws, that is why we use GetByteCount.
            Length += _encoding.GetBytes(_charsBuffers, 0, length, _buffer, Length);
            return(true);
        }