public static void Compress(Stream input, Stream output, BlockCompressionBuffer buffer) { if (!input.CanRead) { throw new ArgumentException("stream must be readable", nameof(input)); } if (!output.CanWrite) { throw new ArgumentException("stream must be writable", nameof(output)); } var inputBlock = buffer.Input; var outputBlock = buffer.Output; var blockSize = buffer.BlockSize; var w = new BinaryWriter(output); for (; ;) { int read = input.Read(inputBlock, 0, blockSize); if (read == 0) { break; } int compressedSize = CompressBlock(inputBlock, outputBlock, read); // todo: if compressedSize < blockSize-(2+13) else no compression w.Write((ushort)compressedSize); w.Write(outputBlock, 0, compressedSize); if (read < blockSize) { break; } } }
private static void Text() { var text = File.ReadAllText(@"..\..\text.txt"); //var text2 = File.ReadAllText(@"rand.txt"); var blockSizes = new int[] { 1 * 1024, 4 * 1024, 8 * 1024, 16 * 1024 }; Console.Write("N;uncompressed"); foreach (var blockSize in blockSizes) { Console.Write($";compressed{blockSize}"); } foreach (var blockSize in blockSizes) { Console.Write($";ratio{blockSize}"); } foreach (var blockSize in blockSizes) { Console.Write($";throughput{blockSize}"); } Console.WriteLine(); for (int i = 1; i <= 16; i++) { var bytes = _encoding.GetBytes(text.Substring(0, i * 1024)); var output = new MemoryStream(); //var deflate = new DeflateStream(buffer, CompressionLevel.Optimal, true); //var textWriter = new StreamWriter(deflate, _encoding); //textWriter.Write(text); //textWriter.Flush(); //deflate.Dispose(); var cols = new List <Tuple <int, int, double, double> >(); foreach (var blockSize in blockSizes) { var blockBuffer = new BlockCompressionBuffer(blockSize); var input = new MemoryStream(bytes, false); var sw = new Stopwatch(); int n; for (n = 0; n < 1000; n++) { // reset input.Position = 0; output.Position = 0; output.SetLength(0); sw.Start(); BlockCompression.Compress(input, output, blockBuffer); sw.Stop(); } var t = bytes.Length / sw.Elapsed.TotalSeconds; // Kbytes / s var uncompressedSize = bytes.Length; var compressedSize = (int)output.Length; var ratio = compressedSize / (double)uncompressedSize; cols.Add(Tuple.Create(uncompressedSize, compressedSize, ratio, sw.Elapsed.TotalSeconds)); // actually milliseconds } Console.Write($"{i};{cols[0].Item1}"); foreach (var col in cols) { Console.Write($";{col.Item2}"); } foreach (var col in cols) { Console.Write($";{col.Item3:0.00}"); } foreach (var col in cols) { Console.Write($";{col.Item4:0.00}"); } Console.WriteLine(); } }