public void Read(Stream source, BoundedBlockingQueue <byte[]> target) { while (true) { var buffer = ReadInternal(source, out var readBytes); if (readBytes == 0) { break; } if (buffer.Length == 0) { continue; } target.Add(buffer); logger.Debug($"Read bytes {buffer.Length}"); } if (temp.Count != 0) { target.Add(temp.ToArray()); logger.Debug($"Read bytes (temp) {temp.Count}"); } }
void TestBoundedBlockingQueueAddRemove() { BoundedBlockingQueue <TestQueueItem> bbq = new BoundedBlockingQueue <TestQueueItem>(1); Assert.Equal(0, bbq.Count); Assert.Throws <InvalidOperationException>(() => bbq.Add(new TestQueueItem { Value = 100 })); Assert.Equal(0, bbq.Count); Assert.Throws <InvalidOperationException>(() => bbq.Remove(new TestQueueItem { Value = 100 })); Assert.Equal(0, bbq.Count); }
public void Read(Stream source, BoundedBlockingQueue <byte[]> target) { LogFactory.GetInstance().GetLogger <ConsoleLogger>().Debug("Reading started"); while (true) { var buffer = new byte[bufferSize]; var readBytes = source.Read(buffer, 0, buffer.Length); if (readBytes == 0) { break; } if (readBytes < bufferSize) { Array.Resize(ref buffer, readBytes); } target.Add(buffer); } }
public void Write(BoundedBlockingQueue <IndexedBuffer> source, Stream target) { logger.Debug("Writing started"); var currentIndex = 0; foreach (var buffer in source.Consume()) { var bufferIndex = buffer.Index; if (currentIndex == bufferIndex) { WriteInternal(buffer, target); currentIndex++; continue; } if (bufferIndex > currentIndex) { source.Add(buffer); } } }
public void Process(BoundedBlockingQueue <byte[]> source, BoundedBlockingQueue <IndexedBuffer> target) { Logger.Debug("Processing started"); var bufferFactory = new IndexedBufferFactory(); var waitHandlesHelper = new EventWaitHandlesHelper(); foreach (var buffer in source.Consume()) { var waitHandle = source.AddingCompleted ? waitHandlesHelper.GetNew() : null; var indexedBuffer = bufferFactory.GetNext(); scheduler.StartNew(() => { indexedBuffer.Data = ProcessInternal(buffer); target.Add(indexedBuffer); Logger.Debug($"Processed {indexedBuffer.Index}"); }, waitHandle); } Logger.Debug("Consuming finished"); waitHandlesHelper.WaitAll(); target.CompleteAdding(); }