public static T StreamToMessage <T>(Stream inStream) where T : IRocketPackObject <T> { using var hub = new BytesHub(); const int bufferSize = 4096; while (inStream.Position < inStream.Length) { var readLength = inStream.Read(hub.Writer.GetSpan(bufferSize)); if (readLength < 0) { break; } hub.Writer.Advance(readLength); } return(IRocketPackObject <T> .Import(hub.Reader.GetSequence(), BytesPool.Shared)); }
public static async IAsyncEnumerable <TValue> GetValuesAsync <TKey, TValue>(this IBytesStorage <TKey> bytesStorage, [EnumeratorCancellation] CancellationToken cancellationToken = default) where TKey : notnull, IEquatable <TKey> where TValue : IRocketPackObject <TValue> { var bytesPool = BytesPool.Shared; using var hub = new BytesHub(bytesPool); await foreach (var key in bytesStorage.GetKeysAsync(cancellationToken)) { if (!await bytesStorage.TryReadAsync(key, hub.Writer, cancellationToken)) { continue; } var value = IRocketPackObject <TValue> .Import(hub.Reader.GetSequence(), bytesPool); yield return(value); hub.Reset(); } }