//closes the current block private void closeBlock() { //close compression stream compressionStream.Close(); //is this block empty? Then remove the new block header if (this.decompressedByteCountWithinBlock == 0) { this.fileStream.SetLength(this.fileStream.Length - 16); return; } //close block by filling the 8 byte block header (compressed block size) //go back 8 bytes this.fileStream.Seek(-8, SeekOrigin.Current); //write "compressed block size" byte[] buffer = BitConverter.GetBytes(this.mStream.Length); this.fileStream.Write(buffer, 0, 8); //go back to file head this.fileStream.Seek(0, SeekOrigin.End); //write memory stream to file this.mStream.Position = 0; this.mStream.CopyTo(this.fileStream); }
// Called by the task when it is done with this writer, launches the async write internal void Flush() { OutputSize += _bufferPos; _writeTask = Task <uint> .Factory.StartNew(() => { if (Compress) { _compressor.Write(_memBuffer, 0, (int)_bufferPos); _compressor.Flush(); _compressor.Close(); _file.Flush(); } else { _file.Write(_memBuffer, 0, (int)_bufferPos); _file.Flush(); } var size = (uint)_file.Position; _compressor?.Dispose(); _file?.Dispose(); return(Compress ? size : 0); }); }
/// <inheritdoc /> public byte[] Compress(byte[] source) { var output = new MemoryStream(); using (LZ4EncoderStream stream = LZ4Stream.Encode(output)) { var reader = new MemoryStream(source); reader.CopyTo(stream); stream.Close(); return(output.ToArray()); } }