Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asynchronous BinaryReader and BinaryWriter in .Net?

Tags:

.net

f#

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?

like image 763
Henrik Avatar asked Apr 25 '12 12:04

Henrik


1 Answers

You can use FileStream, make sure you call the constructor properly so it enables async and has a large enough buffer.

var buffer = new byte[2048];

using var r = new FileStream(
                  Path,
                  FileMode.Open, 
                  FileAccess.ReadWrite, 
                  FileShare.None, 
                  2048000, 
                  FileOptions.Asynchronous);

await r.ReadAsync(buffer, 0, 2048);
await r.Writesync(buffer);

Now that you have a byte array you can have three options to convert them to objects:

  • use BitConverter
  • use BinaryFormatter and MemoryStream
  • write your own custom logic
like image 166
Bizhan Avatar answered Sep 24 '22 08:09

Bizhan