Esempio n. 1
0
        /// <summary>If the stream is not disposed, and the compression mode is set to compress, writes all the remaining encoder's data into this stream.</summary>
        /// <exception cref="InvalidDataException">The encoder ran into invalid data.</exception>
        /// <exception cref="ObjectDisposedException">The stream is disposed.</exception>
        public override void Flush()
        {
            EnsureNotDisposed();
            if (_mode == CompressionMode.Compress)
            {
                if (_encoder._state == null || _encoder._state.IsClosed)
                {
                    return;
                }

                OperationStatus lastResult = OperationStatus.DestinationTooSmall;
                Span <byte>     output     = new Span <byte>(_buffer);
                while (lastResult == OperationStatus.DestinationTooSmall)
                {
                    int bytesWritten;
                    lastResult = _encoder.Flush(output, out bytesWritten);
                    if (lastResult == OperationStatus.InvalidData)
                    {
                        throw new InvalidDataException(SR.BrotliStream_Compress_InvalidData);
                    }
                    if (bytesWritten > 0)
                    {
                        _stream.Write(output.Slice(0, bytesWritten));
                    }
                }

                _stream.Flush();
            }
        }
Esempio n. 2
0
        public void RoundTrip_Chunks()
        {
            int           chunkSize = 100;
            int           totalSize = 20000;
            BrotliEncoder encoder   = default;
            BrotliDecoder decoder   = default;

            for (int i = 0; i < totalSize; i += chunkSize)
            {
                byte[] uncompressed = new byte[chunkSize];
                new Random().NextBytes(uncompressed);
                byte[] compressed       = new byte[BrotliEncoder.GetMaxCompressedLength(chunkSize)];
                byte[] decompressed     = new byte[chunkSize];
                var    uncompressedSpan = new ReadOnlySpan <byte>(uncompressed);
                var    compressedSpan   = new Span <byte>(compressed);
                var    decompressedSpan = new Span <byte>(decompressed);

                int totalWrittenThisIteration = 0;
                var compress = encoder.Compress(uncompressedSpan, compressedSpan, out int bytesConsumed, out int bytesWritten, isFinalBlock: false);
                totalWrittenThisIteration += bytesWritten;
                compress = encoder.Flush(compressedSpan.Slice(bytesWritten), out bytesWritten);
                totalWrittenThisIteration += bytesWritten;

                var res = decoder.Decompress(compressedSpan.Slice(0, totalWrittenThisIteration), decompressedSpan, out int decompressbytesConsumed, out int decompressbytesWritten);
                Assert.Equal(totalWrittenThisIteration, decompressbytesConsumed);
                Assert.Equal(bytesConsumed, decompressbytesWritten);
                Assert.Equal <byte>(uncompressed, decompressedSpan.ToArray());
            }
        }