/// <summary> /// Safely receive the header. /// </summary> /// <param name="blob">blob</param> /// <param name="headerDefinition">header definition</param> /// <param name="exception">exception that occured</param> /// <returns>the header, otherwise null if it could not be fetched</returns> private StreamBlobHeader SafeGetHeader(WrappedPageBlob blob, HeaderDefinitionMetadata headerDefinition, out Exception exception) { StreamBlobHeader header = null; exception = null; try { if (headerDefinition == null || headerDefinition.HeaderSizeInBytes == 0) { throw new InvalidHeaderDataException( $"Attempted to download a header, but the size specified is zero. This aggregate with id [{blob.Name}] may be corrupt."); } var downloadedData = blob.DownloadBytes(headerDefinition.HeaderStartLocationOffsetBytes, headerDefinition.HeaderStartLocationOffsetBytes + headerDefinition.HeaderSizeInBytes); using (var ms = new MemoryStream(downloadedData, false)) { header = _serializer.Deserialize <StreamBlobHeader>(ms.ToArray()); } } catch (Exception ex) { exception = ex; } return(header); }
/// <summary> /// Creates a commit from the provided definition /// </summary> /// <param name="streamId"></param> /// <param name="commitDefinition"></param> /// <returns></returns> private ICommit CreateCommitFromDefinition(WrappedPageBlob blob, PageBlobCommitDefinition commitDefinition) { var startIndex = commitDefinition.StartPage * BlobPageSize; var endIndex = startIndex + commitDefinition.DataSizeBytes; var downloadedData = blob.DownloadBytes(startIndex, endIndex, true); using (var ms = new MemoryStream(downloadedData, false)) { AzureBlobCommit azureBlobCommit; try { azureBlobCommit = _serializer.Deserialize <AzureBlobCommit>(ms); } catch (Exception ex) { var message = $"Blob with uri [{((CloudPageBlob) blob).Uri}] is corrupt."; Logger.Fatal(message); throw new InvalidDataException(message, ex); } return(CreateCommitFromAzureBlobCommit(azureBlobCommit)); } }