private void Parse(Stream stream) { this.Success = false; if (!stream.CanRead) { return; } // Read the stream into a byte array byte[] data = MultipartParser.ToByteArray(stream); if (data.Length < 1) { return; } // finding the delimiter (the string in the beginning and end of the file int delimeterIndex = MultipartParser.SimpleBoyerMooreSearch(data, Encoding.UTF8.GetBytes("\r\n")); // here we got delimeter index if (delimeterIndex == -1) { return; } byte[] delimeterBytes = new byte[delimeterIndex]; Array.Copy(data, delimeterBytes, delimeterIndex); // removing the very first couple of lines, till we get the beginning of the JPG file byte[] newLineBytes = Encoding.UTF8.GetBytes("\r\n\r\n"); int startIndex = 0; startIndex = MultipartParser.SimpleBoyerMooreSearch(data, newLineBytes); if (startIndex == -1) { return; } int startIndexWith2Lines = startIndex + 4; // 4 is the bytes of "\r\n\r\n" int newLength = data.Length - startIndexWith2Lines; byte[] newByteArray = new byte[newLength]; Array.Copy(data, startIndex + 4, newByteArray, 0, newLength - 1); // check for the end of the stream, is ther same delimeter int isThereDelimeterInTheEnd = MultipartParser.SimpleBoyerMooreSearch(newByteArray, delimeterBytes); if (isThereDelimeterInTheEnd == -1) { return; // the file corrupted so } int endIndex = isThereDelimeterInTheEnd - delimeterBytes.Length; byte[] lastArray = new byte[endIndex]; Array.Copy(newByteArray, 0, lastArray, 0, endIndex); this.FileContents = lastArray; this.Success = true; }