NewCharBuffer() public static method

public static NewCharBuffer ( int size ) : char[]
size int
return char[]
        private bool ReadStringCore(int length, ref long offset, out string result)
        {
            this._lastOffset = this._offset;

            if (length == 0)
            {
                result = String.Empty;
                return(true);
            }

            // TODO: Span<byte>
            var byteBuffer   = BufferManager.NewByteBuffer(length * 4);
            var charBuffer   = BufferManager.NewCharBuffer(length);
            var resultBuffer = new StringBuilder(length);
            var decoder      = MessagePackConvert.Utf8NonBomStrict.GetDecoder();
            var remaining    = length;

#if DEBUG
            bool isCompleted;
#endif // DEBUG
            // Retrying for splitted Stream such as NetworkStream
            do
            {
                var reading    = Math.Min(byteBuffer.Length, remaining);
                var readLength = this._source.Read(byteBuffer, 0, reading);
                if (readLength == 0)
                {
                    if (this._useStreamPosition)
                    {
                        // Rollback
                        this._source.Position -= resultBuffer.Length;
                    }
                    else
                    {
                        // Throw because rollback is not available
                        this.ThrowEofException(reading);
                    }

                    result = default(string);
                    return(false);
                }

#if DEBUG
                isCompleted =
#endif // DEBUG
                decoder.DecodeString(byteBuffer, 0, readLength, charBuffer, resultBuffer);

                remaining -= readLength;
            } while (remaining > 0);

#if DEBUG
            Contract.Assert(isCompleted, "isCompleted == true");
#endif // DEBUG
            result  = resultBuffer.ToString();
            offset += length;
            return(true);
        }
        private async Task <AsyncReadResult <Int64OffsetValue <string> > > ReadStringCoreAsync(int length, long offset, CancellationToken cancellationToken)
        {
            this._lastOffset = this._offset;

            if (length == 0)
            {
                return(AsyncReadResult.Success(String.Empty, offset));
            }

            // TODO: Span<byte>
            var byteBuffer   = BufferManager.NewByteBuffer(length * 4);
            var charBuffer   = BufferManager.NewCharBuffer(length);
            var resultBuffer = new StringBuilder(length);
            var decoder      = MessagePackConvert.Utf8NonBomStrict.GetDecoder();
            var remaining    = length;

#if DEBUG
            bool isCompleted;
#endif // DEBUG
            // Retrying for splitted Stream such as NetworkStream
            do
            {
                var reading    = Math.Min(byteBuffer.Length, remaining);
                var readLength = await this._source.ReadAsync(byteBuffer, 0, reading, cancellationToken).ConfigureAwait(false);

                if (readLength == 0)
                {
                    if (this._useStreamPosition)
                    {
                        // Rollback
                        this._source.Position -= resultBuffer.Length;
                    }
                    else
                    {
                        // Throw because rollback is not available
                        this.ThrowEofException(reading);
                    }

                    return(AsyncReadResult.Fail <Int64OffsetValue <string> >());
                }

#if DEBUG
                isCompleted =
#endif // DEBUG
                decoder.DecodeString(byteBuffer, 0, readLength, charBuffer, resultBuffer);

                remaining -= readLength;
            } while (remaining > 0);

#if DEBUG
            Contract.Assert(isCompleted, "isCompleted == true");
#endif // DEBUG
            offset += length;
            return(AsyncReadResult.Success(resultBuffer.ToString(), offset));
        }
        private void WriteStringBody(string value)
        {
            var chars  = BufferManager.NewCharBuffer(value.Length);
            int offset = 0;

            while (offset < value.Length)
            {
                int copying = Math.Min(value.Length - offset, chars.Length);
                value.CopyTo(offset, chars, 0, copying);
                this.WriteStringBody(chars, copying);
                offset += copying;
            }
        }
        private async Task WriteStringBodyAsync(string value, CancellationToken cancellationToken)
        {
            var chars  = BufferManager.NewCharBuffer(value.Length);
            int offset = 0;

            while (offset < value.Length)
            {
                int copying = Math.Min(value.Length - offset, chars.Length);
                value.CopyTo(offset, chars, 0, copying);
                await this.WriteStringBodyAsync(chars, copying, cancellationToken).ConfigureAwait(false);

                offset += copying;
            }
        }
        private async Task <string> ReadStringAsyncCore(long length, CancellationToken cancellationToken)
        {
            if (length == 0)
            {
                this.InternalCollectionType = CollectionType.None;
                return(String.Empty);
            }

            this.CheckLength(length, ReadValueResult.String);

            var length32 = unchecked (( int )length);
            var bytes    = BufferManager.NewByteBuffer(length32);

            if (length32 <= bytes.Length)
            {
                await this.ReadStrictAsync(bytes, length32, cancellationToken).ConfigureAwait(false);

                var result = Encoding.UTF8.GetString(bytes, 0, length32);
                this.InternalCollectionType = CollectionType.None;
                return(result);
            }

            var decoder      = Encoding.UTF8.GetDecoder();
            var chars        = BufferManager.NewCharBuffer(bytes.Length);
            var stringBuffer = new StringBuilder(length32);
            var remaining    = length32;

            do
            {
                var reading = Math.Min(remaining, bytes.Length);
                this._lastOffset = this._offset;
                var bytesRead = await this._source.ReadAsync(bytes, 0, reading, cancellationToken).ConfigureAwait(false);

                this._offset += bytesRead;
                if (bytesRead == 0)
                {
                    this.ThrowEofException(reading);
                }

                remaining -= bytesRead;

                var isCompleted = false;
                var bytesOffset = 0;

                while (!isCompleted)
                {
                    int bytesUsed;
                    int charsUsed;
                    decoder.Convert(
                        bytes,
                        bytesOffset,
                        bytesRead - bytesOffset,
                        chars,
                        0,
                        chars.Length,
                        (bytesRead == 0),
                        // flush when last read.
                        out bytesUsed,
                        out charsUsed,
                        out isCompleted
                        );

                    stringBuffer.Append(chars, 0, charsUsed);
                    bytesOffset += bytesUsed;
                }
            } while (remaining > 0);

            this.InternalCollectionType = CollectionType.None;
            return(stringBuffer.ToString());
        }