private static Task CreateFillQueueTask(Stream inputStream, List <string> blockIds, BlockingCollection <ByteArrayWithBlockId> queue, CancellationTokenSource cts) { var fillQueueTask = Task.Run(() => { // Put Block is limited to 4MB per block var buffer = new byte[OnePutBlockSizeLimitInBytes]; try { var blockNumber = 0; while (true) { if (cts.IsCancellationRequested) { return; } var read = inputStream.Read(buffer, 0, buffer.Length); if (read <= 0) { break; } while (read < buffer.Length) { var lastRead = inputStream.Read(buffer, read, buffer.Length - read); if (lastRead <= 0) { break; } read += lastRead; } var destination = new byte[read]; Buffer.BlockCopy(buffer, 0, destination, 0, read); var blockNumberInBytes = BitConverter.GetBytes(blockNumber++); var blockIdString = Convert.ToBase64String(blockNumberInBytes); blockIds.Add(blockIdString); var byteArrayWithBlockId = new ByteArrayWithBlockId { StreamAsByteArray = destination, BlockId = blockIdString }; while (queue.TryAdd(byteArrayWithBlockId, millisecondsTimeout: 200) == false) { if (cts.IsCancellationRequested) { return; } } } } catch (Exception) { //if we can't read from the input stream, //we need to cancel the upload tasks cts.Cancel(); throw; } finally { queue.CompleteAdding(); //we don't need the stream anymore inputStream.Dispose(); } }); return(fillQueueTask); }
private static Task CreateFillQueueTask(Stream inputStream, List<string> blockIds, BlockingCollection<ByteArrayWithBlockId> queue, CancellationTokenSource cts) { var fillQueueTask = Task.Run(() => { // Put Block is limited to 4MB per block var buffer = new byte[OnePutBlockSizeLimitInBytes]; try { var blockNumber = 0; while (true) { if (cts.IsCancellationRequested) return; var read = inputStream.Read(buffer, 0, buffer.Length); if (read <= 0) break; while (read < buffer.Length) { var lastRead = inputStream.Read(buffer, read, buffer.Length - read); if (lastRead <= 0) break; read += lastRead; } var destination = new byte[read]; Buffer.BlockCopy(buffer, 0, destination, 0, read); var blockNumberInBytes = BitConverter.GetBytes(blockNumber++); var blockIdString = Convert.ToBase64String(blockNumberInBytes); blockIds.Add(blockIdString); var byteArrayWithBlockId = new ByteArrayWithBlockId { StreamAsByteArray = destination, BlockId = blockIdString }; while (queue.TryAdd(byteArrayWithBlockId, millisecondsTimeout: 200) == false) { if (cts.IsCancellationRequested) return; } } } catch (Exception) { //if we can't read from the input stream, //we need to cancel the upload tasks cts.Cancel(); throw; } finally { queue.CompleteAdding(); //we don't need the stream anymore inputStream.Dispose(); } }); return fillQueueTask; }