NewByteBuffer() public static method

public static NewByteBuffer ( int size ) : byte[]
size int
return byte[]
        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 async Task <bool> DrainAsync(uint size, CancellationToken cancellationToken)
        {
            this._lastOffset = this._offset;

            if (this._useStreamPosition)
            {
                var drained = this._source.Seek(size, SeekOrigin.Current);
                if (drained < size)
                {
                    // Rollback
                    this._source.Position -= drained;
                    return(false);
                }

                this._offset += size;
                return(true);
            }
            else
            {
                // Actually, buffer size should be smaller than 2GB.
                var buffer       = BufferManager.NewByteBuffer(unchecked (( int )Math.Max(Int32.MaxValue, size)));
                var totalSkipped = 0L;
                while (totalSkipped < size)
                {
                    var skipping = unchecked (( int )Math.Min(buffer.Length, size - totalSkipped));
                    var skipped  = await this._source.ReadAsync(buffer, 0, skipping, cancellationToken).ConfigureAwait(false);

                    totalSkipped += skipped;
                    if (skipped < skipping)
                    {
                        // Record
                        this._offset += totalSkipped;
                        return(false);
                    }
                }

                this._offset += totalSkipped;
                return(true);
            }
        }
        private async Task WriteStringBodyAsync(string value, CancellationToken cancellationToken)
        {
            var remainingCharsLength = value.Length;
#endif // !FEATURE_POINTER_CONVERSION

            var buffer      = BufferManager.NewByteBuffer(value.Length * 4);
            var encoder     = Encoding.UTF8.GetEncoder();
            var valueOffset = 0;

            bool isCompleted = false;
            do
            {
                int charsUsed, bytesUsed;
                isCompleted = EncodeString(encoder, value, valueOffset, remainingCharsLength, buffer, out charsUsed, out bytesUsed);

                valueOffset          += charsUsed;
                remainingCharsLength -= charsUsed;
                await this._destination.WriteAsync(buffer, 0, bytesUsed, cancellationToken).ConfigureAwait(false);
            } while (remainingCharsLength > 0);

#if DEBUG
            Contract.Assert(isCompleted, "Encoding is not completed!");
#endif // DEBUG
        }
        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());
        }