|
I keep getting an EndOfStreamException meaning that the BinaryReader has attempted to read beyond the stream length when reading the string.
However, I'm not sure why this is happening and the documentation on MSDN just says that it can be thrown, but not what a properly formatted string would look like.
My stream has a Unicode string that ends with 2 bytes of NULL values. I'm pretty sure that Unicode strings are null-terminated with 2 NULL bytes.
My byte array in my stream looks like this, note that those are zeros on all even indices plus the two zeros at the end of the array: [0] = 0 [1] = C [2] = 0 [3] = E [4] = 0 [5] = S [6] = 0 [7] = S [8] = 0 [9] = N [10] = 0 [11] = A [12] = 0 [13] = 0
Anyway, when I'm in my debugger and I look at what's in my stream I can see the 2 NULL bytes at the end.
But whenever I tried to read it as a string using BinaryReader.ReadString(), its like it doesn't recognize the terminating character and throws an EndOfStreamException.
Has anyone seen this or know what the problem is? Is it that ReadString() was meant to interpret the .NET formatting of a string and not a null-terminated Unicode character array?
I have worked around this by using BinaryReader.ReadChars() and specifying how many chars to read rather than letting the BinaryReader try to determine when the string ends, but I'm curious to know why it doesn't work when I use ReadString() on a null-terminated Unicode string.
Thanks, Aaron |