Exemplo n.º 1
0
    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.
    }
Exemplo n.º 2
0
    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();
        }
Exemplo n.º 4
0
    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));
    }
Exemplo n.º 5
0
    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);
    }
Exemplo n.º 6
0
    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);
    }
Exemplo n.º 7
0
    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());
        }
Exemplo n.º 9
0
    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);
    }
Exemplo n.º 10
0
    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));
    }