Looking at the StreamReader source, it is not possible to mix the use of the StreamReader
and reading of bytes direct from the stream as the StreamReader
reads bytes into its internal buffer then decodes all it can into a character buffer available for reading.
Setting the buffer size to 0 doesn't help as it forces this to a minimum of 128 bytes.
For my use, I needed to scrap the StreamReader
altogether and replace the ReadLine()
with something that will read directly from the stream for parsing myself.
private string ReadLine() {
// Stringbuilder to insert the read characters in to
StringBuilder line = new StringBuilder();
// Create an array to store the maximum number of bytes for a single character
byte[] bytes = new byte[this.encoding.GetMaxByteCount(1)];
int byteCount = 0;
while (true) {
// Read the first byte
bytes[0] = (byte)this.rtspStream.ReadByte();
byteCount = 1;
// If the encoding says this isn't a full character, read until it does
while (this.encoding.GetCharCount(bytes, 0, byteCount) == 0) {
bytes[byteCount++] = (byte)this.rtspStream.ReadByte();
}
// Get the unencoded character
char thisChar = this.encoding.GetChars(bytes, 0, byteCount)[0];
// Exit if it's a new line (/r/n or /n)
if (thisChar == '\r') { continue; }
if (thisChar == '\n') { break; }
line.Append(thisChar);
}
return line.ToString();
}