I'm implement a text read that read from end to start, I face two problems
- the stream can only read forward
- can not guarantee the first byte is a start of
charif I get a 1kb buffer from the stream end.
I'm looking into the Decoder class and the DecoderFallbackBuffer , but I didn't find a way to detect the invalid bytes.
void Doo() {
var b = new Span<byte>(Encoding.UTF8.GetBytes("你好啊,兄弟们哼哼哼"));
var b3 = b[2..];
WriteLine(Encoding.UTF8.GetString(b3));
WriteLine(decoder.FallbackBuffer.Remaining);
while (decoder.FallbackBuffer.Fallback(b3.ToArray(), 0))
{
var r = decoder.FallbackBuffer.Remaining;
WriteLine(decoder.FallbackBuffer.Remaining);
b3 = b3[1..]; // move forward
}
WriteLine(Encoding.UTF8.GetString(b3));
}
Doo()
�好啊,兄弟们哼哼哼
0
1
System.ArgumentException: Recursive fallback not allowed for bytes \xE5 \xA5 \xBD \xE5 \x95 \x8A \xEF \xBC \x8C \xE5 \x85 \x84 \xE5 \xBC \x9F \xE4 \xBB \xAC \xE5 \x93 .... (Parameter 'bytesUnknown')
+ System.Text.DecoderFallbackBuffer.ThrowLastBytesRecursive(byte[])
+ System.Text.DecoderReplacementFallbackBuffer.Fallback(byte[], int)
+ Submission#7.Doo()
Is this possible in C# ?
What do I need to detect the first valid byte's position?