ReadAsync() public method

public ReadAsync ( byte array, int offset, int count, System cancellationToken ) : System.Threading.Tasks.Task
array byte
offset int
count int
cancellationToken System
return System.Threading.Tasks.Task
Exemplo n.º 1
1
        /// <summary>  
        /// decompress gzip data
        /// </summary>  
        /// <param name="data">compressed data</param>  
        /// <returns></returns>  
        public async static Task<byte[]> Decompress(byte[] data)
        {
            MemoryStream ms = null;
            GZipStream compressedzipStream = null;
            MemoryStream outBuffer = new MemoryStream();

            try
            {
                ms = new MemoryStream(data);
                compressedzipStream = new GZipStream(ms, CompressionMode.Decompress);

                byte[] block = new byte[1024 * 16];
                while (true)
                {
                    int bytesRead = await compressedzipStream.ReadAsync(block, 0, block.Length);
                    if (bytesRead <= 0)
                    {
                        break;
                    }
                    else
                    {
                        outBuffer.Write(block, 0, bytesRead);
                    }
                }
            }
            finally
            {
                if (null != compressedzipStream) compressedzipStream.Close();
                if (null != ms) ms.Close();
            }

            return outBuffer.ToArray();
        }
Exemplo n.º 2
0
		public async Task LoadAsync(Stream input, ZipHeader header, CancellationToken cancellationToken)
		{
			if (input == null) throw new ArgumentNullException(nameof(input));
			if (header == null) throw new ArgumentNullException(nameof(header));

			// Clear the files
			Files.Clear();

			var buffer = new byte[64 * 1024];

			foreach (var file in header.Files)
			{
				var fileSize = file.Item2;
				using (var output = new MemoryStream(fileSize))
				{
					int readBytes;
					while ((readBytes = await input.ReadAsync(buffer, 0, Math.Min(fileSize, buffer.Length), cancellationToken)) != 0)
					{
						await output.WriteAsync(buffer, 0, readBytes, cancellationToken);
						fileSize -= readBytes;
					}

					output.Position = 0;

					using (var data = new MemoryStream(fileSize))
					{
						using (var zip = new GZipStream(output, CompressionMode.Decompress, true))
						{
							while ((readBytes = await zip.ReadAsync(buffer, 0, buffer.Length, cancellationToken)) != 0)
							{
								await data.WriteAsync(buffer, 0, readBytes, cancellationToken);
							}
							Files.Add(data.ToArray());
						}
					}
				}
			}
		}
Exemplo n.º 3
0
        // Making this async since regular read/write are tested below
        private static async Task DecompressAsync(MemoryStream compareStream, MemoryStream gzStream)
        {
            var ms = new MemoryStream();
            var zip = new GZipStream(gzStream, CompressionMode.Decompress);

            var GZipStream = new MemoryStream();

            int _bufferSize = 1024;
            var bytes = new Byte[_bufferSize];
            bool finished = false;
            int retCount;
            while (!finished)
            {
                retCount = await zip.ReadAsync(bytes, 0, _bufferSize);

                if (retCount != 0)
                    await GZipStream.WriteAsync(bytes, 0, retCount);
                else
                    finished = true;
            }

            GZipStream.Position = 0;
            compareStream.Position = 0;

            byte[] compareArray = compareStream.ToArray();
            byte[] writtenArray = GZipStream.ToArray();

            Assert.Equal(compareArray.Length, writtenArray.Length);
            for (int i = 0; i < compareArray.Length; i++)
            {
                Assert.Equal(compareArray[i], writtenArray[i]);
            }
        }
Exemplo n.º 4
0
        async Task<MemoryStream> Decompress(FilterDownloadResult filter, CancellationToken cancellationToken, IProgress<ProgressModel> progress)
        {
            using (var stream = filter.Stream)
            {
                var result = new MemoryStream();

                stream.Seek(0, SeekOrigin.Begin);

                cancellationToken.ThrowIfCancellationRequested();

                switch (filter.CompressionFormat)
                {
                    case CompressionFormat.GZip:
                        using(var gzipFile = new GZipStream(stream, CompressionMode.Decompress))
                        {
                            var buffer = new byte[1024 * 64];
                            int bytesRead;
                            while ((bytesRead = await gzipFile.ReadAsync(buffer, 0, buffer.Length, cancellationToken)) > 0)
                            {
                                cancellationToken.ThrowIfCancellationRequested();
                                result.Write(buffer, 0, bytesRead);
                                progress.Report(new ProgressModel(UpdateState.Decompressing, "Decompressing...", -1));
                            }
                        }
                        break;

                    case CompressionFormat.Zip:

                        using (var zipFile = new ZipArchive(stream,ZipArchiveMode.Read))
                        {
                            progress.Report(new ProgressModel(UpdateState.Decompressing, "Decompressing...", -1));
                            
                            if (zipFile.Entries.Count == 0) throw new IOException("There are no entries in the zip file.");
                            if (zipFile.Entries.Count > 1) throw new IOException("There is more than one file in the zip file. This application will need to be updated to support this.");

                            var entry = zipFile.Entries.First();

                            using (var entryStream = entry.Open())
                            {
                                cancellationToken.ThrowIfCancellationRequested();
                                await entryStream.CopyToAsync(result);
                            }
                        }
                        break;

                    default:
                        await stream.CopyToAsync(result);
                        break;
                }

                return result;
            }
        }
Exemplo n.º 5
0
        private static async Task<DecompressionDetails> Decompress(DecompressionDetails compressionDetails)
        {
            byte[] compressedData = new byte[compressionDetails.ChunkSize];

            // uncompressed the chunk
            using (MemoryStream uncompressedDataStream = new MemoryStream(compressionDetails.Bytes))
            using (GZipStream streamUncompressed = new GZipStream(uncompressedDataStream, CompressionMode.Decompress))
            {
                int index = 0;
                int count = compressedData.Length;
                while (index < compressedData.Length - 1)
                {
                    // read the chunk in the compressed stream
                    var bytesRead = await streamUncompressed.ReadAsync(compressedData, index, count);
                    index += bytesRead;
                    count -= bytesRead;
                }
            }

            compressionDetails = new DecompressionDetails
            {
                Bytes = compressedData,
                ChunkSize = compressionDetails.ChunkSize,
                Sequence = compressionDetails.Sequence,
                IsProcessed = compressionDetails.IsProcessed
            };
            return compressionDetails;
        }
Exemplo n.º 6
0
        /// <summary>
        /// Decompresses a byte array containing compressed data.
        /// </summary>
        /// <param name="value">
        /// The byte array of compressed data.
        /// </param>
        /// <returns>
        /// A byte array containing decompressed data.
        /// </returns>
        public async Task<byte[]> DecompressAsync(byte[] value)
        {
            using (MemoryStream ms = new MemoryStream(value))
            {
                using (GZipStream gs = new GZipStream(ms, CompressionMode.Decompress))
                {
                    using (MemoryStream fin = new MemoryStream())
                    {
                        byte[] buffer = new byte[1024];
                        int numberRead;
                        while ((numberRead = await gs.ReadAsync(buffer, 0, buffer.Length)) > 0)
                        {
                            await fin.WriteAsync(buffer, 0, numberRead);
                        }

                        return fin.ToArray();
                    }
                }
            }
        }
Exemplo n.º 7
0
        async Task<MemoryStream> Decompress(FilterDownloadResult filter, CancellationToken cancellationToken, IProgress<ProgressModel> progress)
        {
            using (var stream = filter.Stream)
            {
                var result = new MemoryStream();

                stream.Seek(0, SeekOrigin.Begin);

                cancellationToken.ThrowIfCancellationRequested();

                switch (filter.CompressionFormat)
                {
                    case CompressionFormat.GZip:
                        using(var gzipFile = new GZipStream(stream, CompressionMode.Decompress))
                        {
                            var buffer = new byte[1024 * 64];
                            int bytesRead;
                            while ((bytesRead = await gzipFile.ReadAsync(buffer, 0, buffer.Length, cancellationToken)) > 0)
                            {
                                cancellationToken.ThrowIfCancellationRequested();
                                result.Write(buffer, 0, bytesRead);
                                progress.Report(new ProgressModel(UpdateState.Decompressing, "Decompressing...", -1));
                            }
                        }
                        break;

                    case CompressionFormat.Zip:

                        EventHandler<ReadProgressEventArgs> reportProgress = (sender, args) =>
                        {
                            cancellationToken.ThrowIfCancellationRequested();
                            if (args.EventType != ZipProgressEventType.Extracting_EntryBytesWritten) return;
                            var percentage = (int)Math.Floor( (double)args.BytesTransferred / args.TotalBytesToTransfer * 100);
                            progress.Report(new ProgressModel(UpdateState.Decompressing, "Unzipping...", percentage));
                        };

                        using (var zipFile = ZipFile.Read(stream, reportProgress))
                        {
                            if (zipFile.Entries.Count == 0) throw new ZipException("There are no entries in the zip file.");
                            if (zipFile.Entries.Count > 1) throw new ZipException("There is more than one file in the zip file. This application will need to be updated to support this.");

                            var entry = zipFile.Entries.First();

                            entry.Extract(result);
                        }
                        break;

                    default:
                        await stream.CopyToAsync(result);
                        break;
                }

                return result;
            }
        }