private void Decompress(object threadNumber) { ByteChunk inputChunk; while (readBuffer.TryDequeue(out inputChunk) && !cancel) { using (MemoryStream ms = new MemoryStream(inputChunk.Content)) { using (GZipStream gz = new GZipStream(ms, CompressionMode.Decompress)) { int bytesRead; byte[] buffer = new byte[bufferSize]; bytesRead = gz.Read(buffer, 0, buffer.Length); byte[] lastBuffer = new byte[bytesRead]; Buffer.BlockCopy(buffer, 0, lastBuffer, 0, bytesRead); ByteChunk outputChunk = new ByteChunk(inputChunk.ID, lastBuffer); writeBuffer.Enqueue(outputChunk); } } } ManualResetEvent exitThreat = exitCompressionThread[(int)threadNumber]; exitThreat.Set(); }
public void EnquequeBytes(byte[] buffer) { lock (queue) { while (queueCounter >= maxSize) { Monitor.Wait(queue); } ByteChunk chunk = new ByteChunk(idCounter, buffer); queue.Enqueue(chunk); idCounter++; queueCounter++; Monitor.PulseAll(queue); } }
public void Enqueue(ByteChunk chunk) { int id = chunk.ID; lock (queue) { while (queueCounter >= maxSize || id != idCounter) { Monitor.Wait(queue); } queue.Enqueue(chunk); idCounter++; queueCounter++; Monitor.PulseAll(queue); } }
public bool TryDequeue(out ByteChunk chunk) { lock (queue) { while (queueCounter == 0) { if (closed) { chunk = new ByteChunk(); return(false); } Monitor.Wait(queue); } chunk = queue.Dequeue(); queueCounter--; Monitor.PulseAll(queue); return(true); } }
private void Compress(object threadNumber) { ByteChunk inputChunk; while (readBuffer.TryDequeue(out inputChunk) && !cancel) { using (MemoryStream ms = new MemoryStream()) { using (GZipStream gz = new GZipStream(ms, CompressionMode.Compress)) using (BinaryWriter bw = new BinaryWriter(gz)) { bw.Write(inputChunk.Content, 0, inputChunk.Content.Length); } byte[] outBuffer = ms.ToArray(); ByteChunk outputChunk = new ByteChunk(inputChunk.ID, outBuffer); writeBuffer.Enqueue(outputChunk); } } ManualResetEvent exitThread = exitCompressionThread[(int)threadNumber]; exitThread.Set(); }