Asynchronous BinaryReader and BinaryWriter in .Net?

后端 未结 3 385
说谎
说谎 2021-01-11 23:40

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 wou

3条回答
  •  悲&欢浪女
    2021-01-12 00:47

    I've created versions of those classes that are async: https://github.com/ronnieoverby/AsyncBinaryReaderWriter

    Example usage:

    async Task Main()
    {
        using var ms = new MemoryStream();
        using var writer = new AsyncBinaryWriter(ms);
        await writer.WriteAsync("today is: ");
        await writer.WriteAsync(DateTime.Today.Ticks);
        await writer.FlushAsync();
    
        ms.Position = 0;
    
        using var reader = new AsyncBinaryReader(ms);
        var preamble = await reader.ReadStringAsync();
        var payload = new DateTime(await reader.ReadInt64Async());
    
        Console.WriteLine(preamble + payload);
    }
    

    All of the same read/write methods present on the BCL binary reader/writer classes are present.

提交回复
热议问题