/// <summary>
        /// Read the metadata out of a blob file (stored in the system as length + bytes). It is assumed that the blob
        /// exists.
        /// </summary>
        /// <param name="blob">source of the metadata</param>
        /// <returns>metadata bytes</returns>
        public static byte[] ReadMetadataFile(CloudPageBlob blob)
        {
            // Optimization for small metadata file size: we read some maximum length bytes, and if the file size is
            // smaller than that, we do not have to issue a second read request after reading the length of metadata.
            byte[] firstRead  = new byte[OneShotReadMaxFileSize];
            var    downloaded = blob.DownloadRangeToByteArray(firstRead, 0, 0, OneShotReadMaxFileSize);

            Debug.Assert(downloaded == OneShotReadMaxFileSize);


            int length = BitConverter.ToInt32(firstRead, 0);

            // If file length is less than what we have read, we can return from memory immediately
            if (length < OneShotReadMaxFileSize - sizeof(int))
            {
                // This still copies the array. But since the cost will be dominated by read request to Azure, I am
                // guessing it does not matter
                return(firstRead.Skip(sizeof(int)).Take(length).ToArray());
            }

            // Otherwise, copy over what we have read and read the remaining bytes.
            byte[] result       = new byte[length];
            int    numBytesRead = OneShotReadMaxFileSize - sizeof(int);

            Array.Copy(firstRead, sizeof(int), result, 0, numBytesRead);
            downloaded = blob.DownloadRangeToByteArray(result, numBytesRead, OneShotReadMaxFileSize, length - numBytesRead);
            Debug.Assert(downloaded == length - numBytesRead, "Underfilled read buffer");
            return(result);
        }
        //********************
        //*                  *
        //*  PageRead_Click  *
        //*                  *
        //********************
        // Read a page.

        private void PageRead_Click(object sender, RoutedEventArgs e)
        {
            int pageNumber = 0;

            byte[] bytes = new byte[512];

            try
            {
                pageNumber = Convert.ToInt32(PageNumber.Text);
                if (pageNumber < 0 || pageNumber > MaxPageNumber)
                {
                    MessageBox.Show("Cannot write page - page number is out of range.\n\nEnter a value from 0 to " + MaxPageNumber.ToString(), "Invalid Page Number");
                    return;
                }

                Cursor = Cursors.Wait;

                byte[] data = new byte[512];

                PageBlob.DownloadRangeToByteArray(data, 0, pageNumber * 512, 512);

                PageHexData.Text = BitConverter.ToString(data).Replace("-", " ");

                Cursor = Cursors.Arrow;
            }
            catch (Exception ex)
            {
                MessageBox.Show("The following error occurred attempting to read the page:\n\n" + ex.Message, "Error reading page");
            }
        }
        public byte[] DownloadPageData(CloudBlobContainer container, string key, long startOffset, long length)
        {
            CloudPageBlob pageBlob = container.GetPageBlobReference(key);

            byte[] result = new byte[length];
            pageBlob.DownloadRangeToByteArray(result, 0, startOffset, length);
            return(result);
        }
示例#4
0
        /// <summary>
        /// Download the page range specified
        /// </summary>
        /// <param name="startPage">start page</param>
        /// <param name="endPage">end page</param>
        /// <param name="accessCondition">access conditions</param>
        /// <returns></returns>
        public byte[] DownloadBytes(int startIndex, int endIndex, bool disregardConcurrency = false)
        {
            try
            {
                var accessCondition = disregardConcurrency ? null : AccessCondition.GenerateIfMatchCondition(_pageBlob.Properties.ETag);

                var data = new byte[endIndex - startIndex];
                Logger.Verbose("Downloading [{0}] bytes for blob [{1}], etag [{2}]", data.Length, _pageBlob.Uri, _pageBlob.Properties.ETag);
                var bytesDownloaded = _pageBlob.DownloadRangeToByteArray(data, 0, startIndex, data.Length,
                                                                         accessCondition);
                return(data);
            }
            catch (AzureStorage.StorageException ex)
            { throw HandleAndRemapCommonExceptions(ex); }
        }
示例#5
0
        public override int Read(byte[] buffer, int offset, int count)
        {
            long currentLength = Length;

            // can't skip ahead of write
            if (_readPosition + count > currentLength)
            {
                count = (int)(currentLength - _readPosition);
            }

            if (count == 0)
            {
                return(0);
            }

            int numBytesRead = _pageBlob.DownloadRangeToByteArray(buffer, offset, _readPosition, count);

            _readPosition += numBytesRead;
            return(numBytesRead);
        }