2using System.Diagnostics;
4using System.IO.Compression;
11 #region Private Properties
16 private GZipStream _compressor;
19 private readonly MemoryStream _gzipOutput;
23 private readonly CompressionLevel _compressionLevel;
29 #endregion Private Properties
31 #region Public Properties
52 public enum CompressionLevel
58 #endregion Public Properties
80 int? min_buffer_length =
null,
int? max_buffer_length =
null,
82 CompressionLevel compressionLevel = CompressionLevel.Fastest)
83 : base(min_buffer_length, max_buffer_length)
87 _compressionStrategy = compressionStrategy;
88 _compressionLevel = compressionLevel;
90 _compressor = GetCompressorForFramework();
95 private GZipStream GetCompressorForFramework()
98 return new GZipStream(_gzipOutput, CompressionMode.Compress,
true);
100 return new GZipStream(_gzipOutput, _compressionLevel,
true);
107 try { _gzipOutput.Dispose(); }
108 catch (ObjectDisposedException) { }
109 try { _compressor?.Dispose(); }
110 catch (ObjectDisposedException) { }
118 if (!_source.IsRunning)
133 if (!_source.CanTake)
138 _gzipOutput.Position = 0;
140 _compressor = GetCompressorForFramework();
150 Debug.WriteLine($
"GZip got {buffer.LengthFilled} bytes to compress.");
156 }
while (_source.CanTake && _gzipOutput.Position < buffer_size);
159 Debug.WriteLine(
"GZip got EOF from its source stream.");
163 _compressor.Dispose();
168 int compressed_length = (int)_gzipOutput.Position;
169 if (compressed_length == 0)
172 throw new EndOfStreamException(
"GZip compressor produced no compressed bytes " +
173 $
"despite {TotalBytesIn - previousTotalBytesIn} bytes being written to the compressor!");
178 next.Bytes =
new byte[compressed_length];
179 _gzipOutput.Position = 0;
180 next.LengthFilled += _gzipOutput.Read(next.Bytes, 0, compressed_length);
182 if (next.LengthFilled != compressed_length)
183 throw new EndOfStreamException(
"The GZip Output MemoryStream did not return all compressed data!");
186 Debug.WriteLine($
"GZip produced {next.LengthFilled} bytes of compressed " +
187 $
"data from {TotalBytesIn - previousTotalBytesIn} read bytes.");
Produces BufferedByte objects of a variable size depending on whether the consumer of this buffer is ...
int MaximumBufferLength
The max length of the each byte array to generate. (Default 32 MiB)
Converts buffered bytes to strings.
CompressionStrategy
Indicates whether the resulting chunks of compressed data should be pieces of a zip file,...
@ CompressEachChunk
Each output chunk will be its own zip file. Required by protocols that decompress each chunk as you u...
@ CompressEntireFile
All output chunks will be part of the same zip file. Required by protocols that only decompress the e...
override bool TryProduceNext(out BufferedBytes next, int buffer_size)
Compress the next BufferedBytes.
GZipBufferedBytesProducer(IProducerConsumerBuffer< BufferedBytes > source, int? min_buffer_length=null, int? max_buffer_length=null, CompressionStrategy compressionStrategy=CompressionStrategy.CompressEachChunk, CompressionLevel compressionLevel=CompressionLevel.Fastest)
Construct a producer of strings from buffered bytes.
override void OnStart()
Invoked before the producer thread is started.
int TotalBytesIn
Returns the total number of input bytes processed by this compressor.
int TotalBytesOut
The total number of output bytes produced after compression.
override void OnDispose()
Dispose actions.
override bool IsProducerFinished()
Indicates whether it can produce more items.
CancellationTokenSource Cancellation
Can be used to cancel the buffering process.
Large Data Upload Utilities.
static bool EnableDebugLogging
When in debug mode, allows detailed debug logging of the data upload process.
Interface for a class that consumes from a source asynchronously and produces a queue of some other r...
byte[] Bytes
The buffer of bytes filled.
int LengthFilled
The actual length of bytes, which may be shorter.
The structure containing an array of bytes and integer indicating the number of bytes in the array th...