Stream Reading & Multibyte encoding.
-
I have a network application that is reading an incomming message. The message should be UTF-8 formatted and could be transfered with garbage. I am currently using the StreamReader and BinReader to read from the stream, but I am unsure if I am using these objects properly. When I use the StreamReader to ReadToEnd() will it stop when it has reached an incomplete character? Will it leave the stream position after these incomplete characters, or will it go past them and just not include them in the results? Assert(this);
-
I have a network application that is reading an incomming message. The message should be UTF-8 formatted and could be transfered with garbage. I am currently using the StreamReader and BinReader to read from the stream, but I am unsure if I am using these objects properly. When I use the StreamReader to ReadToEnd() will it stop when it has reached an incomplete character? Will it leave the stream position after these incomplete characters, or will it go past them and just not include them in the results? Assert(this);
This is from MSDN: ReadToEnd works best when you need to read all the input from the current position to the end of the stream. If more control is needed over how many characters are read from the stream, use the Read(Char[],Int32,Int32) method overload, which generally results in better performance. ReadToEnd assumes that the stream knows when it has reached an end. For interactive protocols, in which the server sends data only when you ask for it and does not close the connection, ReadToEnd might block indefinitely and should be avoided. Note than when using the Read method, it is more efficient to use a buffer that is the same size as the internal buffer of the stream. If the size of the buffer was unspecified when the stream was constructed, its default size is 4 kilobytes (4096 bytes).
-
This is from MSDN: ReadToEnd works best when you need to read all the input from the current position to the end of the stream. If more control is needed over how many characters are read from the stream, use the Read(Char[],Int32,Int32) method overload, which generally results in better performance. ReadToEnd assumes that the stream knows when it has reached an end. For interactive protocols, in which the server sends data only when you ask for it and does not close the connection, ReadToEnd might block indefinitely and should be avoided. Note than when using the Read method, it is more efficient to use a buffer that is the same size as the internal buffer of the stream. If the size of the buffer was unspecified when the stream was constructed, its default size is 4 kilobytes (4096 bytes).
Hmm, I'll try changing it to Read. I'm guessing that an incomplete character could cause a block, I guess I'll just have to waste the memory in triple allocation (stream, array, string). It's kind of hard to specify a buffer that is the same size as the stream though since with multibyte I won't know the number of chars that are actually in the stream until it's decoded. Assert(this);