<commit_before>using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace HttpMultipartParser
{
    /// <summary>
    ///     Provides methods to parse a
    ///     <see href="http://www.ietf.org/rfc/rfc2388.txt">
    ///         <c>multipart/form-data</c>
    ///     </see>
    ///     stream into it's parameters and file data.
    /// </summary>
    /// <remarks>
    ///     <para>
    ///         A parameter is defined as any non-file data passed in the multipart stream. For example
    ///         any form fields would be considered a parameter.
    ///     </para>
    ///     <para>
    ///         The parser determines if a section is a file or not based on the presence or absence
    ///         of the filename argument for the Content-Type header. If filename is set then the section
    ///         is assumed to be a file, otherwise it is assumed to be parameter data.
    ///     </para>
    /// </remarks>
    /// <example>
    ///     <code lang="C#">
    ///       Stream multipartStream = GetTheMultipartStream();
    ///       string boundary = GetTheBoundary();
    ///       var parser = new StreamingMultipartFormDataParser(multipartStream, boundary, Encoding.UTF8);
    ///
    ///       // Set up our delegates for how we want to handle recieved data.
    ///       // In our case parameters will be written to a dictionary and files
    ///       // will be written to a filestream
    ///       parser.ParameterHandler += parameter => AddToDictionary(parameter);
    ///       parser.FileHandler += (name, fileName, type, disposition, buffer, bytes) => WriteDataToFile(fileName, buffer, bytes);
    ///       parser.Run();
    ///   </code>
    /// </example>
    public class StreamingMultipartFormDataParser
    {
        #region Constants

        /// <summary>
        ///     The default buffer size.
        /// </summary>
        /// <remarks>
        ///     4096 is the optimal buffer size as it matches the internal buffer of a StreamReader
        ///     See: http://stackoverflow.com/a/129318/203133
        ///     See: http://msdn.microsoft.com/en-us/library/9kstw824.aspx (under remarks).
        /// </remarks>
        private const int DefaultBufferSize = 4096;

        #endregion

        #region Fields

        /// <summary>
        ///     The stream we are parsing.
        /// </summary>
        private readonly Stream stream;

        /// <summary>
        ///     The boundary of the multipart message  as a string.
        /// </summary>
        private string boundary;

        /// <summary>
        ///     The boundary of the multipart message as a byte string
        ///     encoded with CurrentEncoding.
        /// </summary>
        private byte[] boundaryBinary;

        /// <summary>
        ///     The end boundary of the multipart message as a string.
        /// </summary>
        private string endBoundary;

        /// <summary>
        ///     The end boundary of the multipart message as a byte string
        ///     encoded with CurrentEncoding.
        /// </summary>
        private byte[] endBoundaryBinary;

        /// <summary>
        ///     Determines if we have consumed the end boundary binary and determines
        ///     if we are done parsing.
        /// </summary>
        private bool readEndBoundary;

        /// <summary>
        ///     List of mimetypes that should be detected as file.
        /// </summary>
        private string[] binaryMimeTypes = { "application/octet-stream" };

        #endregion

        #region Constructors and Destructors

        /// <summary>
        ///     Initializes a new instance of the <see cref="StreamingMultipartFormDataParser" /> class
        ///     with an input stream. Boundary will be automatically detected based on the
        ///     first line of input.
        /// </summary>
        /// <param name="stream">
        ///     The stream containing the multipart data.
        /// </param>
        public StreamingMultipartFormDataParser(Stream stream)<commit_msg>Fix IDE0044 "Make field readonly" and move the readonly field to avoid SA1214 "Readonly fields should appear before non-readonly fields"<commit_after>using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace HttpMultipartParser
{
    /// <summary>
    ///     Provides methods to parse a
    ///     <see href="http://www.ietf.org/rfc/rfc2388.txt">
    ///         <c>multipart/form-data</c>
    ///     </see>
    ///     stream into it's parameters and file data.
    /// </summary>
    /// <remarks>
    ///     <para>
    ///         A parameter is defined as any non-file data passed in the multipart stream. For example
    ///         any form fields would be considered a parameter.
    ///     </para>
    ///     <para>
    ///         The parser determines if a section is a file or not based on the presence or absence
    ///         of the filename argument for the Content-Type header. If filename is set then the section
    ///         is assumed to be a file, otherwise it is assumed to be parameter data.
    ///     </para>
    /// </remarks>
    /// <example>
    ///     <code lang="C#">
    ///       Stream multipartStream = GetTheMultipartStream();
    ///       string boundary = GetTheBoundary();
    ///       var parser = new StreamingMultipartFormDataParser(multipartStream, boundary, Encoding.UTF8);
    ///
    ///       // Set up our delegates for how we want to handle recieved data.
    ///       // In our case parameters will be written to a dictionary and files
    ///       // will be written to a filestream
    ///       parser.ParameterHandler += parameter => AddToDictionary(parameter);
    ///       parser.FileHandler += (name, fileName, type, disposition, buffer, bytes) => WriteDataToFile(fileName, buffer, bytes);
    ///       parser.Run();
    ///   </code>
    /// </example>
    public class StreamingMultipartFormDataParser
    {
        #region Constants

        /// <summary>
        ///     The default buffer size.
        /// </summary>
        /// <remarks>
        ///     4096 is the optimal buffer size as it matches the internal buffer of a StreamReader
        ///     See: http://stackoverflow.com/a/129318/203133
        ///     See: http://msdn.microsoft.com/en-us/library/9kstw824.aspx (under remarks).
        /// </remarks>
        private const int DefaultBufferSize = 4096;

        #endregion

        #region Fields

        /// <summary>
        ///     List of mimetypes that should be detected as file.
        /// </summary>
        private readonly string[] binaryMimeTypes = { "application/octet-stream" };

        /// <summary>
        ///     The stream we are parsing.
        /// </summary>
        private readonly Stream stream;

        /// <summary>
        ///     The boundary of the multipart message  as a string.
        /// </summary>
        private string boundary;

        /// <summary>
        ///     The boundary of the multipart message as a byte string
        ///     encoded with CurrentEncoding.
        /// </summary>
        private byte[] boundaryBinary;

        /// <summary>
        ///     The end boundary of the multipart message as a string.
        /// </summary>
        private string endBoundary;

        /// <summary>
        ///     The end boundary of the multipart message as a byte string
        ///     encoded with CurrentEncoding.
        /// </summary>
        private byte[] endBoundaryBinary;

        /// <summary>
        ///     Determines if we have consumed the end boundary binary and determines
        ///     if we are done parsing.
        /// </summary>
        private bool readEndBoundary;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        ///     Initializes a new instance of the <see cref="StreamingMultipartFormDataParser" /> class
        ///     with an input stream. Boundary will be automatically detected based on the
        ///     first line of input.
        /// </summary>
        /// <param name="stream">
        ///     The stream containing the multipart data.
        /// </param>
        public StreamingMultipartFormDataParser(Stream stream)
{'fraction_non_alphanumeric': 0.11435666982024598, 'fraction_numerical': 0.007805108798486282, 'mean_word_length': 1.700191570881226, 'pattern_counts': {'":': 0, '<': 81, '<?xml version=': 0, '>': 85, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5999113', 'n_tokens_mistral': 2194, 'n_tokens_neox': 2133, 'n_words': 761}