Exemplo n.º 1
0
        /// <summary>Flushes current chunk.</summary>
        private void FlushCurrentChunk()
        {
            if (_bufferOffset <= 0)
            {
                return;
            }

            var compressed       = new byte[_bufferOffset];
            var compressedLength = _highCompression
                                ? LZ4Codec.EncodeHC(_buffer, 0, _bufferOffset, compressed, 0, _bufferOffset)
                                : LZ4Codec.Encode(_buffer, 0, _bufferOffset, compressed, 0, _bufferOffset);

            if (compressedLength <= 0 || compressedLength >= _bufferOffset)
            {
                // incompressible block
                compressed       = _buffer;
                compressedLength = _bufferOffset;
            }

            var isCompressed = compressedLength < _bufferOffset;

            if (fileHeaderInfo == null)
            {
                fileHeaderInfo = new LZ4FileHeaderInfo();
                fileHeaderInfo.FrameDescriptor_BD_BlockMaxSize     = BlockMaximumSize.Block64K;
                fileHeaderInfo.FrameDescriptor_FLG_BChecksum       = false;
                fileHeaderInfo.FrameDescriptor_FLG_BIndependence   = true;
                fileHeaderInfo.FrameDescriptor_FLG_ContentChecksum = true;
                fileHeaderInfo.FrameDescriptor_FLG_ContentSize     = false;
                fileHeaderInfo.FrameDescriptor_FLG_Version         = 64;


                //U32 const xxh = XXH32(header, length, 0);
                //return (BYTE)(xxh >> 8);
                //    *dstPtr = LZ4F_headerChecksum(headerStart, dstPtr - headerStart);
                LZ4FileHeaderInfo.WriteHeader(_innerStream, fileHeaderInfo);
            }
            LZ4HeaderChunkInfo chunkInfo = new LZ4HeaderChunkInfo();

            chunkInfo.IsCompressed = true;
            chunkInfo.ChunkSize    = compressedLength;
            LZ4HeaderChunkInfo.WriteHeader(_innerStream, chunkInfo);

            _innerStream.Write(compressed, 0, compressedLength);

            _bufferOffset = 0;
        }
Exemplo n.º 2
0
        private async Task FlushCurrentChunkAsync()
        {
            if (_bufferOffset <= 0)
            {
                return;
            }

            var compressed       = new byte[_bufferOffset];
            var compressedLength = _highCompression
                ? LZ4Codec.EncodeHC(_buffer, 0, _bufferOffset, compressed, 0, _bufferOffset)
                : LZ4Codec.Encode(_buffer, 0, _bufferOffset, compressed, 0, _bufferOffset);

            if (compressedLength <= 0 || compressedLength >= _bufferOffset)
            {
                // incompressible block
                compressed       = _buffer;
                compressedLength = _bufferOffset;
            }

            var isCompressed = compressedLength < _bufferOffset;

            var flags = ChunkFlags.None;

            if (isCompressed)
            {
                flags |= ChunkFlags.Compressed;
            }
            if (_highCompression)
            {
                flags |= ChunkFlags.HighCompression;
            }

            await WriteVarIntAsync((ulong)flags).ConfigureAwait(false);
            await WriteVarIntAsync((ulong)_bufferOffset).ConfigureAwait(false);

            if (isCompressed)
            {
                await WriteVarIntAsync((ulong)compressedLength).ConfigureAwait(false);
            }

            await _innerStream.WriteAsync(compressed, 0, compressedLength).ConfigureAwait(false);

            _bufferOffset = 0;
        }
Exemplo n.º 3
0
        /// <summary>Flushes current chunk.</summary>
        private void FlushCurrentChunk()
        {
            if (_bufferOffset <= 0)
            {
                return;
            }

            var compressed       = new byte[_bufferOffset];
            var compressedLength = _highCompression
                                ? LZ4Codec.EncodeHC(_buffer, 0, _bufferOffset, compressed, 0, _bufferOffset)
                                : LZ4Codec.Encode(_buffer, 0, _bufferOffset, compressed, 0, _bufferOffset);

            if (compressedLength <= 0 || compressedLength >= _bufferOffset)
            {
                // incompressible block
                compressed       = _buffer;
                compressedLength = _bufferOffset;
            }

            var isCompressed = compressedLength < _bufferOffset;

            var flags = ChunkFlags.None;

            if (isCompressed)
            {
                flags |= ChunkFlags.Compressed;
            }
            if (_highCompression)
            {
                flags |= ChunkFlags.HighCompression;
            }

            WriteVarInt((ulong)flags);
            WriteVarInt((ulong)_bufferOffset);
            if (isCompressed)
            {
                WriteVarInt((ulong)compressedLength);
            }

            _innerStream.Write(compressed, 0, compressedLength);

            _bufferOffset = 0;
        }