public static async Task <byte[]> New_ReadAsync(string fileName) { byte[] buffer = new byte[10]; using (var s = File.OpenRead(fileName)) using (var sr = new BufferedStreamReader(s)) await sr.ReadAsync(buffer); return(buffer); }
public static async Task <byte[]> New_ReadAsync(string fileName) { byte[] buffer = new byte[10]; using (var s = File.OpenRead(fileName)) using (var sr = new BufferedStreamReader(s, new BufferedStreamReaderConfig(totalPreFetchBlocks: 2))) await sr.ReadAsync(buffer); return(buffer); }
public async Task ReadAsync_CheckEOF2() { var streamMock = new Mock <Stream>(); int i = 1; streamMock.Setup(p => p.Read(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int>())) .Returns <byte[], int, int>((a, b, c) => i--); streamMock.Setup(p => p.ReadAsync(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <CancellationToken>())) .ReturnsAsync((byte[] a, int b, int c, CancellationToken d) => i--); byte[] tmparr = new byte[2]; BufferedStreamReader s = new BufferedStreamReader(streamMock.Object, GetConfig(1)); var r1 = await s.ReadAsync(tmparr, 0, 1); var r2 = await s.ReadAsync(tmparr, 1, 1); Assert.AreEqual(1, r1); Assert.AreEqual(0, r2); }
public async Task ReadAsync_InnerStreamCountBufferedBytes(int shadowBufferSize, int expectedBytesRead) { int bytesRead = 0; var streamMock = new Mock <Stream>(); streamMock.Setup(p => p.ReadAsync(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <CancellationToken>())) .Callback <byte[], int, int, CancellationToken>((a, b, c, d) => bytesRead += c) .ReturnsAsync((byte[] a, int b, int c, CancellationToken d) => c); streamMock.Setup(p => p.Read(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int>())) .Callback <byte[], int, int>((a, b, c) => bytesRead += c) .Returns <byte[], int, int>((a, b, c) => c); byte[] tmparr = new byte[3]; BufferedStreamReader s = new BufferedStreamReader(streamMock.Object, GetConfig(shadowBufferSize)); await s.ReadAsync(tmparr, 0, 1); await s.ReadAsync(tmparr, 1, 1); await s.ReadAsync(tmparr, 2, 1); Assert.AreEqual(expectedBytesRead, bytesRead); streamMock.Verify(f => f.ReadAsync(It.IsAny <byte[]>(), It.IsAny <int>(), It.Is <int>(f => f != shadowBufferSize), It.IsAny <CancellationToken>()), Times.Never()); }
public async Task ReadAsync_CorrectBytesAreReturned(int shadowBufferSize, int readBlockLength, int loopCount) { int totBytes = loopCount * readBlockLength; byte[] originalBuffer = Enumerable.Range(1, totBytes).Select(f => (byte)f).ToArray(); MemoryStream memStream = new MemoryStream(originalBuffer); byte[] finalBuffer = new byte[totBytes]; BufferedStreamReader s = new BufferedStreamReader(memStream, GetConfig(shadowBufferSize)); for (int f = 0, ix = 0; f < loopCount; f++, ix += readBlockLength) { await s.ReadAsync(finalBuffer, ix, readBlockLength); } Assert.AreEqual(originalBuffer, finalBuffer); }
public async Task ReadAsyncWithPreFetcher_CorrectBytesAreReturned(int totalPreFetchBlocks, int totBytes, int readBlockLength, int shadowBufferSize) { byte[] originalBuffer = Enumerable.Range(1, totBytes).Select(f => (byte)f).ToArray(); MemoryStream memStream = new MemoryStream(originalBuffer); byte[] finalBuffer = new byte[totBytes]; using (BufferedStreamReader s = new BufferedStreamReader(memStream, new BufferedStreamReaderConfig( totalPreFetchBlocks: totalPreFetchBlocks, shadowBufferSize: shadowBufferSize ))) { for (int f = 0, ix = 0; f < totBytes / readBlockLength; f++, ix += readBlockLength) { await s.ReadAsync(finalBuffer, ix, readBlockLength); } } Assert.AreEqual(originalBuffer, finalBuffer); }