public void Write_DoesNotThrow_IfBufferLimitIsReached() { // Arrange var input = new byte[5]; using var bufferingStream = new FileBufferingWriteStream(memoryThreshold: 2, bufferLimit: 10, tempFileDirectoryAccessor: () => TempDirectory); // Act bufferingStream.Write(input, 0, input.Length); bufferingStream.Write(input, 0, input.Length); // Should get to exactly the buffer limit, which is fine // If we got here, the test succeeded. }
public void Write_Throws_IfWriteCumulativeWritesExceedsBuffersLimit() { // Arrange var input = new byte[6]; var bufferingStream = new FileBufferingWriteStream(memoryThreshold: 2, bufferLimit: 10, tempFileDirectoryAccessor: () => TempDirectory); // Act bufferingStream.Write(input, 0, input.Length); var exception = Assert.Throws <IOException>(() => bufferingStream.Write(input, 0, input.Length)); Assert.Equal("Buffer limit exceeded.", exception.Message); // Verify we return the buffer. Assert.True(bufferingStream.Disposed); }
public void FileBufferWriteStreamFromAspNetCore() { using var writer = new FileBufferingWriteStream(memoryThreshold: MemoryThreshold); foreach (var chunk in GetChunks()) { writer.Write(chunk.Span); } writer.Flush(); using var ms = new MemoryStream(content.Length); writer.DrainBufferAsync(ms).GetAwaiter().GetResult(); }
public void Write_AfterMemoryThresholdIsReached_BuffersToMemory() { // Arrange var input = new byte[] { 1, 2, 3, 4, 5, 6, 7 }; using var bufferingStream = new FileBufferingWriteStream(memoryThreshold: 4, tempFileDirectoryAccessor: () => TempDirectory); // Act bufferingStream.Write(input, 0, 5); bufferingStream.Write(input, 5, 2); // Assert var pageBuffer = bufferingStream.PagedByteBuffer; var fileStream = bufferingStream.FileStream; // File should have been created. Assert.NotNull(fileStream); var fileBytes = ReadFileContent(fileStream !); Assert.Equal(new byte[] { 1, 2, 3, 4, 5, }, fileBytes); Assert.Equal(new byte[] { 6, 7 }, ReadBufferedContent(pageBuffer)); }
public void Write_BuffersContentToDisk_WhenMemoryThresholdIsReached() { // Arrange var input = new byte[] { 1, 2, 3, }; using var bufferingStream = new FileBufferingWriteStream(memoryThreshold: 2, tempFileDirectoryAccessor: () => TempDirectory); bufferingStream.Write(input, 0, 2); // Act bufferingStream.Write(input, 2, 1); // Assert var pageBuffer = bufferingStream.PagedByteBuffer; var fileStream = bufferingStream.FileStream; // File should have been created. Assert.NotNull(fileStream); var fileBytes = ReadFileContent(fileStream !); Assert.Equal(input, fileBytes); // No content should be in the memory stream Assert.Equal(0, pageBuffer.Length); }
public async Task DrainBufferAsync_WithContentInDisk_CopiesContentFromMemoryStream() { // Arrange var input = Enumerable.Repeat((byte)0xca, 30).ToArray(); using var bufferingStream = new FileBufferingWriteStream(memoryThreshold: 21, tempFileDirectoryAccessor: () => TempDirectory); bufferingStream.Write(input, 0, input.Length); var memoryStream = new MemoryStream(); // Act await bufferingStream.DrainBufferAsync(memoryStream, default); // Assert Assert.Equal(input, memoryStream.ToArray()); Assert.Equal(0, bufferingStream.Length); }
public async Task DrainBufferAsync_CopiesContentFromMemoryStream() { // Arrange var input = new byte[] { 1, 2, 3, 4, 5 }; using var bufferingStream = new FileBufferingWriteStream(tempFileDirectoryAccessor: () => TempDirectory); bufferingStream.Write(input, 0, input.Length); var memoryStream = new MemoryStream(); // Act await bufferingStream.DrainBufferAsync(memoryStream, default); // Assert Assert.Equal(input, memoryStream.ToArray()); Assert.Equal(0, bufferingStream.Length); }
public async Task <ActionResult> DrawAllowanceAsync(InvoiceQueryViewModel viewModel) { String viewUrl = $"{Startup.Properties["HostDomain"]}{VirtualPathUtility.ToAbsolute("~/Invoice/CanvasPrintAllowance")}{Request.QueryString}"; var converter = new HtmlConverter(); var bytes = converter.FromUrl(viewUrl); Response.ContentType = "image/jpeg"; using (FileBufferingWriteStream output = new FileBufferingWriteStream()) { output.Write(bytes); //output.Seek(0, SeekOrigin.Begin); await output.DrainBufferAsync(Response.Body); } return(new EmptyResult()); }
public void Write_BuffersContentToMemory() { // Arrange using var bufferingStream = new FileBufferingWriteStream(tempFileDirectoryAccessor: () => TempDirectory); var input = Encoding.UTF8.GetBytes("Hello world"); // Act bufferingStream.Write(input, 0, input.Length); // Assert Assert.Equal(input.Length, bufferingStream.Length); // We should have written content to memory var pagedByteBuffer = bufferingStream.PagedByteBuffer; Assert.Equal(input, ReadBufferedContent(pagedByteBuffer)); // No files should not have been created. Assert.Null(bufferingStream.FileStream); }
public void Write_BeforeMemoryThresholdIsReached_WritesToMemory() { // Arrange var input = new byte[] { 1, 2, }; using var bufferingStream = new FileBufferingWriteStream(memoryThreshold: 2, tempFileDirectoryAccessor: () => TempDirectory); // Act bufferingStream.Write(input, 0, 2); // Assert var pageBuffer = bufferingStream.PagedByteBuffer; var fileStream = bufferingStream.FileStream; Assert.Equal(input.Length, bufferingStream.Length); // File should have been created. Assert.Null(fileStream); // No content should be in the memory stream Assert.Equal(2, pageBuffer.Length); Assert.Equal(input, ReadBufferedContent(pageBuffer)); }