public void Wrap_Original_Stream_On_Over() { var sut = SequenceStream.Using( stream => new StreamWriter( stream, Encoding.UTF8, leaveOpen: false, bufferSize: 128 ) ); var compressed = sut.Over( stream => new GZipStream( stream, leaveOpen: false, compressionLevel: CompressionLevel.Fastest ) ); using var wordsStream = compressed .On(new[] { "Read", "the", "content" }) .Writing((output, word) => output.Write($"{word} ")); using var reader = new StreamReader( new GZipStream(wordsStream, CompressionMode.Decompress), Encoding.UTF8 ); var result = reader.ReadToEnd(); result.Should().Be("Read the content "); }
public Stream Create <TItem, TContext>( Func <Stream, TContext> contextFactory, Action <TContext> dispose, IEnumerable <TItem> sequence, ArrayPool <byte> pool, Action <TContext, TItem> write) => SequenceStream .Using(contextFactory, dispose) .On(sequence.ToAsyncEnumerable()) .Pooling(pool) .Writing(write);
public static Stream BinaryContext(IEnumerable <Person> persons) => SequenceStream.Using( stream => new BinaryWriter(stream, Encoding.UTF8) ) .On(persons) .Writing( (binaryWriter, person) => { binaryWriter.Write(person.Name); binaryWriter.Write(person.Age); } );
public void Test() { using var source = new CancellationTokenSource(); var cancelled = false; using var strings = SequenceStream.Using(stream => new StreamWriter(stream, Encoding.UTF8)) .On(Yield()) .WithCancellation(source.Token) .Writing( (output, @string) => output.Write(@string) ); using var streamReader = new StreamReader(strings); Assert.ThrowsAsync <TaskCanceledException>( streamReader.ReadToEndAsync ); cancelled.Should().BeTrue(); async IAsyncEnumerable <string> Yield(
public static Stream ItemMetaInformationOnBuilder(IEnumerable <string> names) { return(SequenceStream.Using( stream => new StreamWriter(stream, Encoding.UTF8) ) .On(names) .AsItems() .Writing( (writer, item) => { if (item.Kind.IsFirst()) { writer.WriteLine("Names"); } writer.Write($"{item.Index}: {item.Value}"); if (!item.Kind.IsLast()) { writer.WriteLine(); } } )); }
public static SequenceStream.Builder <JsonWriter> Using(JsonWriterFactory factory) => SequenceStream.Using(factory.Create);