/// <summary> /// Writes compressed data to the given stream. /// </summary> /// <param name="uncompressedStream">A stream where the compressed data should be written.</param> /// <param name="level">The Zlib compression level that should be used. Default is Z_BEST_COMPRESSION = 9.</param> public ZlibCompressedWriter(Stream uncompressedStream, int level = 9) : base(uncompressedStream) { /* Since we need to write the number of compressed bytes that we are going to send first, * We cannot directly write to the uncompressedStream. * We first write the compressed data to zMemoryStream, and after that we write the data from it to the uncompressedStream * using CompressedWriter. */ zMemoryStream = new MemoryStream(); zCompressStream = new ZOutputStream(zMemoryStream, level) { //The VNC Protocol uses Z_SYNC_FLUSH as a Flush Mode FlushMode = zlibConst.Z_SYNC_FLUSH }; Level = level; compressedWriter = new BinaryWriter(uncompressedStream); bigWriter = new BigEndianBinaryWriter(uncompressedStream); }