Ejemplo 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(dataBuffer, 0, bufferOffset, compressed, 0, bufferOffset)
                                : LZ4Codec.Encode(dataBuffer, 0, bufferOffset, compressed, 0, bufferOffset);

            if (compressedLength <= 0 || compressedLength >= bufferOffset)
            {
                // uncompressible block
                compressed       = dataBuffer;
                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);
            innerStreamPosition += compressedLength;

            bufferOffset = 0;
        }