1

Is it really impossible to read from stream while it is being written? I got this code that writes bytes to dstStream, the problem is that I cannot read from it while it is being written. Is there any way to circumvent this limitation?

Edit: Just a clarification: The stream is an Audio stream that I need to process in Real-Time. Is there a way to "republish" a real-time stream?

var srcStream = await Request.Content.ReadAsStreamAsync();

var dstStream = new MemoryStream();

var buffer = new byte[4096];  
int bytesRead;
while ((bytesRead = await srcStream.ReadAsync(buffer, 0, buffer.Length, token).ConfigureAwait(false)) != 0)
{
    await dstStream.WriteAsync(buffer, 0, bytesRead, token).ConfigureAwait(false);

}
user3077725
  • 783
  • 1
  • 10
  • 17
  • 1
    Have a look at these related questions: [Asynchronous Reading/Writing to same stream?](http://stackoverflow.com/questions/12008561/asynchronous-reading-writing-to-same-stream), [Writing to then reading from a MemoryStream](http://stackoverflow.com/questions/1232443/writing-to-then-reading-from-a-memorystream) and [simultaneous read-write a file in C#](http://stackoverflow.com/questions/3817477/simultaneous-read-write-a-file-in-c-sharp) – TaW May 24 '14 at 14:24
  • Don't expect a method named "ReadAsStreamAsync" to give you a stream that's also writable. These names are quite intentional, they *emphasize* the nature of the stream. And don't expect to find an alternative, network streams are by nature highly uni-directional. Corresponding to, say, the HTTP protocol's GET and PUT requests. – Hans Passant May 24 '14 at 15:22
  • @HansPassant Thanks, but what I am trying to do is simply "republish" the Request.Content.ReadAsStreamAsync(). I want the data from ReadAsStreamAsync to be written into another stream. My problem is that I am having problem to use the other stream in real time, just like I use the original Request.Content.ReadAsStreamAsync(). – user3077725 May 24 '14 at 16:07
  • @TaW Thanks, but these links are no good, because in my case I am dealing with audio stream that I need to process in real-time. Edited the OP. – user3077725 May 24 '14 at 18:49

0 Answers0