public MultipartMessage ParseMessage(string contentType, Stream inputStream)
        {
            if (log.IsDebugEnabled)
                log.DebugFormat("Message content-type: '{0}'", contentType);

            HeaderField contentTypeField = ParseHeaderField(
                HttpConstants.ContentType,
                contentType);

            MultipartMessage message = new MultipartMessage(contentTypeField);

            if (!message.IsMultipart)
                throw ParserException("This is not a MIME multipart message. The content type '{0}' is not supported", contentType);

            if (log.IsDebugEnabled)
                log.DebugFormat("Multipart message boundary: '{0}'", message.Boundary);

            BinaryBufferWithPatternWatching buffer = new BinaryBufferWithPatternWatching(
                ConstructDelimiterBuffer(message));

            try
            {
                SkipToFirstDelimiter(inputStream, buffer);

                while (true)
                {
                    int markerByte1 = inputStream.ReadByte();
                    int markerByte2 = inputStream.ReadByte();

                    if (markerByte1 == -1 || markerByte2 == -1)
                        throw ParserException("Unexpected end of stream (1).");

                    if ((byte)markerByte1 == '-' && (byte)markerByte2 == '-')
                        break;

                    if ((byte)markerByte1 == '\r' && (byte)markerByte2 == '\n')
                    {
                        MultipartMessagePart part = ReadPart(message, inputStream, buffer);
                        message.AddPart(part);
                    }
                    else
                        throw ParserException("Invalid multipart message: unexpected message part markers.");
                }

                return message;
            }
            catch (EndOfStreamException ex)
            {
                throw new MultipartMessageParserException("Unexpected end of stream.", ex);
            }
        }
        public MultipartMessage ReadHeader(string contentType)
        {
            HeaderField contentTypeField = ParseHeaderField(
                HttpConstants.ContentType,
                contentType);

            message = new MultipartMessage(contentTypeField);

            if (!message.IsMultipart)
                throw ParserException("This is not a MIME multipart message. The content type '{0}' is not supported", contentType);

            buffer = new BinaryBufferWithPatternWatching(ConstructDelimiterBuffer(message));
            SkipToFirstDelimiter();

            return message;
        }
        public MultipartMessage ReadHeader(string contentType)
        {
            HeaderField contentTypeField = ParseHeaderField(
                HttpConstants.ContentType,
                contentType);

            message = new MultipartMessage(contentTypeField);

            if (!message.IsMultipart)
            {
                throw ParserException("This is not a MIME multipart message. The content type '{0}' is not supported", contentType);
            }

            buffer = new BinaryBufferWithPatternWatching(ConstructDelimiterBuffer(message));
            SkipToFirstDelimiter();

            return(message);
        }
        private static void SkipToFirstDelimiter(Stream inputStream, BinaryBufferWithPatternWatching buffer)
        {
            buffer.Start();

            while (true)
            {
                int value = inputStream.ReadByte();
                if (value == -1)
                {
                    throw ParserException("Unexpected end of stream (2).");
                }

                byte byteValue = (byte)value;
                if (buffer.WriteByte(byteValue))
                {
                    return;
                }
            }
        }
        private static MultipartMessagePart ReadPart(
            MultipartMessage message,
            Stream inputStream,
            BinaryBufferWithPatternWatching buffer)
        {
            buffer.Start();

            while (true)
            {
                int value = inputStream.ReadByte();
                if (value == -1)
                {
                    throw ParserException("Unexpected end of stream (3).");
                }

                byte byteValue      = (byte)value;
                bool patternMatched = buffer.WriteByte(byteValue);

                if (!patternMatched)
                {
                    continue;
                }

                MultipartMessagePart part = ParsePart(
                    message,
                    buffer.ToArray(),
                    buffer.Pattern.Length);

                if (log.IsDebugEnabled)
                {
                    log.DebugFormat("Found part '{0}'", message.BaseEncoding.GetString(part.Data));
                }

                return(part);
            }
        }
        private static void SkipToFirstDelimiter(Stream inputStream, BinaryBufferWithPatternWatching buffer)
        {
            buffer.Start();

            while (true)
            {
                int value = inputStream.ReadByte();
                if (value == -1)
                    throw ParserException("Unexpected end of stream (2).");

                byte byteValue = (byte)value;
                if (buffer.WriteByte(byteValue))
                    return;
            }
        }
        private static MultipartMessagePart ReadPart(
            MultipartMessage message,
            Stream inputStream,
            BinaryBufferWithPatternWatching buffer)
        {
            buffer.Start();

            while (true)
            {
                int value = inputStream.ReadByte();
                if (value == -1)
                    throw ParserException("Unexpected end of stream (3).");

                byte byteValue = (byte)value;
                bool patternMatched = buffer.WriteByte(byteValue);

                if (!patternMatched)
                    continue;

                MultipartMessagePart part = ParsePart(
                    message,
                    buffer.ToArray(),
                    buffer.Pattern.Length);

                if (log.IsDebugEnabled)
                    log.DebugFormat("Found part '{0}'", message.BaseEncoding.GetString(part.Data));

                return part;
            }
        }
        public MultipartMessage ParseMessage(string contentType, Stream inputStream)
        {
            if (log.IsDebugEnabled)
            {
                log.DebugFormat("Message content-type: '{0}'", contentType);
            }

            HeaderField contentTypeField = ParseHeaderField(
                HttpConstants.ContentType,
                contentType);

            MultipartMessage message = new MultipartMessage(contentTypeField);

            if (!message.IsMultipart)
            {
                throw ParserException("This is not a MIME multipart message. The content type '{0}' is not supported", contentType);
            }

            if (log.IsDebugEnabled)
            {
                log.DebugFormat("Multipart message boundary: '{0}'", message.Boundary);
            }

            BinaryBufferWithPatternWatching buffer = new BinaryBufferWithPatternWatching(
                ConstructDelimiterBuffer(message));

            try
            {
                SkipToFirstDelimiter(inputStream, buffer);

                while (true)
                {
                    int markerByte1 = inputStream.ReadByte();
                    int markerByte2 = inputStream.ReadByte();

                    if (markerByte1 == -1 || markerByte2 == -1)
                    {
                        throw ParserException("Unexpected end of stream (1).");
                    }

                    if ((byte)markerByte1 == '-' && (byte)markerByte2 == '-')
                    {
                        break;
                    }

                    if ((byte)markerByte1 == '\r' && (byte)markerByte2 == '\n')
                    {
                        MultipartMessagePart part = ReadPart(message, inputStream, buffer);
                        message.AddPart(part);
                    }
                    else
                    {
                        throw ParserException("Invalid multipart message: unexpected message part markers.");
                    }
                }

                return(message);
            }
            catch (EndOfStreamException ex)
            {
                throw new MultipartMessageParserException("Unexpected end of stream.", ex);
            }
        }