public override void Reader(string source, ref SynchronizedQueue <Chunk> readerQueue) { try { using (BinaryReader br = new BinaryReader(new FileStream(source, FileMode.Open, FileAccess.Read, FileShare.None))) { _blockCount = br.ReadInt64(); for (int count = 0; count < _blockCount; count++) { int blockNumber = br.ReadInt32(); int blockLength = br.ReadInt32(); byte[] blockValue = br.ReadBytes(blockLength); if (blockValue == null) { throw new ArgumentNullException("Error read compressing file"); } Chunk readerChunk = new Chunk(blockNumber, blockValue); if (!readerQueue.TryEnqueue(readerChunk)) { return; } } } } catch (Exception e) { OnCancel(); OnError(); Console.WriteLine(e.Message); return; } }
public override void Writer(string sink, ref SynchronizedQueue <Chunk> writerQueue) { int counter = 0; int blockNumber = -1; byte[] blockValue = null; try { using (BinaryWriter bw = new BinaryWriter(new FileStream(sink, FileMode.Create, FileAccess.Write, FileShare.None))) { bw.Write(BitConverter.GetBytes(_blockCount)); while (true) { Chunk writerChunk = new Chunk(blockNumber, blockValue); if (!writerQueue.TryDequeue(out writerChunk)) { return; } if (writerChunk.Data == null) { break; } try { bw.Write(BitConverter.GetBytes(writerChunk.Id)); bw.Write(writerChunk.Data.Length); bw.Write(writerChunk.Data); } catch (IOException) { OnCancel(); OnError(); Console.WriteLine("Error write compressing file"); bw.Close(); File.Delete(sink); return; } counter++; if (counter == _blockCount) { OnCancel(); } } } } catch (Exception) { OnCancel(); OnError(); Console.WriteLine("Error write compressing file"); } }
public override void Writer(string sink, ref SynchronizedQueue <Chunk> writerQueue) { int cnt = 0; int blockNumber = -1; byte[] blockValue = null; Dictionary <int, byte[]> buffer = new Dictionary <int, byte[]>(); try { using (BinaryWriter bw = new BinaryWriter(new FileStream(sink, FileMode.Create, FileAccess.Write, FileShare.None))) { while (true) { Chunk writerChunk = new Chunk(blockNumber, blockValue); if (!writerQueue.TryDequeue(out writerChunk)) { return; } if (writerChunk.Data == null) { break; } buffer[writerChunk.Id] = writerChunk.Data; while (buffer.ContainsKey(cnt)) { bw.Write(buffer[cnt]); buffer.Remove(cnt); cnt++; if (cnt == _blockCount) { OnCancel(); return; } } } } } catch (Exception) { OnCancel(); OnError(); Console.WriteLine("Error write decompressing file"); } }
public override void Reader(string source, ref SynchronizedQueue <Chunk> readerQueue) { try { FileInfo fi = new FileInfo(source); _blockCount = fi.Length / GZip.BUFFER_SIZE; if (fi.Length % GZip.BUFFER_SIZE > 0) { _blockCount++; } } catch (Exception) { OnCancel(); OnError(); Console.WriteLine("Error read file"); return; } try { using (BinaryReader br = new BinaryReader(new FileStream(source, FileMode.Open, FileAccess.Read, FileShare.None))) { for (int blockNumber = 0; blockNumber < _blockCount; blockNumber++) { byte[] blockValue = br.ReadBytes(GZip.BUFFER_SIZE); if (blockValue == null) { throw new ArgumentNullException("Error read file"); } Chunk readerChunk = new Chunk(blockNumber, blockValue); if (!readerQueue.TryEnqueue(readerChunk)) { return; } } } } catch (Exception e) { OnCancel(); OnError(); Console.WriteLine(e.Message); return; } }
public override void Process(ref SynchronizedQueue <Chunk> readerQueue, ref SynchronizedQueue <Chunk> writerQueue) { int blockNumber = -1; byte[] blockValue = null; while (true) { try { Chunk readerChunk = new Chunk(blockNumber, blockValue); if (!readerQueue.TryDequeue(out readerChunk)) { return; } if (readerChunk.Data == null) { break; } byte[] blockData = Operation(readerChunk.Data); Chunk writerChunk = new Chunk(readerChunk.Id, blockData); if (!writerQueue.TryEnqueue(writerChunk)) { return; } } catch (Exception) { Cancel(); Error(); Console.WriteLine("Error compressing"); return; } } }