Exemple #1
0
        private static void AssertAlgorithm(ICompressionAlgorithm algorithm, string text)
        {
            Assert.AreEqual(text,
                            Encoding.UTF8.GetString(algorithm.Decompress(algorithm.Compress(Encoding.UTF8.GetBytes(text)))));

            using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(text)))
                using (var compressed = algorithm.Compress(stream))
                    using (var decompressed = algorithm.Decompress(compressed))
                        using (var decompressedStream = new MemoryStream())
                        {
                            decompressed.CopyTo(decompressedStream);

                            Assert.AreEqual(text, Encoding.UTF8.GetString(decompressedStream.ToArray()));
                        }
        }
Exemple #2
0
        public Message Compress(ICompressionAlgorithm packer)
        {
            var result = packer.Compress(Content);

            Size = 0;
            AddBytes(result);
            return(this);
        }
Exemple #3
0
        public static Stream Compress(this ICompressionAlgorithm algorithm, Stream stream)
        {
            Guard.AgainstNull(algorithm, nameof(algorithm));
            Guard.AgainstNull(stream, nameof(stream));

            using (var ms = new MemoryStream())
            {
                stream.CopyTo(ms);

                return(new MemoryStream(algorithm.Compress(ms.ToArray())));
            }
        }
Exemple #4
0
        public (int firstPosition, int lastPosition, int numBytes) Write(ExtendedBinaryWriter writer)
        {
            var compressedLength = _compressionAlgorithm.Compress(_uncompressedBlock, BlockOffset,
                                                                  _compressedBlock, _compressedBlock.Length);

            writer.WriteOpt(compressedLength);
            writer.WriteOpt(_firstPosition);
            //writer.WriteOpt(_lastPosition);
            writer.WriteOpt(_count);
            writer.Write(_compressedBlock, 0, compressedLength);

            _writer.BaseStream.Position = 0;

            return(_firstPosition, _lastPosition, compressedLength);
        }
Exemple #5
0
        internal void WriteInterval(PhylopInterval interval, ExtendedBinaryWriter writer)
        {
            if (_scoreCount == 0)
            {
                return;
            }
            ScoresToBytes(_scoreBytes, _scores, _scoreCount);

            var compressedSize = _compressor.Compress(_scoreBytes, _scoreCount * 2, _compressedBuffer, _compressedBuffer.Length);

            // finalizing the file position for this chromosome interval
            interval.FilePosition   = _writer.BaseStream.Position;
            _currentInterval.Length = _scoreCount;
            ChromosomeIntervals.Add(interval);

            _scoreCount = 0;

            WriteScores(writer, _compressedBuffer, compressedSize);
        }
Exemple #6
0
        /// <summary>
        /// Writes the current compression block to a stream.
        /// </summary>
        /// <param name="stream">The stream that will write the compressed data.</param>
        public void Write(Stream stream)
        {
            _header.NumUncompressedBytes = Offset;

            _header.NumCompressedBytes = _compressionAlgorithm.Compress(_uncompressedBlock, _header.NumUncompressedBytes,
                                                                        _compressedBlock, _compressedBlockSize);

            if (_header.NumCompressedBytes > _header.NumUncompressedBytes)
            {
                _header.NumCompressedBytes = -1;
                _header.Write(stream);
                stream.Write(_uncompressedBlock, 0, _header.NumUncompressedBytes);
            }
            else
            {
                _header.Write(stream);
                stream.Write(_compressedBlock, 0, _header.NumCompressedBytes);
            }

            Offset = 0;
        }
        public IMemoryOwner <byte>?Compress(ReadOnlyMemory <byte> input, IRequestSpan parentSpan)
        {
            if (!_clusterOptions.Compression || input.Length < _clusterOptions.CompressionMinSize)
            {
                LogSkipLength(_logger, input.Length, _clusterOptions.CompressionMinSize, null);
                return(null);
            }

            using var compressionSpan = parentSpan.CompressionSpan();

            var compressed = _compressionAlgorithm.Compress(input);

            try
            {
                var compressedMemory = compressed.Memory;
                var compressionRatio = compressedMemory.Length == 0
                    ? 1f
                    : (float)compressedMemory.Length / input.Length;

                if (compressionRatio > _clusterOptions.CompressionMinRatio)
                {
                    AddCompressionTags(compressionSpan, compressionRatio, false);

                    LogSkipCompressionRatio(_logger, compressionRatio, _clusterOptions.CompressionMinRatio, null);

                    // Insufficient compression, so drop it
                    compressed.Dispose();
                    return(null);
                }

                AddCompressionTags(compressionSpan, compressionRatio, true);

                return(compressed);
            }
            catch
            {
                compressed.Dispose();
                throw;
            }
        }
Exemple #8
0
        private async Task <bool> PerformArchiveOperations(ICompressionAlgorithm compressionAlgorithm, string fileType, WriterOptions options)
        {
            return(await Task.Run(async() =>
            {
                var tempFile = await _workingDir.CreateFileAsync("tempFile");

                using (var streamWriter = new StreamWriter(await tempFile.OpenStreamForWriteAsync()))
                {
                    streamWriter.AutoFlush = true;
                    streamWriter.WriteLine(FileText);
                }

                _files = await _workingDir.GetFilesAsync();
                Assert.IsNotNull(_files);
                Assert.AreEqual(_files.Count, 1); // check if file exists

                var archive = await _workingDir.CreateFileAsync(ArchiveName + fileType);
                Assert.IsTrue(await compressionAlgorithm.Compress(_files, archive, _workingDir, options));

                return await ArchiveExtraction(compressionAlgorithm, fileType); // extract archive after creation
            }));
        }
Exemple #9
0
        public IMemoryOwner <byte>?Compress(ReadOnlyMemory <byte> input)
        {
            if (!_clusterOptions.Compression || input.Length < _clusterOptions.CompressionMinSize)
            {
                _logger.LogTrace("Skipping operation compression because length {length} < {compressionMinSize}",
                                 input.Length, _clusterOptions.CompressionMinSize);
                return(null);
            }

            var compressed = _compressionAlgorithm.Compress(input);

            try
            {
                var compressedMemory = compressed.Memory;
                var compressionRatio = compressedMemory.Length == 0
                    ? 1f
                    : (float)compressedMemory.Length / input.Length;

                if (compressionRatio > _clusterOptions.CompressionMinRatio)
                {
                    _logger.LogTrace("Skipping operation compression because compressed size {compressionRatio} > {compressionMinRatio}",
                                     compressionRatio, _clusterOptions.CompressionMinRatio);

                    // Insufficient compression, so drop it
                    compressed.Dispose();
                    return(null);
                }

                return(compressed);
            }
            catch
            {
                compressed.Dispose();
                throw;
            }
        }
 public void Compress(string source, string destination)
 {
     strategy.Compress(source, destination);
 }