public void Test() { using (PatternizedStream pStr = new PatternizedStream(inputStr, sequence)) { int part = 0; long read = 0; byte[] buffer = new byte[4096]; bool isEnd = false; MemoryStream str = new MemoryStream(); while ((read = pStr.ReadTill(buffer, 0, 4096, out isEnd)) > 0) { str.Write(buffer, 0, (int)read); byte[] current = str.ToArray(); for (int i = 0; i < current.Length; i++) { Assert.AreEqual(parts[part][i], current[i], "Malformed data"); } if (isEnd) { Assert.IsFalse(part > 2, "Found too many parts"); Assert.AreEqual(parts[part].Length, str.Length, "Invalid part length"); Assert.IsTrue(parts[part].SequenceEqual(str.ToArray()), "Part data incorrect"); part++; str = new MemoryStream(); } } Assert.IsFalse(part < 3, "Found too little parts"); } }
public MultiPartStream(Stream stream) { if (stream == null) { throw new ArgumentNullException(); } this.stream = stream; splitter = stream.ReadTill(newLineBytes); byte[] fileEnd = new byte[splitter.Length + newLineBytes.Length]; newLineBytes.CopyTo(fileEnd, 0); splitter.CopyTo(fileEnd, newLineBytes.Length); pStr = new PatternizedStream(stream, fileEnd); }
public static MultiPartSection ParseStreaming(byte[] splitter, PatternizedStream stream, Action <MultiPartSection, byte[], long> fileHandler) { byte[] header = null; //Build Header header = stream.ReadTill(fileSplitter); if (header.Length == 0) { return(null); } string headerText = Encoding.UTF8.GetString(header); if (headerText == "--") { return(null); } //Parse Header Regex re = new Regex(@"(?<=Content\-Type:)(.*?)(?=$)"); Match contentTypeMatch = re.Match(headerText); re = new Regex(@"(?<=filename\=\"")(.*?)(?=\"")"); Match fileNameMatch = re.Match(headerText); re = new Regex(@"(?<=name\=\"")(.*?)(?=\"")"); Match nameMatch = re.Match(headerText); MultiPartSection section = new MultiPartSection(); if (contentTypeMatch.Success) { section.ContentType = contentTypeMatch.Value.Trim(); } if (fileNameMatch.Success) { section.FileName = fileNameMatch.Value.Trim(); } if (nameMatch.Success) { section.Name = nameMatch.Value.Trim(); } //Read Content byte[] fileEnd = new byte[splitter.Length + newLineBytes.Length]; newLineBytes.CopyTo(fileEnd, 0); splitter.CopyTo(fileEnd, newLineBytes.Length); //Files if (!string.IsNullOrEmpty(section.FileName) || !string.IsNullOrEmpty(section.ContentType)) { section.Streamed = true; long read = 0; byte[] buffer = new byte[4096]; bool isEnd = false; while ((read = stream.ReadTill(buffer, 0, buffer.Length, out isEnd)) > 0) { fileHandler(section, buffer, read); if (isEnd) { break; } } } //Parameters else { section.Data = stream.ReadTill(fileEnd); } return(section); }