I had this issue too. And here are some facts about it:
System.ArgumentException: The output char buffer is too small to contain the decoded characters, encoding 'Unicode (UTF-8)'
is known to be related to UTF-8 encoding problem (invalid character code) rather than to buffering problem - Detials hereNetworkStream
(Read
and other methods) is known to return only the amount of bytes which is already present in system network buffers instead of blocking until all requested data will be recieved - Details here. So, one needs to useRead
in a loop to get all requested dataBinaryReader
is known to throw an exception when getting less data fromNetworkStream
than it expected, instead of using a loop to retrieve the rest (and YES, I am sure, this means a bug!) - Details here
So, my solution was to partially reimplement BinaryReader
(I have called my class BinReader
) adding some useful features and making proper Read method with a loop:
public int Read( byte[] buf, int off, int count ) {
int read = 0;
while( read < count ) {
int toread = count - read;
int portion = BaseStream.Read( buf, off, toread );
read += portion;
off += portion;
}
return read;
}
That has solved it for me.