<commit_before>﻿// Needed for Workaround

using System;
using System.IO;

#if LESSTHAN_NET45

using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;

#endif

namespace Theraot.Core
{
    public static class StreamExtensions
    {
#if LESSTHAN_NET40
        private const int _defaultBufferSize = 4096;

        [MethodImpl(MethodImplOptionsEx.AggressiveInlining)]
        public static void CopyTo(this Stream input, Stream output)
        {
            //Added in .NET 4.0
            if (input == null)
            {
                throw new ArgumentNullException(nameof(input));
            }
            if (output == null)
            {
                throw new ArgumentNullException(nameof(output));
            }
            var buffer = new byte[_defaultBufferSize];
            int read;
            do
            {
                read = input.Read(buffer, 0, _defaultBufferSize);
                output.Write(buffer, 0, read);
            }
            while (read != 0);
        }

        [MethodImpl(MethodImplOptionsEx.AggressiveInlining)]
        public static void CopyTo(this Stream input, Stream output, int bufferSize)
        {
            //Added in .NET 4.0
            if (input == null)
            {
                throw new ArgumentNullException(nameof(input));
            }
            if (output == null)
            {
                throw new ArgumentNullException(nameof(output));
            }
            var buffer = new byte[bufferSize];
            int read;
            do
            {
                read = input.Read(buffer, 0, bufferSize);
                output.Write(buffer, 0, read);
            }
            while (read != 0);
        }

#endif

#if LESSTHAN_NET45

        [MethodImpl(MethodImplOptionsEx.AggressiveInlining)]
        public static async Task<int> ReadAsync(this Stream stream, byte[] buffer, int offset, int count, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            return await Task.Factory.FromAsync
            (
                BeginRead,
                stream.EndRead,
                Tuple.Create(stream, buffer, offset, count)
            );
        }

        [MethodImpl(MethodImplOptionsEx.AggressiveInlining)]
        public static async Task<int> ReadAsync(this Stream stream, byte[] buffer, int offset, int count)
        {
            return await Task.Factory.FromAsync
            (
                BeginRead,
                stream.EndRead,
                Tuple.Create(stream, buffer, offset, count)
            );
        }

        private static IAsyncResult BeginRead(AsyncCallback callback, object state)
        {
            var tuple = (Tuple<Stream, byte[], int, int>)state;
            return tuple.Item1.BeginRead(tuple.Item2, tuple.Item3, tuple.Item4, callback, tuple.Item4);
        }

        [MethodImpl(MethodImplOptionsEx.AggressiveInlining)]
        public static async Task WriteAsync(this Stream stream, byte[] buffer, int offset, int count, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            await Task.Factory.FromAsync
            (
                BeginWrite,
                stream.EndWrite,
                Tuple.Create(stream, buffer, offset, count)
            );
        }

        [MethodImpl(MethodImplOptionsEx.AggressiveInlining)]
        public static async Task WriteAsync(this Stream stream, byte[] buffer, int offset, int count)
        {
            await Task.Factory.FromAsync
            (
                BeginWrite,
                stream.EndWrite,
                Tuple.Create(stream, buffer, offset, count)
            );
        }

        private static IAsyncResult BeginWrite(AsyncCallback callback, object state)<commit_msg>StreamExtensions review<commit_after>﻿// Needed for Workaround

using System;
using System.IO;

#if LESSTHAN_NET45

using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;

#endif

namespace Theraot.Core
{
    public static class StreamExtensions
    {
#if LESSTHAN_NET40
        private const int _defaultBufferSize = 4096;

        [MethodImpl(MethodImplOptionsEx.AggressiveInlining)]
        public static void CopyTo(this Stream input, Stream output)
        {
            //Added in .NET 4.0
            if (input == null)
            {
                throw new ArgumentNullException(nameof(input));
            }
            if (output == null)
            {
                throw new ArgumentNullException(nameof(output));
            }
            var buffer = new byte[_defaultBufferSize];
            int read;
            do
            {
                read = input.Read(buffer, 0, _defaultBufferSize);
                output.Write(buffer, 0, read);
            }
            while (read != 0);
        }

        [MethodImpl(MethodImplOptionsEx.AggressiveInlining)]
        public static void CopyTo(this Stream input, Stream output, int bufferSize)
        {
            //Added in .NET 4.0
            if (input == null)
            {
                throw new ArgumentNullException(nameof(input));
            }
            if (output == null)
            {
                throw new ArgumentNullException(nameof(output));
            }
            var buffer = new byte[bufferSize];
            int read;
            do
            {
                read = input.Read(buffer, 0, bufferSize);
                output.Write(buffer, 0, read);
            }
            while (read != 0);
        }

#endif

#if LESSTHAN_NET45

        [MethodImpl(MethodImplOptionsEx.AggressiveInlining)]
        public static async Task<int> ReadAsync(this Stream stream, byte[] buffer, int offset, int count, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            return await Task.Factory.FromAsync
            (
                BeginRead,
                stream.EndRead,
                Tuple.Create(stream, buffer, offset, count)
            ).ConfigureAwait(false);
        }

        [MethodImpl(MethodImplOptionsEx.AggressiveInlining)]
        public static async Task<int> ReadAsync(this Stream stream, byte[] buffer, int offset, int count)
        {
            return await Task.Factory.FromAsync
            (
                BeginRead,
                stream.EndRead,
                Tuple.Create(stream, buffer, offset, count)
            ).ConfigureAwait(false);
        }

        private static IAsyncResult BeginRead(AsyncCallback callback, object state)
        {
            var tuple = (Tuple<Stream, byte[], int, int>)state;
            return tuple.Item1.BeginRead(tuple.Item2, tuple.Item3, tuple.Item4, callback, tuple.Item4);
        }

        [MethodImpl(MethodImplOptionsEx.AggressiveInlining)]
        public static async Task WriteAsync(this Stream stream, byte[] buffer, int offset, int count, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            await Task.Factory.FromAsync
            (
                BeginWrite,
                stream.EndWrite,
                Tuple.Create(stream, buffer, offset, count)
            ).ConfigureAwait(false);
        }

        [MethodImpl(MethodImplOptionsEx.AggressiveInlining)]
        public static async Task WriteAsync(this Stream stream, byte[] buffer, int offset, int count)
        {
            await Task.Factory.FromAsync
            (
                BeginWrite,
                stream.EndWrite,
                Tuple.Create(stream, buffer, offset, count)
            ).ConfigureAwait(false);
        }

        private static IAsyncResult BeginWrite(AsyncCallback callback, object state)
{'fraction_non_alphanumeric': 0.08069904908763814, 'fraction_numerical': 0.006425083526085839, 'mean_word_length': 1.738564391273751, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1553765', 'n_tokens_mistral': 2027, 'n_tokens_neox': 1923, 'n_words': 486}