private async Task<int> ReadBufferAsync()
{
CharLen_Prop = 0;
CharPos_Prop = 0;
Byte[] tmpByteBuffer = ByteBuffer_Prop;
Stream tmpStream = Stream_Prop;
if (!CheckPreamble_Prop)
{
ByteLen_Prop = 0;
}
do
{
if (CheckPreamble_Prop)
{
Debug.Assert(BytePos_Prop <= Preamble_Prop.Length, "possible bug in _compressPreamble. Are two threads using this StreamReader at the same time?");
int tmpBytePos = BytePos_Prop;
int len = await tmpStream.ReadAsync(tmpByteBuffer, tmpBytePos, tmpByteBuffer.Length - tmpBytePos).ConfigureAwait(false);
Debug.Assert(len >= 0, "Stream.Read returned a negative number! This is a bug in your stream class.");
if (len == 0)
{
// EOF but we might have buffered bytes from previous
// attempt to detect preamble that needs to be decoded now
if (ByteLen_Prop > 0)
{
CharLen_Prop += Decoder_Prop.GetChars(tmpByteBuffer, 0, ByteLen_Prop, CharBuffer_Prop, CharLen_Prop);
// Need to zero out the _byteLen after we consume these bytes so that we don't keep infinitely hitting this code path
BytePos_Prop = 0; ByteLen_Prop = 0;
}
return CharLen_Prop;
}
ByteLen_Prop += len;
}
else
{
Debug.Assert(BytePos_Prop == 0, "_bytePos can be non zero only when we are trying to _checkPreamble. Are two threads using this StreamReader at the same time?");
ByteLen_Prop = await tmpStream.ReadAsync(tmpByteBuffer, 0, tmpByteBuffer.Length).ConfigureAwait(false);
Debug.Assert(ByteLen_Prop >= 0, "Stream.Read returned a negative number! Bug in stream class.");
if (ByteLen_Prop == 0) // We're at EOF
{
return CharLen_Prop;
}
}
// _isBlocked == whether we read fewer bytes than we asked for.
// Note we must check it here because CompressBuffer or
// DetectEncoding will change _byteLen.
IsBlocked_Prop = (ByteLen_Prop < tmpByteBuffer.Length);
// Check for preamble before detect encoding. This is not to override the
// user supplied Encoding for the one we implicitly detect. The user could
// customize the encoding which we will loose, such as ThrowOnError on UTF8
if (IsPreamble())
{
continue;
}
// If we're supposed to detect the encoding and haven't done so yet,
// do it. Note this may need to be called more than once.
if (DetectEncoding_Prop && ByteLen_Prop >= 2)
{
DetectEncoding();
}
CharLen_Prop += Decoder_Prop.GetChars(tmpByteBuffer, 0, ByteLen_Prop, CharBuffer_Prop, CharLen_Prop);
} while (CharLen_Prop == 0);
return CharLen_Prop;
}
#endregion