Ejemplo n.º 1
0
        public FileAstronaut(string path, long?fileSizeLimitBytes, Encoding encoding = null, bool buffered = false)
        {
            if (string.IsNullOrWhiteSpace(path))
            {
                throw new ArgumentNullException(nameof(path));
            }
            if (fileSizeLimitBytes.HasValue && fileSizeLimitBytes < 0)
            {
                throw new ArgumentException("Negative value provided; file size limit must be non-negative.");
            }

            _fileSizeLimitBytes = fileSizeLimitBytes;
            _buffered           = buffered;

            var directory = Path.GetDirectoryName(path);

            if (!string.IsNullOrWhiteSpace(directory) && !Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }

            Stream output = _underlyingStream = System.IO.File.Open(path, FileMode.Append, FileAccess.Write, FileShare.Read);

            if (fileSizeLimitBytes != null)
            {
                output = _sizeLimitedStream = new SizeLimitedStream(_underlyingStream);
            }

            _output = new StreamWriter(output, encoding ?? new UTF8Encoding(encoderShouldEmitUTF8Identifier: false));
        }
Ejemplo n.º 2
0
    public void Ctor_NullInnerStream_Throws()
    {
        // Arrange
        Stream innerStream = null;

        // Act + Assert
        Assert.Throws <ArgumentNullException>(() =>
        {
            using var sizeLimitedStream = new SizeLimitedStream(innerStream, 1);
        });
    }
Ejemplo n.º 3
0
    public void BeginRead_InnerStreamExceedsSizeLimit_Throws(bool exceedsLimit)
    {
        // Arrange
        var sizeLimit = 10;
        var bytes     = new byte[sizeLimit + (exceedsLimit ? 1 : 0)];

        using var innerStream       = new MemoryStream(bytes);
        using var sizeLimitedStream = new SizeLimitedStream(innerStream, sizeLimit);

        var buffer = new byte[bytes.Length];

        // Act
        var exception = Record.Exception(() =>
        {
            var asyncResult = sizeLimitedStream.BeginRead(buffer, 0, buffer.Length, (o) => { }, null);
            sizeLimitedStream.EndRead(asyncResult);
        });

        // Assert
        AssertStreamReadingException(exception, exceedsLimit);
    }
Ejemplo n.º 4
0
    public void Read_InnerStreamExceedsSizeLimit_Throws(bool exceedsLimit)
    {
        // Arrange
        var sizeLimit = 10;
        var bytes     = new byte[sizeLimit + (exceedsLimit ? 1 : 0)];

        using var innerStream       = new MemoryStream(bytes);
        using var sizeLimitedStream = new SizeLimitedStream(innerStream, sizeLimit);

        var buffer = new byte[bytes.Length];

        // Act
        var exception = Record.Exception(() =>
        {
            while (sizeLimitedStream.Read(buffer, 0, buffer.Length) > 0)
            {
            }
        });

        // Assert
        AssertStreamReadingException(exception, exceedsLimit);
    }
Ejemplo n.º 5
0
    public async Task ReadAsync_InnerStreamExceedsSizeLimit_Throws(bool exceedsLimit)
    {
        // Arrange
        var sizeLimit = 10;
        var bytes     = new byte[sizeLimit + (exceedsLimit ? 1 : 0)];

        using var innerStream       = new MemoryStream(bytes);
        using var sizeLimitedStream = new SizeLimitedStream(innerStream, sizeLimit);

        var buffer = new byte[bytes.Length];

        // Act
        var exception = await Record.ExceptionAsync(async() =>
        {
            while (await sizeLimitedStream.ReadAsync(buffer) > 0)
            {
            }
        });

        // Assert
        AssertStreamReadingException(exception, exceedsLimit);
    }