public async Task TestCompositeStream() { const int LineCount = 100; var ms = new MemoryStream(); var cs = new CircularStream(); var task = Task.Run(() => { using (var stream = cs.CreateReaderView()) { return(SHA1.Create().ComputeHash(stream)); } }); byte[] expected; using (var ds = new CompositeStream(ms, cs.CreateWriterView())) { var sw = new StreamWriter(ds); for (int i = 0; i < LineCount; i++) { sw.WriteLine($"Line {i}: test!"); } sw.Flush(); expected = SHA1.Create().ComputeHash(ms.ToArray()); } Assert.Equal(expected, await task); }
public void TestCircularStream() { const int LineCount = 100; var cs = new CircularStream(); Task.Run(() => { using (var writer = cs.CreateWriterView()) using (var sw = new StreamWriter(writer)) { for (int i = 0; i < LineCount; i++) { sw.WriteLine($"Line {i}: test!"); } } }); using (var reader = cs.CreateReaderView()) using (var sr = new StreamReader(reader)) { for (int i = 0; i < LineCount; i++) { Assert.Equal($"Line {i}: test!", sr.ReadLine()); } Assert.Equal(string.Empty, sr.ReadToEnd()); } }
public static Stream WithSha256Hash(this Stream stream, out Task <byte[]> hashTask) { var cs = new CircularStream(); hashTask = Task.Run(() => { using var csr = cs.CreateReaderView(); return(HashUtility.GetSha256Hash(csr)); }); return(new CompositeStream(stream, cs.CreateWriterView())); }
public static Stream WithMd5Hash(this Stream stream, out Task <byte[]> hashTask) { var cs = new CircularStream(); hashTask = Task.Run(() => { using var csr = cs.CreateReaderView(); return(MD5.Create().ComputeHash(csr)); }); return(new CompositeStream(stream, cs.CreateWriterView())); }