Reading a stream into a MemoryStream in multiple threads

I'm stuck in place. I am reading a FLV file from a URL. I read this in a Stream and then write this stream to a MemoryStream in a loop. When the code exits the loop, I write the entire MemoryStream to ByteArray and then writing this ByteArray to a local file on my hard drive.

Since this flv is too large, it takes a long time to process in a loop. I'm thinking of reading the original large stream in a MemoryStream in multiple streams. This means splitting a stream into 10 parts and writing these parts to a MemoryStream in multiple streams. How to do it?

I am attaching my piece of code.

//Get a data stream from the url
                WebRequest req = WebRequest.Create(url);
                WebResponse response = req.GetResponse();
                using (Stream stream = response.GetResponseStream())
                {
                    //Download in chuncks
                    byte[] buffer = new byte[1024];

                    //Get Total Size
                    int dataLength = (int)response.ContentLength;



                    //Download to memory
                    //Note: adjust the streams here to download directly to the hard drive
                    using (MemoryStream memStream = new MemoryStream())
                    {
                        while (true)
                        {
                            //Try to read the data
                            int bytesRead = stream.Read(buffer, 0, buffer.Length);

                            if (bytesRead == 0)
                            {
                                Application.DoEvents();
                                break;
                            }
                            else
                            {
                                //Write the downloaded data
                                memStream.Write(buffer, 0, bytesRead);
                            }
                        }

                        //Convert the downloaded stream to a byte array
                        byte[] downloadedData = memStream.ToArray();
                    }  


                }

Any help is appreciated Thanks

+5
2

, . , , , .

, , , WebClient, WebRequest. WebClient.DownloadDataAsync WebClient.DownloadFileAsync .

, .

+2

; IO. 1 , IO, , IO. , ( , ) -, . : .

- ; , , ; , .

+1

All Articles