protected override void Write(IFileWriter dataWriter, DataChunk chunk) { var prefixSize = StructSerializer.GetSize <GzipChunkPrefix>(); using (MemoryStream memoryStream = new MemoryStream(prefixSize + chunk.Data.Length)) { using (GZipStream compressionStream = new GZipStream(memoryStream, CompressionMode.Compress, leaveOpen: true)) { memoryStream.Position = prefixSize; compressionStream.Write(chunk.Data, 0, chunk.Data.Length); } var prefix = new GzipChunkPrefix() { ChunkSize = chunk.Data.Length, Offset = chunk.Offset, GzipDataSize = (int)memoryStream.Length - prefixSize }; memoryStream.Position = 0; var prefixBytes = StructSerializer.Serialize(prefix); memoryStream.Write(prefixBytes, 0, prefixBytes.Length); memoryStream.Capacity = (int)memoryStream.Length; var compressedData = memoryStream.GetBuffer(); var offset = Interlocked.Add(ref _writeOffset, compressedData.Length) - compressedData.Length; dataWriter.WriteChunk(offset, compressedData); } }
protected override DataChunk Read(IFileReader dataReader) { long offset; GzipChunkPrefix prefix; DataChunk dataChunk = null; lock (_locker) { if (_readOffset >= dataReader.Size) { return(null); } var prefixSize = StructSerializer.GetSize <GzipChunkPrefix>(); var prefixBytes = dataReader.ReadChunk(_readOffset, prefixSize); prefix = StructSerializer.Deserialize <GzipChunkPrefix>(prefixBytes); offset = _readOffset + prefixSize; _readOffset = offset + prefix.GzipDataSize; } var decompressedData = new byte[prefix.ChunkSize]; var data = dataReader.ReadChunk(offset, prefix.GzipDataSize); using (MemoryStream memoryStream = new MemoryStream(data)) { using (GZipStream decompressionStream = new GZipStream(memoryStream, CompressionMode.Decompress, leaveOpen: true)) { var decompressedDataSize = decompressionStream.Read(decompressedData, 0, prefix.ChunkSize); if (decompressedDataSize != prefix.ChunkSize) { Console.WriteLine("The specified compressed file is malformed."); } } dataChunk = new DataChunk(prefix.Offset, decompressedData); } return(dataChunk); }