.Net 中的异步 BinaryReader 和 BinaryWriter? [英] Asynchronous BinaryReader and BinaryWriter in .Net?

查看:23
本文介绍了.Net 中的异步 BinaryReader 和 BinaryWriter?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想异步读取和写入字节和结构化值类型,而不必担心解码器和字节移位:有什么可以让我这样做的吗?

I'd like to read and write bytes and structured value types, asynchronously, without having to worry about decoders and byte-shifting: is there something out there that would allow me to do that?

推荐答案

BinaryReaderBinaryWriter 无法实现.您可以从底层 BaseStream 并发读取,但是 文档 声明如下:

It's not possible with BinaryReader or BinaryWriter. You could read concurrently from the underlying BaseStream, but the documentation states the following:

在读取或使用 BinaryReader 时使用底层流会导致数据丢失和损坏.例如,相同的字节可能会被多次读取,字节可能会被跳过,或者字符读取可能变得不可预测.

Using the underlying stream while reading or while using the BinaryReader can cause data loss and corruption. For example, the same bytes might be read more than once, bytes might be skipped, or character reading might become unpredictable.

因此,唯一的方法是推出您自己的实现.但这样做的好处是有争议的.来自 Microsoft 的 Marco Greg 在博客文章中添加了以下评论 Should我为同步方法公开了异步包装器?:

Therefore the only way is to roll your own implementation. But the benefit of doing that is debatable. Marco Greg from Microsoft added the following comment to the blog article Should I expose asynchronous wrappers for synchronous methods?:

Jon:BinaryReader/Writer 没有 XxxAsync 方法的原因是这些类型的方法通常只从先前打开的底层流中读取/写入很少的字节.在实践中,数据经常被缓存,并且从底层源获取数据所需的时间通常非常短,不值得异步执行.

Jon: The reason that the BinaryReader/Writer do not have XxxAsync methods is that the methods on those types typically read/write only very few bytes from an underlying stream that has been previously opened. In practice, the data is frequently cached and the time required to fetch the data from the underlying source is typically so small that it is not worth it doing it asynchronously.

值得注意的是,这些类型的一些方法在某些情况下可能会传输大量数据(例如 ReadString).更进一步,这些方法的异步版本可能会添加,也可能不会添加,但在不久的将来不太可能发生.

Notably, there are some methods on these types that in some circumstances may transfer larger amounts of data (e.g. ReadString). Further down the line, Async versions for those methods may or may not be added, but it is unlikely it will happen in the immediate future.

一般来说,如果您正在读取的数据量很大(至少数百或数千字节),或者您是第一次访问资源(例如第一次读取),您应该只考虑异步 IO 方法即使您正在读取一个字节,从一个文件可能需要启动磁盘).

In general, you should only consider Async IO methods if the amount of data you are reading is significant (at least several hundreds or thousands of bytes), or if you are accessing a resource for the first time (e.g. a first read from a file may require to spin up the disk even if you are reading one byte).

这听起来很合理.如果您确实需要解决方案,除了滚动您自己的 BinaryReader/BinaryWriter 之外,还有多种解决方法.您可以在单独的线程中运行它(这可能效率低下),或者如果您愿意更改文件格式或有线协议,您可以使用此模式(伪代码):

This sounds reasonable. If you do need a solution there are several workarounds apart from rolling your own BinaryReader/BinaryWriter. You can run it in a separate thread (which can be inefficient) or if you're willing to change the file format or wire protocol you could use this pattern (pseudo-code):

//read packet length
await stream.ReadAsync(buffer);
var packetLength=convertToInt(buffer);

//read complete packet asynchronously
await stream.ReadAsync(buffer,packetLength);

//process packet with BinaryReader
using(var br=new BinaryReader(new MemoryStream(buffer))
{
  //...
}

请注意,此模式仅在完整缓冲区易于装入内存且性能可能会受到影响时才有用.

Please note that this pattern is only useful if the complete buffer easily fits in memory and that performance might suffer.

这篇关于.Net 中的异步 BinaryReader 和 BinaryWriter?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆