internal static async Task CopyBytesToStreamChunked(this CustomBinaryReader clientStreamReader, Stream stream)
        {
            while (true)
            {
                var chuchkHead = await clientStreamReader.ReadLineAsync().ConfigureAwait(false);

                var chunkSize = int.Parse(chuchkHead, NumberStyles.HexNumber);

                if (chunkSize != 0)
                {
                    var buffer = await clientStreamReader.ReadBytesAsync(chunkSize);

                    await stream.WriteAsync(buffer, 0, buffer.Length);

                    //chunk trail
                    await clientStreamReader.ReadLineAsync().ConfigureAwait(false);
                }
                else
                {
                    await clientStreamReader.ReadLineAsync().ConfigureAwait(false);

                    break;
                }
            }
        }
        internal static async Task CopyBytesToStream(this CustomBinaryReader streamReader, Stream stream, long totalBytesToRead)
        {
            var totalbytesRead = 0;

            long bytesToRead;

            if (totalBytesToRead < Constants.BUFFER_SIZE)
            {
                bytesToRead = totalBytesToRead;
            }
            else
            {
                bytesToRead = Constants.BUFFER_SIZE;
            }


            while (totalbytesRead < totalBytesToRead)
            {
                var buffer = await streamReader.ReadBytesAsync(bytesToRead);

                if (buffer.Length == 0)
                {
                    break;
                }

                totalbytesRead += buffer.Length;

                var remainingBytes = totalBytesToRead - totalbytesRead;
                if (remainingBytes < bytesToRead)
                {
                    bytesToRead = remainingBytes;
                }
                await stream.WriteAsync(buffer, 0, buffer.Length);
            }
        }
        //Send chunked response
        private static async Task WriteResponseBodyChunked(CustomBinaryReader inStreamReader, Stream outStream)
        {
            while (true)
            {
                var chunkHead = await inStreamReader.ReadLineAsync().ConfigureAwait(false);

                var chunkSize = int.Parse(chunkHead, NumberStyles.HexNumber);

                if (chunkSize != 0)
                {
                    var buffer = await inStreamReader.ReadBytesAsync(chunkSize).ConfigureAwait(false);

                    var chunkHeadBytes = Encoding.ASCII.GetBytes(chunkSize.ToString("x2"));

                    await outStream.WriteAsync(chunkHeadBytes, 0, chunkHeadBytes.Length).ConfigureAwait(false);

                    await outStream.WriteAsync(Constants.NewLineBytes, 0, Constants.NewLineBytes.Length).ConfigureAwait(false);

                    await outStream.WriteAsync(buffer, 0, chunkSize).ConfigureAwait(false);

                    await outStream.WriteAsync(Constants.NewLineBytes, 0, Constants.NewLineBytes.Length).ConfigureAwait(false);

                    await inStreamReader.ReadLineAsync().ConfigureAwait(false);
                }
                else
                {
                    await inStreamReader.ReadLineAsync().ConfigureAwait(false);

                    await outStream.WriteAsync(Constants.ChunkEnd, 0, Constants.ChunkEnd.Length).ConfigureAwait(false);

                    break;
                }
            }
        }
Exemple #4
0
        /// <summary>
        /// copies the specified bytes to the stream from the input stream
        /// </summary>
        /// <param name="streamReader"></param>
        /// <param name="stream"></param>
        /// <param name="totalBytesToRead"></param>
        /// <returns></returns>
        internal static async Task CopyBytesToStream(this CustomBinaryReader streamReader, Stream stream, long totalBytesToRead)
        {
            var  buffer         = streamReader.Buffer;
            long remainingBytes = totalBytesToRead;

            while (remainingBytes > 0)
            {
                int bytesToRead = buffer.Length;
                if (remainingBytes < bytesToRead)
                {
                    bytesToRead = (int)remainingBytes;
                }

                int bytesRead = await streamReader.ReadBytesAsync(buffer, bytesToRead);

                if (bytesRead == 0)
                {
                    break;
                }

                remainingBytes -= bytesRead;

                await stream.WriteAsync(buffer, 0, bytesRead);
            }
        }
        /// <summary>
        /// copies the specified bytes to the stream from the input stream
        /// </summary>
        /// <param name="streamReader"></param>
        /// <param name="bufferSize"></param>
        /// <param name="stream"></param>
        /// <param name="totalBytesToRead"></param>
        /// <returns></returns>
        internal static async Task CopyBytesToStream(this CustomBinaryReader streamReader, int bufferSize, Stream stream, long totalBytesToRead)
        {
            var totalbytesRead = 0;

            long bytesToRead = totalBytesToRead < bufferSize ? totalBytesToRead : bufferSize;

            while (totalbytesRead < totalBytesToRead)
            {
                var buffer = await streamReader.ReadBytesAsync(bufferSize, bytesToRead);

                if (buffer.Length == 0)
                {
                    break;
                }

                totalbytesRead += buffer.Length;

                var remainingBytes = totalBytesToRead - totalbytesRead;
                if (remainingBytes < bytesToRead)
                {
                    bytesToRead = remainingBytes;
                }

                await stream.WriteAsync(buffer, 0, buffer.Length);
            }
        }