Esempio n. 1
0
        public static async Task OverlappingWriteAsync()
        {
            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
                    {
                        task = zip.WriteAsync(buffer, 0, buffer.Length);                                      // write needs to be bigger than the internal write buffer
                        Assert.True(writeStream.WriteHit);
                        Assert.Throws <InvalidOperationException>(() => { zip.WriteAsync(buffer, 32, 32); }); // "overlapping write"
                    }
                    finally
                    {
                        // Unblock Async operations
                        writeStream.manualResetEvent.Set();
                        // The original WriteAsync should be able to complete
                        Assert.True(task.Wait(100 * 500));
                    }
                }
        }
        private static async Task WriteCompressedStreamAsync(
            DeflateStream compressedStream,
            FileStream sourceFileStream,
            BinaryWriter final,
            CancellationToken ct        = default,
            IProgress <double> progress = null)
        {
            var buffer         = new byte[4096];
            var totalBytesRead = 0L;
            int bytesRead;
            var fileLength   = sourceFileStream.Length;
            var lastProgress = 0d;

            while ((bytesRead = await sourceFileStream.ReadAsync(buffer, 0, buffer.Length)) > 0)
            {
                ct.ThrowIfCancellationRequested();

                if (bytesRead > fileLength)
                {
                    totalBytesRead += fileLength;
                    await compressedStream.WriteAsync(buffer, 0, Convert.ToInt32(fileLength));
                }
                else if (totalBytesRead + bytesRead <= fileLength)
                {
                    totalBytesRead += bytesRead;
                    await compressedStream.WriteAsync(buffer, 0, bytesRead);
                }
                else if (totalBytesRead + bytesRead > fileLength)
                {
                    var leftToRead = fileLength - totalBytesRead;
                    totalBytesRead += leftToRead;
                    final.Write(buffer, 0, Convert.ToInt32(leftToRead));
                }

                var newProgress = (double)totalBytesRead / fileLength * 100;

                if (newProgress - lastProgress > 1)
                {
                    progress?.Report(newProgress);
                    lastProgress = newProgress;
                }

                if (totalBytesRead >= fileLength)
                {
                    break;
                }
            }
        }
Esempio n. 3
0
        public override async Task ProcessFrame(WebSocketFrame f)
        {
            if (f.OpCode == WebSocketOpCode.BinaryFrame || f.OpCode == WebSocketOpCode.TextFrame)
            {
                try
                {
                    using (MemoryStream ms = new MemoryStream())
                    {
                        using (DeflateStream df = new DeflateStream(ms, CompressionMode.Compress, true))
                        {
                            await df.WriteAsync(f.Payload, 0, f.Payload.Length);
                        }

                        await ms.WriteAsync(new byte[] { 0x00 }, 0, 1);

                        f.Flags        |= WebSocketFlags.RSV3;
                        f.Payload       = ms.ToArray();
                        f.PayloadLength = (ulong)f.Payload.Length;
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            }
        }
Esempio n. 4
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));
                }
            }
        }
Esempio n. 5
0
        public static async Task <byte[]> CompressBytesAsync(byte[] data, int offset, int length,
                                                             CompressionLevel compressionLevel = CompressionLevel.Fastest)
        {
            await using var ms = new MemoryStream();
            await using (var ds = new DeflateStream(ms, compressionLevel))
            {
                await ds.WriteAsync(data, offset, length);
            }

            var compressed = ms.ToArray();
            var buffer     = new byte[2 /* zlib header */ + compressed.Length + 4 /* Adler32 checksum */];

            buffer[0] = 0x78;
            buffer[1] = (byte)(
                compressionLevel == CompressionLevel.Optimal ? 0xDA :
                compressionLevel == CompressionLevel.Fastest ? 0x9C : 0x01
                );

            var adler = new Adler32();

            adler.Update(data, offset, length);

            Buffer.BlockCopy(compressed, 0, buffer, 2, compressed.Length);
            Buffer.BlockCopy(adler.Checksum, 0, buffer, compressed.Length + 2, 4);

            return(buffer);
        }
Esempio n. 6
0
        /// <summary>
        /// Get the compressed(or not) output data according to request.
        /// </summary>
        /// <param name="context">The http context.</param>
        /// <param name="data">The data to output.</param>
        /// <param name="cancellationToken">The cancellation token which will cancel this method.</param>
        /// <returns>The compressed or not output data.</returns>
        private async Task <byte[]> GetOutputDataAsync(IJsonRpcHttpContext context, byte[] data, CancellationToken cancellationToken = default)
        {
            var outputData     = data;
            var acceptEncoding = context.GetRequestHeader("Accept-Encoding");

            if (acceptEncoding != null && acceptEncoding.Contains("gzip"))
            {
                context.SetResponseHeader("Content-Encoding", "gzip");
                await using var memoryStream = new MemoryStream();
                await using var outputStream = new GZipStream(memoryStream, CompressionMode.Compress);
                await outputStream.WriteAsync(outputData, cancellationToken).ConfigureAwait(false);

                await outputStream.FlushAsync(cancellationToken).ConfigureAwait(false);

                outputData = memoryStream.ToArray();
            }
            else if (acceptEncoding != null && acceptEncoding.Contains("deflate"))
            {
                context.SetResponseHeader("Content-Encoding", "deflate");
                await using var memoryStream = new MemoryStream();
                await using var outputStream = new DeflateStream(memoryStream, CompressionMode.Compress);
                await outputStream.WriteAsync(outputData, cancellationToken).ConfigureAwait(false);

                await outputStream.FlushAsync(cancellationToken).ConfigureAwait(false);

                outputData = memoryStream.ToArray();
            }
            return(outputData);
        }
        public async ValueTask <ArraySegment <byte> > CompressAsync(ReadOnlyMemory <byte> inputData)
        {
            Guard.AgainstEmpty(inputData, nameof(inputData));

            var compressedStream = RecyclableManager.GetStream(nameof(RecyclableDeflateProvider));

            using (var deflateStream = new DeflateStream(compressedStream, CompressionLevel, true))
            {
                await deflateStream
                .WriteAsync(inputData)
                .ConfigureAwait(false);
            }

            if (compressedStream.TryGetBuffer(out var buffer))
            {
                return(buffer);
            }
            else
            {
                using (compressedStream) // dispose stream after allocation.
                {
                    return(compressedStream.ToArray());
                }
            }
        }
Esempio n. 8
0
        /// <summary>
        /// ファイルの暗号化の基本操作を行います
        /// </summary>
        private async Task FileEncryption(string password, string readPath, string outPath, CancellationToken token)
        {
            using (FileStream writefs = new FileStream(outPath, FileMode.Create, FileAccess.Write))
            {
                using (CryptoStream cse = new CryptoStream
                                              (await EncryptionWriteAsync(password, writefs), rijndael.CreateEncryptor(rijndael.Key, rijndael.IV), CryptoStreamMode.Write))
                {
                    using (DeflateStream ds = new DeflateStream(cse, CompressionMode.Compress))
                    {
                        using (FileStream readfs = new FileStream(readPath, FileMode.Open, FileAccess.Read))
                        {
                            long progress = 0; int len = 0; byte[] buffer = new byte[4096];
                            while ((len = readfs.Read(buffer, 0, 4096)) > 0)
                            {
                                await ds.WriteAsync(buffer, 0, len);

                                Progress.EncryptionUpdate(progress); progress += len; //進捗状況

                                token.ThrowIfCancellationRequested();                 //処理の中断
                            }
                            Progress.MaximumPatch(outPath);                           //余ったプログレスバーの最大値を埋める
                        }
                    }
                }
            }
        }
Esempio n. 9
0
        public void ReadWriteArgumentValidation()
        {
            using (var ds = new DeflateStream(new MemoryStream(), CompressionMode.Compress))
            {
                Assert.Throws <ArgumentNullException>(() => ds.Write(null, 0, 0));
                Assert.Throws <ArgumentOutOfRangeException>(() => ds.Write(new byte[1], -1, 0));
                Assert.Throws <ArgumentOutOfRangeException>(() => ds.Write(new byte[1], 0, -1));
                AssertExtensions.Throws <ArgumentException>(null, () => ds.Write(new byte[1], 0, 2));
                AssertExtensions.Throws <ArgumentException>(null, () => ds.Write(new byte[1], 1, 1));
                Assert.Throws <InvalidOperationException>(() => ds.Read(new byte[1], 0, 1));
                ds.Write(new byte[1], 0, 0);
            }
            using (var ds = new DeflateStream(new MemoryStream(), CompressionMode.Compress))
            {
                Assert.Throws <ArgumentNullException>(() => { ds.WriteAsync(null, 0, 0); });
                Assert.Throws <ArgumentOutOfRangeException>(() => { ds.WriteAsync(new byte[1], -1, 0); });
                Assert.Throws <ArgumentOutOfRangeException>(() => { ds.WriteAsync(new byte[1], 0, -1); });
                AssertExtensions.Throws <ArgumentException>(null, () => { ds.WriteAsync(new byte[1], 0, 2); });
                AssertExtensions.Throws <ArgumentException>(null, () => { ds.WriteAsync(new byte[1], 1, 1); });
                Assert.Throws <InvalidOperationException>(() => { ds.Read(new byte[1], 0, 1); });
            }

            using (var ds = new DeflateStream(new MemoryStream(), CompressionMode.Decompress))
            {
                Assert.Throws <ArgumentNullException>(() => ds.Read(null, 0, 0));
                Assert.Throws <ArgumentOutOfRangeException>(() => ds.Read(new byte[1], -1, 0));
                Assert.Throws <ArgumentOutOfRangeException>(() => ds.Read(new byte[1], 0, -1));
                AssertExtensions.Throws <ArgumentException>(null, () => ds.Read(new byte[1], 0, 2));
                AssertExtensions.Throws <ArgumentException>(null, () => ds.Read(new byte[1], 1, 1));
                Assert.Throws <InvalidOperationException>(() => ds.Write(new byte[1], 0, 1));

                var data = new byte[1] {
                    42
                };
                Assert.Equal(0, ds.Read(data, 0, 0));
                Assert.Equal(42, data[0]);
            }
            using (var ds = new DeflateStream(new MemoryStream(), CompressionMode.Decompress))
            {
                Assert.Throws <ArgumentNullException>(() => { ds.ReadAsync(null, 0, 0); });
                Assert.Throws <ArgumentOutOfRangeException>(() => { ds.ReadAsync(new byte[1], -1, 0); });
                Assert.Throws <ArgumentOutOfRangeException>(() => { ds.ReadAsync(new byte[1], 0, -1); });
                AssertExtensions.Throws <ArgumentException>(null, () => { ds.ReadAsync(new byte[1], 0, 2); });
                AssertExtensions.Throws <ArgumentException>(null, () => { ds.ReadAsync(new byte[1], 1, 1); });
                Assert.Throws <InvalidOperationException>(() => { ds.Write(new byte[1], 0, 1); });
            }
        }
Esempio n. 10
0
        private static async Task ResponseBase(HttpListenerRequest request, HttpListenerResponse response, byte[] content, HttpStatusCode statusCode = HttpStatusCode.OK)
        {
            if ((request == null) || (response == null) || (content == null) || (content.Length == 0))
            {
                ASF.ArchiLogger.LogNullError(nameof(request) + " || " + nameof(response) + " || " + nameof(content));
                return;
            }

            try {
                if (response.StatusCode != (ushort)statusCode)
                {
                    response.StatusCode = (ushort)statusCode;
                }

                response.AddHeader("Access-Control-Allow-Origin", "*");
                response.AddHeader("Date", DateTime.UtcNow.ToString("R"));

                if (CompressableContentTypes.Contains(response.ContentType))
                {
                    string acceptEncoding = request.Headers["Accept-Encoding"];

                    if (!string.IsNullOrEmpty(acceptEncoding))
                    {
                        if (acceptEncoding.Contains("gzip"))
                        {
                            response.AddHeader("Content-Encoding", "gzip");
                            using (MemoryStream ms = new MemoryStream()) {
                                using (GZipStream stream = new GZipStream(ms, CompressionMode.Compress)) {
                                    await stream.WriteAsync(content, 0, content.Length).ConfigureAwait(false);
                                }

                                content = ms.ToArray();
                            }
                        }
                        else if (acceptEncoding.Contains("deflate"))
                        {
                            response.AddHeader("Content-Encoding", "deflate");
                            using (MemoryStream ms = new MemoryStream()) {
                                using (DeflateStream stream = new DeflateStream(ms, CompressionMode.Compress)) {
                                    await stream.WriteAsync(content, 0, content.Length).ConfigureAwait(false);
                                }

                                content = ms.ToArray();
                            }
                        }
                    }
                }

                response.ContentLength64 = content.Length;
                await response.OutputStream.WriteAsync(content, 0, content.Length).ConfigureAwait(false);
            } catch (HttpListenerException e) {
                ASF.ArchiLogger.LogGenericDebuggingException(e);
            } catch (ObjectDisposedException e) {
                ASF.ArchiLogger.LogGenericDebuggingException(e);
            } catch (Exception e) {
                ASF.ArchiLogger.LogGenericException(e);
                await ResponseStatusCode(request, response, HttpStatusCode.ServiceUnavailable).ConfigureAwait(false);
            }
        }
Esempio n. 11
0
 public override async Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
 {
     RemoveUTF8BOM(buffer, ref offset, ref count);
     if (count == 0)
     {
         return;
     }
     await _deflate.WriteAsync(buffer, offset, count, cancellationToken).ConfigureAwait(false);
 }
        public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancel)
        {
            RemoveUTF8BOM(buffer, ref offset, ref count);
            if (count == 0)
            {
                return(Task.FromResult <object>(null));
            }

            return(_deflate.WriteAsync(buffer, offset, count, cancel));
        }
Esempio n. 13
0
 public static async Task <byte[]> CompressDeflatAsync(byte[] data)
 {
     using (MemoryStream output = new MemoryStream())
     {
         using (DeflateStream dstream = new DeflateStream(output, CompressionLevel.Optimal))
         {
             await dstream.WriteAsync(data, 0, data.Length);
         }
         return(output.ToArray());
     }
 }
Esempio n. 14
0
        async public static Task <byte[]> CompressAsync(byte[] data)
        {
            MemoryStream outputStream = new MemoryStream();

            using (DeflateStream deflateStream = new DeflateStream(outputStream, CompressionMode.Compress))
            {
                await deflateStream.WriteAsync(data, 0, data.Length);
            }

            return(outputStream.ToArray());
        }
Esempio n. 15
0
        internal async Task Send()
        {
            var compressionType = ParseAcceptEncoding(this.HttpRequest.Headers.Get(AcceptEncoding));

            if (this.responseBody.Length < this.request.Server.MinimumResponseSizeToCompress)
            {
                compressionType = ResponseCompressionType.None;
            }

            Stream       compressorStream = null;
            MemoryStream originalStream   = this.responseBody;

            switch (compressionType)
            {
            case ResponseCompressionType.GZip:
                this.responseBody = this.request.GetStream();
                this.HttpResponse.AddHeader(ContentEncoding, "gzip");
                compressorStream = new GZipStream(this.responseBody, CompressionMode.Compress, true);
                break;

            case ResponseCompressionType.Deflate:
                this.responseBody = this.request.GetStream();
                this.HttpResponse.AddHeader(ContentEncoding, "deflate");
                compressorStream = new DeflateStream(this.responseBody, CompressionMode.Compress, true);
                break;
            }

            if (compressorStream != null)
            {
                this.HttpResponse.AddHeader("UncompressedLength",
                                            originalStream.Length.ToString(CultureInfo.InvariantCulture));

                var uncompressedBytes = originalStream.GetBuffer();
                using (compressorStream)
                {
                    Events.Write.BeginCompressingResponse(uncompressedBytes.Length);
                    await compressorStream.WriteAsync(uncompressedBytes, 0, uncompressedBytes.Length);

                    Events.Write.CompleteCompressingResponse(this.responseBody.Length);
                }
                originalStream.Dispose();
            }

            try
            {
                this.responseBody.Position = 0;
                await this.responseBody.CopyToAsync(this.HttpResponse.OutputStream);
            }
            finally
            {
                this.responseBody.Dispose();
            }
        }
        public async Task <byte[]> Compress(byte[] responseBody)
        {
            using (var ms = new MemoryStream())
            {
                using (var zip = new DeflateStream(ms, CompressionMode.Compress, true))
                {
                    await zip.WriteAsync(responseBody, 0, responseBody.Length).ConfigureAwait(false);
                }

                return(ms.ToArray());
            }
        }
Esempio n. 17
0
        public async Task <byte[]> CompressAsync(ReadOnlyMemory <byte> data)
        {
            using var compressedStream = new MemoryStream();

            using (var deflateStream = new DeflateStream(compressedStream, CompressionMode.Compress))
            {
                await deflateStream
                .WriteAsync(data)
                .ConfigureAwait(false);
            }

            return(compressedStream.ToArray());
        }
Esempio n. 18
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);
            }
        }
Esempio n. 19
0
        private static async Task BaseResponse(this HttpListenerContext context, byte[] response, HttpStatusCode statusCode = HttpStatusCode.OK)
        {
            if ((context == null) || (response == null) || (response.Length == 0))
            {
                ASF.ArchiLogger.LogNullError(nameof(context) + " || " + nameof(response));
                return;
            }

            try {
                if (context.Response.StatusCode != (ushort)statusCode)
                {
                    context.Response.StatusCode = (ushort)statusCode;
                }

                context.Response.AppendHeader("Access-Control-Allow-Origin", "null");

                string acceptEncoding = context.Request.Headers["Accept-Encoding"];

                if (!string.IsNullOrEmpty(acceptEncoding))
                {
                    if (acceptEncoding.Contains("gzip"))
                    {
                        context.Response.AddHeader("Content-Encoding", "gzip");
                        using (MemoryStream ms = new MemoryStream()) {
                            using (GZipStream stream = new GZipStream(ms, CompressionMode.Compress)) {
                                await stream.WriteAsync(response, 0, response.Length).ConfigureAwait(false);
                            }

                            response = ms.ToArray();
                        }
                    }
                    else if (acceptEncoding.Contains("deflate"))
                    {
                        context.Response.AddHeader("Content-Encoding", "deflate");
                        using (MemoryStream ms = new MemoryStream()) {
                            using (DeflateStream stream = new DeflateStream(ms, CompressionMode.Compress)) {
                                await stream.WriteAsync(response, 0, response.Length).ConfigureAwait(false);
                            }

                            response = ms.ToArray();
                        }
                    }
                }

                context.Response.ContentLength64 = response.Length;
                await context.Response.OutputStream.WriteAsync(response, 0, response.Length).ConfigureAwait(false);
            } catch (Exception e) {
                ASF.ArchiLogger.LogGenericDebugException(e);
            }
        }
Esempio n. 20
0
        async public static Task <byte[]> EncryptWithCompressAsync(byte[] data, byte[] key, byte[] iv)
        {
            MemoryStream memoryStream = new MemoryStream();

            using (Aes algorithm = Aes.Create())
                using (ICryptoTransform encryptor = algorithm.CreateEncryptor(key, iv))
                    using (CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
                        using (DeflateStream deflateStream = new DeflateStream(cryptoStream, CompressionMode.Compress))
                        {
                            await deflateStream.WriteAsync(data, 0, data.Length);
                        }

            return(memoryStream.ToArray());
        }
Esempio n. 21
0
        private static async Task ResponseBase(HttpListenerRequest request, HttpListenerResponse response, byte[] content, HttpStatusCode statusCode = HttpStatusCode.OK)
        {
            if ((request == null) || (response == null) || (content == null) || (content.Length == 0))
            {
                ASF.ArchiLogger.LogNullError(nameof(request) + " || " + nameof(response) + " || " + nameof(content));
                return;
            }

            try {
                if (response.StatusCode != (ushort)statusCode)
                {
                    response.StatusCode = (ushort)statusCode;
                }

                response.AppendHeader("Access-Control-Allow-Origin", "*");

                string acceptEncoding = request.Headers["Accept-Encoding"];

                if (!string.IsNullOrEmpty(acceptEncoding))
                {
                    if (acceptEncoding.Contains("gzip"))
                    {
                        response.AddHeader("Content-Encoding", "gzip");
                        using (MemoryStream ms = new MemoryStream()) {
                            using (GZipStream stream = new GZipStream(ms, CompressionMode.Compress)) {
                                await stream.WriteAsync(content, 0, content.Length).ConfigureAwait(false);
                            }

                            content = ms.ToArray();
                        }
                    }
                    else if (acceptEncoding.Contains("deflate"))
                    {
                        response.AddHeader("Content-Encoding", "deflate");
                        using (MemoryStream ms = new MemoryStream()) {
                            using (DeflateStream stream = new DeflateStream(ms, CompressionMode.Compress)) {
                                await stream.WriteAsync(content, 0, content.Length).ConfigureAwait(false);
                            }

                            content = ms.ToArray();
                        }
                    }
                }

                response.ContentLength64 = content.Length;
                await response.OutputStream.WriteAsync(content, 0, content.Length).ConfigureAwait(false);
            } catch (ObjectDisposedException) {
                // Ignored, request is no longer valid
            }
        }
Esempio n. 22
0
        /// <summary>
        /// Compresses a byte[] with Deflate.
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static async Task <byte[]> CompressBytesWithDeflateAsync(byte[] input)
        {
            byte[] output = null;

            using (var memoryStream = new MemoryStream()) // Don't stack these usings, Gzip stream won't flush until dispose
            {
                using (var deflateStream = new DeflateStream(memoryStream, CompressionMode.Compress, false))
                { await deflateStream.WriteAsync(input, 0, input.Length); }

                output = memoryStream.ToArray();
            }

            return(output);
        }
        /// <summary>
        /// Retrieve a new <c>MemoryStream</c> object with the contents contained zipped data writen from the unzipped
        /// bytes in <c>ReadOnlyMemory&lt;byte&gt;</c>.
        /// </summary>
        /// <remarks>The new stream's position is set to the beginning of the stream when returned.</remarks>
        /// <param name="data"></param>
        /// <returns></returns>
        public async ValueTask <MemoryStream> CompressToStreamAsync(ReadOnlyMemory <byte> inputData)
        {
            Guard.AgainstEmpty(inputData, nameof(inputData));

            var compressedStream = RecyclableManager.GetStream(nameof(RecyclableDeflateProvider));

            using (var deflateStream = new DeflateStream(compressedStream, CompressionLevel, true))
            {
                await deflateStream
                .WriteAsync(inputData)
                .ConfigureAwait(false);
            }

            compressedStream.Seek(0, SeekOrigin.Begin);
            return(compressedStream);
        }
Esempio n. 24
0
        /// <summary>
        /// Retrieve a new <c>MemoryStream</c> object with the contents contained zipped data writen from the unzipped
        /// bytes in <c>ReadOnlyMemory&lt;byte&gt;</c>.
        /// </summary>
        /// <remarks>The new stream's position is set to the beginning of the stream when returned.</remarks>
        /// <param name="data"></param>
        /// <returns></returns>
        public async ValueTask <MemoryStream> CompressToStreamAsync(ReadOnlyMemory <byte> compressedData)
        {
            Guard.AgainstEmpty(compressedData, nameof(compressedData));

            var compressedStream = new MemoryStream();

            using (var deflateStream = new DeflateStream(compressedStream, CompressionLevel, true))
            {
                await deflateStream
                .WriteAsync(compressedData)
                .ConfigureAwait(false);
            }

            compressedStream.Seek(0, SeekOrigin.Begin);
            return(compressedStream);
        }
Esempio n. 25
0
        private static async Task <byte[]> CompressBinaryWithDeflateAsync(byte[] message)
        {
            byte[] compressedMessagePayload = null;

            using (MemoryStream compressedMessagePayloadStream = new MemoryStream())
            {
                using (DeflateStream compressedMessagePayloadCompressStream = new DeflateStream(compressedMessagePayloadStream, CompressionMode.Compress))
                {
                    await compressedMessagePayloadCompressStream.WriteAsync(message, 0, message.Length);
                }

                compressedMessagePayload = compressedMessagePayloadStream.ToArray();
            }

            return(compressedMessagePayload);
        }
        public override async Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException(nameof(buffer));
            }
            if (offset < 0 || offset > buffer.Length)
            {
                throw new ArgumentOutOfRangeException(nameof(offset));
            }
            if (count < 0 || offset + count > buffer.Length)
            {
                throw new ArgumentOutOfRangeException(nameof(count));
            }

            if (count == 0)
            {
                return;
            }
            await _deflate.WriteAsync(buffer, offset, count, cancellationToken).ConfigureAwait(false);
        }
Esempio n. 27
0
        public async ValueTask <ArraySegment <byte> > CompressAsync(ReadOnlyMemory <byte> inputData)
        {
            Guard.AgainstEmpty(inputData, nameof(inputData));

            using var compressedStream = new MemoryStream();
            using (var deflateStream = new DeflateStream(compressedStream, CompressionLevel, false))
            {
                await deflateStream
                .WriteAsync(inputData)
                .ConfigureAwait(false);
            }

            if (compressedStream.TryGetBuffer(out var buffer))
            {
                return(buffer);
            }
            else
            {
                return(compressedStream.ToArray());
            }
        }
Esempio n. 28
0
        public static Task <byte[]> WriteDeflateAsync(byte[] data)
        {
            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }

            var dataStream        = new MemoryStream();
            var compressionStream = new DeflateStream(dataStream, CompressionMode.Compress);

            return(compressionStream.WriteAsync(data, 0, data.Length)
                   .ContinueWith(task =>
            {
                using (dataStream)
                {
                    //Must manually dispose before returning
                    //https://github.com/dotnet/runtime/issues/15371
                    compressionStream.Dispose();
                    return dataStream.ToArray();
                }
            }));
        }
Esempio n. 29
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);
     }
 }
Esempio n. 30
0
        public void ReadWriteArgumentValidation()
        {
            using (var ds = new DeflateStream(new MemoryStream(), CompressionMode.Compress))
            {
                Assert.Throws<ArgumentNullException>(() => ds.Write(null, 0, 0));
                Assert.Throws<ArgumentOutOfRangeException>(() => ds.Write(new byte[1], -1, 0));
                Assert.Throws<ArgumentOutOfRangeException>(() => ds.Write(new byte[1], 0, -1));
                Assert.Throws<ArgumentException>(() => ds.Write(new byte[1], 0, 2));
                Assert.Throws<ArgumentException>(() => ds.Write(new byte[1], 1, 1));
                Assert.Throws<InvalidOperationException>(() => ds.Read(new byte[1], 0, 1));
                ds.Write(new byte[1], 0, 0);
            }
            using (var ds = new DeflateStream(new MemoryStream(), CompressionMode.Compress))
            {
                Assert.Throws<ArgumentNullException>(() => { ds.WriteAsync(null, 0, 0); });
                Assert.Throws<ArgumentOutOfRangeException>(() => { ds.WriteAsync(new byte[1], -1, 0); });
                Assert.Throws<ArgumentOutOfRangeException>(() => { ds.WriteAsync(new byte[1], 0, -1); });
                Assert.Throws<ArgumentException>(() => { ds.WriteAsync(new byte[1], 0, 2); });
                Assert.Throws<ArgumentException>(() => { ds.WriteAsync(new byte[1], 1, 1); });
                Assert.Throws<InvalidOperationException>(() => { ds.Read(new byte[1], 0, 1); });
            }

            using (var ds = new DeflateStream(new MemoryStream(), CompressionMode.Decompress))
            {
                Assert.Throws<ArgumentNullException>(() => ds.Read(null, 0, 0));
                Assert.Throws<ArgumentOutOfRangeException>(() => ds.Read(new byte[1], -1, 0));
                Assert.Throws<ArgumentOutOfRangeException>(() => ds.Read(new byte[1], 0, -1));
                Assert.Throws<ArgumentException>(() => ds.Read(new byte[1], 0, 2));
                Assert.Throws<ArgumentException>(() => ds.Read(new byte[1], 1, 1));
                Assert.Throws<InvalidOperationException>(() => ds.Write(new byte[1], 0, 1));

                var data = new byte[1] { 42 };
                Assert.Equal(0, ds.Read(data, 0, 0));
                Assert.Equal(42, data[0]);
            }
            using (var ds = new DeflateStream(new MemoryStream(), CompressionMode.Decompress))
            {
                Assert.Throws<ArgumentNullException>(() => { ds.ReadAsync(null, 0, 0); });
                Assert.Throws<ArgumentOutOfRangeException>(() => { ds.ReadAsync(new byte[1], -1, 0); });
                Assert.Throws<ArgumentOutOfRangeException>(() => { ds.ReadAsync(new byte[1], 0, -1); });
                Assert.Throws<ArgumentException>(() => { ds.ReadAsync(new byte[1], 0, 2); });
                Assert.Throws<ArgumentException>(() => { ds.ReadAsync(new byte[1], 1, 1); });
                Assert.Throws<InvalidOperationException>(() => { ds.Write(new byte[1], 0, 1); });
            }
        }
Esempio n. 31
0
 public override Task WriteAsync(DeflateStream unzip, byte[] buffer, int offset, int count) => unzip.WriteAsync(buffer, offset, count);
        private static async Task CompressFileAsL33TZipAsync(
            Stream inputStream,
            Stream outputStream,
            IProgress <double> progress = null,
            CancellationToken ct        = default)
        {
            //
            var fileLength = inputStream.Length;

            //Write L33T Header
            using (var writer = new AsyncBinaryWriter(inputStream))
            {
                //Write L33T Header & File Length
                if (fileLength > int.MaxValue)
                {
                    await writer.WriteAsync(L66THeader.ToCharArray(), ct);

                    await writer.WriteAsync(fileLength, ct);
                }
                else
                {
                    await writer.WriteAsync(L33THeader.ToCharArray(), ct);

                    await writer.WriteAsync(Convert.ToInt32(fileLength), ct);
                }

                //Write Deflate specification (2 Byte)
                await writer.WriteAsync(new byte[] { 0x78, 0x9C }, ct);
            }

            //Write Content
            var buffer         = new byte[BufferSize];
            var totalBytesRead = 0L;
            var lastProgress   = 0d;
            int bytesRead;

            using var compressedStream = new DeflateStream(outputStream, CompressionLevel.Optimal);
            while ((bytesRead = await inputStream.ReadAsync(buffer, 0, buffer.Length, ct)) > 0)
            {
                ct.ThrowIfCancellationRequested();

                if (bytesRead > fileLength)
                {
                    totalBytesRead += fileLength;
                    await compressedStream.WriteAsync(buffer, 0, Convert.ToInt32(fileLength), ct);
                }
                else if (totalBytesRead + bytesRead <= fileLength)
                {
                    totalBytesRead += bytesRead;
                    await compressedStream.WriteAsync(buffer, 0, bytesRead, ct);
                }
                else if (totalBytesRead + bytesRead > fileLength)
                {
                    var leftToRead = fileLength - totalBytesRead;
                    totalBytesRead += leftToRead;
                    await compressedStream.WriteAsync(buffer, 0, Convert.ToInt32(leftToRead), ct);
                }

                var newProgress = (double)totalBytesRead / fileLength * 100;

                if (newProgress - lastProgress > 1)
                {
                    progress?.Report(newProgress);
                    lastProgress = newProgress;
                }

                if (totalBytesRead >= fileLength)
                {
                    break;
                }
            }
        }
Esempio n. 33
0
 public override Task WriteAsync(DeflateStream unzip, byte[] buffer, int offset, int count) => unzip.WriteAsync(buffer, offset, count);
Esempio n. 34
0
        static async void Function2()
        {
            //ADAPTERY TEKSTOWE
            //using (FileStream fs = File.Create("test.txt"))
            //{
            //    using (TextWriter writer = new StreamWriter(fs))
            //    //using (TextWriter writer = new StreamWriter(fs, Encoding.Unicode))
            //    {
            //        writer.WriteLine("wiersz1");
            //        writer.WriteLine("wiersz2");
            //    }
            //}
            //using (FileStream fs = File.OpenRead("test.txt"))
            //{
            //    using (TextReader reader = new StreamReader(fs))
            //    {
            //        Console.WriteLine(reader.ReadLine());
            //        Console.WriteLine(reader.ReadLine());
            //    }
            //}
            //to samo nizej bo klasa File oferuje statyczne CreateText,AppendText i OpenText w celu skrócenai


            //using (TextWriter writer = File.CreateText("text.txt"))
            //{
            //    writer.WriteLine("wiersz1");
            //    writer.WriteLine("wiersz2");
            //}
            //using (TextWriter writer = File.AppendText("text.txt"))
            //    writer.WriteLine("wiersz3");
            //using (TextReader reader = File.OpenText("text.txt"))
            //    while (reader.Peek() > -1)
            //    {
            //        Console.WriteLine(reader.ReadLine());
            //    }


            //KOMPRESJA STRUMIENIA
            //string[] words = "The quixk brown fox jumps over the lazy dog".Split();
            //Random rand = new Random();
            //using (Stream s = File.Create("compressed.bin"))
            //using (Stream ds = new DeflateStream(s, CompressionMode.Compress))
            //using (TextWriter w = new StreamWriter(ds))
            //{
            //    for (int i = 0; i < 1000; i++)
            //        await w.WriteAsync(words[rand.Next(words.Length)] + " ");
            //}
            //Console.WriteLine(new FileInfo("compressed.bin").Length);

            //using (Stream s = File.Create(("compressed.bin")))
            //using (Stream ds = new DeflateStream(s, CompressionMode.Decompress))
            //using (TextReader r = new StreamReader(ds))
            //    Console.Write(await r.ReadToEndAsync());

            //Kompresja w pamięci
            byte[]       data = new byte[1000];
            MemoryStream ms   = new MemoryStream();

            using (Stream ds = new DeflateStream(ms, CompressionMode.Compress, true))
                await ds.WriteAsync(data, 0, data.Length);
            Console.WriteLine(ms.Length);
            ms.Position = 0;
            using (Stream ds = new DeflateStream(ms, CompressionMode.Decompress))
            {
                for (int i = 0; i < 1000; i += await ds.ReadAsync(data, i, 1000 - i))
                {
                }
            }

            //praca z plikami w postacci archiwum zipstr. 637
            //dodać referencje do projektu ZipFile
            //ZipFile.CreateFromDirectory(@"D:\Dokumenty\Visual Studio 2015\Projects\c_sharp_book\Chapter15",
            //    @"D:\Dokumenty\Visual Studio 2015\Projects\c_sharp_book\skompresowany15.zip");
            //dekompresja
            //ZipFile.ExtractToDirectory(@"D:\Dokumenty\Visual Studio 2015\Projects\c_sharp_book\skompresowany15.zip", @"D:\Dokumenty\Visual Studio 2015\Projects\c_sharp_book\Chapter15");

            using (ZipArchive zip =
                       ZipFile.Open(@"D:\Dokumenty\Visual Studio 2015\Projects\c_sharp_book\skompresowany15.zip",
                                    ZipArchiveMode.Read))
            {
                foreach (var entry in zip.Entries)
                {
                    Console.WriteLine(entry.FullName + " " + entry.Length);
                }
            }

            byte[] data2 =
                File.ReadAllBytes(@"D:\Dokumenty\Visual Studio 2015\Projects\c_sharp_book\README.md");
            using (ZipArchive zip =
                       ZipFile.Open(@"D:\Dokumenty\Visual Studio 2015\Projects\c_sharp_book\skompresowany15.zip",
                                    ZipArchiveMode.Update))
            {
                zip.CreateEntry(@"Readme.md").Open().Write(data2, 0, data2.Length);
            }

            var letters = new string[] { "A", "B", "C", "D" };
            var numbers = new int[] { 1, 2, 3 };
            var q       = letters.Zip(numbers, (l, n) => l + n.ToString());

            foreach (var s in q)
            {
                Console.WriteLine(s);
            }
        }