Example #1
0
        static void Main(string[] args)
        {
            ICompressor pcx  = new PCXCompressor();
            ICompressor rle3 = new RLE3compressor();
            SuperSlowInternetConnection connection = new SuperSlowInternetConnection();

            // This is too expensive to do twice!!
            // ExpensiveData gameData1 = SuperSlowInternetConnection.DownloadBootlegGame();
            // ExpensiveData gameData2 = SuperSlowInternetConnection.DownloadBootlegGame();

            // Let's use a proxy instead.
            ExpensiveData originalGameData = connection.DownloadBootlegGame(1000000);
            ExpensiveData gameData1        = originalGameData.Clone();
            ExpensiveData gameData2        = originalGameData.Clone();


            // Which compression is more effective?
            ICompressResult pcxResult  = gameData1.Compress(pcx);
            ICompressResult rle3Result = gameData1.Compress(rle3);

            if (pcxResult.CompressedData.Size < rle3Result.CompressedData.Size)
            {
                Console.WriteLine($"PCX is smaller with a size of ({pcxResult.CompressedData.Size}) bytes.");
            }
            else
            {
                Console.WriteLine($"RLE3 is smaller with a size of ({pcxResult.CompressedData.Size}) bytes.");
            }
        }
Example #2
0
        public ICompressResult Compress(ExpensiveData data)
        {
            Random        rnd          = new Random((int)(DateTime.Now.Ticks & 0x0FFFFFFFF));
            var           originalData = data;
            int           minSize      = (int)(originalData.Size / 2) + 1;
            int           maxSize      = (int)(originalData.Size * 1.5) + 1;
            var           newDataSize  = rnd.Next(minSize, maxSize);
            ExpensiveData newData      = ExpensiveDataProducer.GetData(newDataSize);

            return(new CompressResult(newData));
        }
Example #3
0
        public static ExpensiveData GetData(int size)
        {
            Random        rnd = new Random((int)(DateTime.Now.Ticks & 0x0FFFFFFFF));
            StringBuilder sb  = new StringBuilder();

            for (int j = 0; j < size; j++)
            {
                byte ascii = (byte)(rnd.Next(65, 65 + 26 - 1) & 0x0FF);
                char c     = (char)ascii;
                sb.Append(c);
            }
            string        s      = sb.ToString();
            ExpensiveData result = new ExpensiveData(s);

            return(result);
        }
Example #4
0
        public CompressResult(ExpensiveData compressedData)
        {
            Random _rnd = new Random();

            CompressedData = compressedData;
        }
Example #5
0
        public ExpensiveData DownloadBootlegGame(int gameSize)
        {
            ExpensiveData randomData = ExpensiveDataProducer.GetData(gameSize);

            return(randomData);
        }