我有一个在do...while
循环期间读取一些数据的 MemoryStream,我需要检查已读取的最后 5 个字节。
如何访问 MemoryStream 中读取的最后 5 个字节?
只需将位置设置为后面 5 个字节,然后重新读取这些字节:
byte[] GetLast5BytesRead(MemoryStream stream)
{
// TODO: Validation that stream.Position is at least 5
byte[] ret = new byte[5];
stream.Position -= 5;
// TODO: Consider throwing an exception if this doesn't return 5
Stream.Read(ret, 0, 5);
return ret;
}