带有X
个字符的未知编码字符串的最大可能字节数是多少?
我有包含流的缓冲区。我想确保在使用缓冲区之前将足够的位加载到缓冲区中以读取字符串。
请查看以下代码。
public async Task<string> ReadString(uint charCount)
{
uint size = ??? ;
// ensure remaining buffer is big enough
// to hold string with provided length
// loads data from stream if buffer is
// smaller. does not throw exception
// if reached end of stream
await EnsureBufferSize(size);
var currentPos = _dataReader.UnconsumedBufferLength;
// Read string. Handles encoding
// Throws exception if buffer is not big enough.
var str = _dataReader.ReadString(charCount);
// calculate number of bytes read
BytesRead += currentPos - _dataReader.UnconsumedBufferLength;
return str;
}
我说未知编码因为我不想让这太复杂。如果我们获取的数据超过需要,则无关紧要。但它永远不会少。