Hello,
Welcome to our Microsoft Q&A platform!
First, the reason why you have the error is that the DataReader is not fully loaded.
Your data consists of uint, string and byte array.
uint fileLength = reader.ReadUInt32();
uint actualFileLength = await reader.LoadAsync(fileLength);
This above code indicates that you loaded the fileLength length data in the DataReader, but in fact, this part of the data contains a string value of length 5.
So when you read the string value, the remaining data is not enough to fill your byte array, as a result, you will get an error.
To meet your needs, you need to call LoadAsync method twice.
Like this:
uint sizeFieldCount = await reader.LoadAsync(sizeof(uint));
if (sizeFieldCount != sizeof(uint))
{
return;
}
uint fileLength = reader.ReadUInt32();
// Read the string.
await reader.LoadAsync(5);
string type = reader.ReadString(5);
// Read the bytes.
uint actualFileLength = await reader.LoadAsync(fileLength);
if (fileLength != actualFileLength)
{
return;
}
var bytes = new byte[actualFileLength];
reader.ReadBytes(bytes);
Thanks