Пример #1
0
        public static async Task OverlappingFlushAsync_DuringFlushAsync()
        {
            byte[] buffer = null;
            string testFilePath = gzTestFile("GZTestDocument.pdf");
            using (var origStream = await LocalMemoryStream.readAppFileAsync(testFilePath))
            {
                buffer = origStream.ToArray();
            }

            using (var writeStream = new ManualSyncMemoryStream(false))
            using (var zip = new DeflateStream(writeStream, CompressionMode.Compress))
            {
                Task task = null;
                try
                {
                    writeStream.manualResetEvent.Set();
                    await zip.WriteAsync(buffer, 0, buffer.Length);
                    writeStream.manualResetEvent.Reset();
                    writeStream.WriteHit = false;
                    task = zip.FlushAsync();
                    Assert.True(writeStream.WriteHit);
                    Assert.Throws<InvalidOperationException>(() => { zip.FlushAsync(); }); // "overlapping flushes"
                }
                finally
                {
                    // Unblock Async operations
                    writeStream.manualResetEvent.Set();
                    // The original WriteAsync should be able to complete
                    Assert.True(task.Wait(100 * 500));
                }
            }
        }
Пример #2
0
        public async Task FlushAsyncFailsAfterDispose()
        {
            var ms = new MemoryStream();
            var ds = new DeflateStream(ms, CompressionMode.Compress);

            ds.Dispose();

            await Assert.ThrowsAsync <ObjectDisposedException>(async() =>
            {
                await ds.FlushAsync();
            });
        }
        /// <inheritdoc/>
        public async Task <Stream> DecompressAsync(Stream stream, int bufferSize, CancellationToken cancellationToken)
        {
            var decompressedStream = new MemoryStream();

            using (var decompressionStream = new DeflateStream(stream, CompressionMode.Decompress))
            {
                await decompressionStream.CopyToAsync(decompressedStream, bufferSize, cancellationToken).ConfigureAwait(false);

                await decompressionStream.FlushAsync().ConfigureAwait(false);
            }

            return(decompressedStream);
        }
Пример #4
0
        public void Precancellation()
        {
            var ms = new MemoryStream();

            using (DeflateStream ds = new DeflateStream(ms, CompressionMode.Compress, leaveOpen: true))
            {
                Assert.True(ds.WriteAsync(new byte[1], 0, 1, new CancellationToken(true)).IsCanceled);
                Assert.True(ds.FlushAsync(new CancellationToken(true)).IsCanceled);
            }
            using (DeflateStream ds = new DeflateStream(ms, CompressionMode.Decompress, leaveOpen: true))
            {
                Assert.True(ds.ReadAsync(new byte[1], 0, 1, new CancellationToken(true)).IsCanceled);
            }
        }
        public override async Task CloseAsync()
        {
            if (_isClosed)
            {
                return;
            }

            _isClosed = true;
            await _deflate.FlushAsync().ConfigureAwait(false);

            SafeEnd.Dispose(_deflate); // TODO DeflateStream cant async flush buffer so this will cause sync Write call and blocks one thread from pool
            await _inner.WriteAsync(BFINAL, 0, 1).ConfigureAwait(false);

            await _inner.CloseAsync().ConfigureAwait(false);
        }
Пример #6
0
        public static async Task <Stream> Compress(this Stream stream)
        {
            stream.Seek(0, SeekOrigin.Begin);
            var compressed = new MemoryStream();
            var deflate    = new DeflateStream(compressed, CompressionLevel.Optimal, true);
            await stream.CopyToAsync(deflate).ConfigureAwait(false);

            await deflate.FlushAsync().ConfigureAwait(false);

            deflate.Close();
            await compressed.FlushAsync().ConfigureAwait(false);

            compressed.Seek(0, SeekOrigin.Begin);

            return(compressed);
        }
Пример #7
0
        /// <summary>
        /// Saves the definitions asynchronous.
        /// </summary>
        /// <param name="storagePath">The storage path.</param>
        /// <param name="game">The game.</param>
        /// <param name="definitions">The definitions.</param>
        /// <returns>Task&lt;System.Boolean&gt;.</returns>
        /// <exception cref="ArgumentException">Definitions types differ.</exception>
        public virtual async Task <bool> SaveDefinitionsAsync(string storagePath, IGame game, IEnumerable <IDefinition> definitions)
        {
            storagePath = ResolveStoragePath(storagePath);
            if (definitions.GroupBy(p => p.ParentDirectory).Count() > 1)
            {
                throw new ArgumentException("Definitions types differ.");
            }
            if (definitions == null || !definitions.Any())
            {
                return(false);
            }
            var path     = SanitizePath(definitions.FirstOrDefault().ParentDirectory);
            var fullPath = Path.Combine(storagePath, game.Type, path);

            if (File.Exists(fullPath))
            {
                DiskOperations.DeleteFile(fullPath);
            }
            var json  = JsonDISerializer.Serialize(definitions.ToList());
            var bytes = Encoding.UTF8.GetBytes(json);

            using var source      = new MemoryStream(bytes);
            using var destination = new MemoryStream();
            using var compress    = new DeflateStream(destination, CompressionLevel.Fastest, true);
            await source.CopyToAsync(compress);

            await compress.FlushAsync();

            if (!Directory.Exists(Path.GetDirectoryName(fullPath)))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(fullPath));
            }
            await File.WriteAllBytesAsync(fullPath, destination.ToArray());

            return(true);
        }
Пример #8
0
        public async Task OverlappingFlushAsync_DuringReadAsync()
        {
            byte[] buffer       = new byte[32];
            string testFilePath = gzTestFile("GZTestDocument.pdf.gz");

            using (var readStream = await ManualSyncMemoryStream.GetStreamFromFileAsync(testFilePath, false, true))
                using (var unzip = new DeflateStream(readStream, CompressionMode.Decompress, true))
                {
                    Task task = null;
                    try
                    {
                        task = ReadAsync(unzip, buffer, 0, 32);
                        Assert.True(readStream.ReadHit);
                        Assert.Throws <InvalidOperationException>(() => { unzip.FlushAsync(); }); // "overlapping read"
                    }
                    finally
                    {
                        // Unblock Async operations
                        readStream.manualResetEvent.Set();
                        // The original ReadAsync should be able to complete
                        Assert.True(task.Wait(100 * 500));
                    }
                }
        }
Пример #9
0
 public async Task OverlappingFlushAsync_DuringReadAsync()
 {
     byte[] buffer = new byte[32];
     string testFilePath = gzTestFile("GZTestDocument.pdf.gz");
     using (var readStream = await ManualSyncMemoryStream.GetStreamFromFileAsync(testFilePath, false, true))
     using (var unzip = new DeflateStream(readStream, CompressionMode.Decompress, true))
     {
         Task task = null;
         try
         {
             task = ReadAsync(unzip, buffer, 0, 32);
             Assert.True(readStream.ReadHit);
             Assert.Throws<InvalidOperationException>(() => { unzip.FlushAsync(); }); // "overlapping read"
         }
         finally
         {
             // Unblock Async operations
             readStream.manualResetEvent.Set();
             // The original ReadAsync should be able to complete
             Assert.True(task.Wait(100 * 500));
         }
     }
 }
Пример #10
0
 public void Precancellation()
 {
     var ms = new MemoryStream();
     using (DeflateStream ds = new DeflateStream(ms, CompressionMode.Compress, leaveOpen: true))
     {
         Assert.True(ds.WriteAsync(new byte[1], 0, 1, new CancellationToken(true)).IsCanceled);
         Assert.True(ds.FlushAsync(new CancellationToken(true)).IsCanceled);
     }
     using (DeflateStream ds = new DeflateStream(ms, CompressionMode.Decompress, leaveOpen: true))
     {
         Assert.True(ds.ReadAsync(new byte[1], 0, 1, new CancellationToken(true)).IsCanceled);
     }
 }
Пример #11
0
        public async Task FlushAsyncFailsAfterDispose()
        {
            var ms = new MemoryStream();
            var ds = new DeflateStream(ms, CompressionMode.Compress);
            ds.Dispose();

            await Assert.ThrowsAsync<ObjectDisposedException>(async () =>
            {
                await ds.FlushAsync();
            });
        }
Пример #12
0
 public async Task Flush()
 {
     var ms = new MemoryStream();
     var ds = new DeflateStream(ms, CompressionMode.Compress);
     ds.Flush();
     await ds.FlushAsync();
 }
Пример #13
0
        public async Task Flush()
        {
            var ms = new MemoryStream();
            var ds = new DeflateStream(ms, CompressionMode.Compress);
            ds.Flush();
            await ds.FlushAsync();

            // Just ensuring Flush doesn't throw
        }
Пример #14
0
 public Task FlushAsync()
 {
     return(DeflateStreamInstance.FlushAsync());
 }