Unbuffered StreamReader

前端 未结 2 410
心在旅途
心在旅途 2020-12-31 02:10

Is there a way to keep StreamReader from doing any buffering?

I\'m trying to handle output from a Process that may be either binary or text. The output will look lik

2条回答
  •  天涯浪人
    2020-12-31 03:14

    Well, you can use Stream.Seek to set the position of the stream. It sounds to me like the problem you're having here is that StreamReader is reading characters rather than bytes (which, depending on the encoding, may be different than 1 byte per character). From the MSDN Library:

    StreamReader is designed for character input in a particular encoding, whereas the Stream class is designed for byte input and output.

    When you call reader.ReadToEnd(), it reads the data in as a character string based on whatever encoding it's using. You might have better luck using the Stream.Read method. Read in your string data with StreamReader and then pull out the binary data into a byte[] when you've read in the header that notifies you of incoming binary data.

提交回复
热议问题