コード例 #1
0
        private void parseStream()
        {
            m_Stream.Position = 0;

            long firstEOLPos = StreamHelpers.Find(m_Stream, EOL_CRLF_BYTES);

            if (firstEOLPos == StreamHelpers.NOT_FOUND_POS)
            {
                firstEOLPos = StreamHelpers.Find(m_Stream, EOL_LF_BYTES);
                if (firstEOLPos == StreamHelpers.NOT_FOUND_POS)
                {
                    throw new NFXException(StringConsts.MULTIPART_NO_LF_NOR_CRLF_ISNT_FOUND_ERROR + this.GetType().Name + ".ParseStream");
                }

                m_EOL = EOL_LF_BYTES;
            }
            else
            {
                m_EOL = EOL_CRLF_BYTES;
            }

            byte[] boundaryBytes = new byte[firstEOLPos];
            boundaryBytes = new byte[firstEOLPos];
            m_Stream.Read(boundaryBytes, 0, (int)firstEOLPos);

            if (m_Boundary != null)
            {
                string streamBoundaryStr = m_Encoding.GetString(boundaryBytes).Substring(2);
                if (streamBoundaryStr != m_Boundary)
                {
                    throw new NFXException(StringConsts.MULTIPART_BOUNDARY_MISMATCH_ERROR.Args(m_Boundary, streamBoundaryStr) +
                                           this.GetType().Name + ".ParseStream");
                }
            }
            else
            {
                var fullBoundary = m_Encoding.GetString(boundaryBytes);
                if (fullBoundary.Length < 3)
                {
                    throw new NFXException(StringConsts.MULTIPART_BOUNDARY_COULDNT_BE_SHORTER_3_ERROR + this.GetType().Name + ".ParseStream");
                }
                m_Boundary = fullBoundary.Substring(2); // remove two leading hyphens
            }

            m_Stream.Position = 0;

            int boundaryLength = boundaryBytes.Length;

            byte[] endBoundaryBytes = new byte[boundaryLength + 2];
            m_Stream.Read(endBoundaryBytes, 0, boundaryLength);
            endBoundaryBytes[boundaryLength] = HYPHEN_BYTE; endBoundaryBytes[boundaryLength + 1] = HYPHEN_BYTE;

            long terminatorPos = StreamHelpers.Find(m_Stream, endBoundaryBytes);

            if (terminatorPos == StreamHelpers.NOT_FOUND_POS)
            {
                throw new NFXException(StringConsts.MULTIPART_TERMINATOR_ISNT_FOUND_ERROR + this.GetType().Name + ".ParseStream");
            }

            var splitSegmentCoordinates = StreamHelpers.Split(m_Stream, boundaryBytes, whatLast: terminatorPos).ToArray();

            foreach (var coordinate in splitSegmentCoordinates.Where(c => (c.Item2 - c.Item1) > m_EOL.Length))
            {
                if (!StreamHelpers.EndsWith(m_Stream, m_EOL, coordinate.Item1, coordinate.Item2))
                {
                    throw new NFXException(StringConsts.MULTIPART_PART_SEGMENT_ISNT_TERMINATED_CORRECTLY_ERROR.Args(m_EOL) + this.GetType().Name + ".ParseStream");
                }

                var part = new MultiPart(m_Stream, coordinate.Item1, coordinate.Item2 - m_EOL.Length, m_EOL, m_Encoding);

                m_Parts.Add(part);
            }
        }