private void CalculateBlocks(Queue<Block> blocksToUpload, int blobLength, ICollection<string> order)
 {
     var offset = 0;
     while (blobLength > 0)
     {
         var blockLength = Math.Min(BlockSize, blobLength);
         var block = new Block { Offset = offset, Length = blockLength };
         blocksToUpload.Enqueue(block);
         order.Add(block.Name);
         offset += blockLength;
         blobLength -= blockLength;
     }
 }
        private Stream DownloadBlockFromBlob(CloudBlob blob, Block block, Queue<Block> blocksToDownload)
        {
            try
            {
                var blobGetRequest = BlobRequest.Get(blob.Uri, 60, null, null);
                blobGetRequest.Headers.Add("x-ms-range", string.Format(CultureInfo.InvariantCulture, "bytes={0}-{1}", block.Offset, block.Offset + block.Length - 1));
                var credentials = blob.ServiceClient.Credentials;
                credentials.SignRequest(blobGetRequest);
                var response = blobGetRequest.GetResponse() as HttpWebResponse;
                return response != null ? response.GetResponseStream() : null;
            }
            catch
            {
                if (++block.Attempt > MaxRetries) throw;

                lock (blocksToDownload) blocksToDownload.Enqueue(block);
            }
            return null;
        }
 private static void ExtractBytesFromBlockIntoBuffer(byte[] buffer, Stream s, Block block)
 {
     var offsetInBlock = 0;
     var remaining = block.Length;
     while (remaining > 0)
     {
         var read = s.Read(buffer, offsetInBlock, remaining);
         offsetInBlock += read;
         remaining -= read;
     }
 }