Java源码示例:com.microsoft.azure.storage.blob.BlockEntry

示例1
private void testStoreBlobMd5(boolean expectMd5Stored) throws Exception {
  assumeNotNull(testAccount);
  // Write a test file.
  String testFileKey = "testFile";
  Path testFilePath = new Path("/" + testFileKey);
  OutputStream outStream = testAccount.getFileSystem().create(testFilePath);
  outStream.write(new byte[] { 5, 15 });
  outStream.close();

  // Check that we stored/didn't store the MD5 field as configured.
  CloudBlockBlob blob = testAccount.getBlobReference(testFileKey);
  blob.downloadAttributes();
  String obtainedMd5 = blob.getProperties().getContentMD5();
  if (expectMd5Stored) {
    assertNotNull(obtainedMd5);
  } else {
    assertNull("Expected no MD5, found: " + obtainedMd5, obtainedMd5);
  }

  // Mess with the content so it doesn't match the MD5.
  String newBlockId = Base64.encode(new byte[] { 55, 44, 33, 22 });
  blob.uploadBlock(newBlockId,
      new ByteArrayInputStream(new byte[] { 6, 45 }), 2);
  blob.commitBlockList(Arrays.asList(new BlockEntry[] { new BlockEntry(
      newBlockId, BlockSearchMode.UNCOMMITTED) }));

  // Now read back the content. If we stored the MD5 for the blob content
  // we should get a data corruption error.
  InputStream inStream = testAccount.getFileSystem().open(testFilePath);
  try {
    byte[] inBuf = new byte[100];
    while (inStream.read(inBuf) > 0){
      //nothing;
    }
    inStream.close();
    if (expectMd5Stored) {
      fail("Should've thrown because of data corruption.");
    }
  } catch (IOException ex) {
    if (!expectMd5Stored) {
      throw ex;
    }
    StorageException cause = (StorageException)ex.getCause();
    assertNotNull(cause);
    assertTrue("Unexpected cause: " + cause,
        cause.getErrorCode().equals(StorageErrorCodeStrings.INVALID_MD5));
  }
}
 
示例2
private void testStoreBlobMd5(boolean expectMd5Stored) throws Exception {
  assumeNotNull(testAccount);
  // Write a test file.
  String testFileKey = "testFile";
  Path testFilePath = new Path("/" + testFileKey);
  OutputStream outStream = testAccount.getFileSystem().create(testFilePath);
  outStream.write(new byte[] { 5, 15 });
  outStream.close();

  // Check that we stored/didn't store the MD5 field as configured.
  CloudBlockBlob blob = testAccount.getBlobReference(testFileKey);
  blob.downloadAttributes();
  String obtainedMd5 = blob.getProperties().getContentMD5();
  if (expectMd5Stored) {
    assertNotNull(obtainedMd5);
  } else {
    assertNull("Expected no MD5, found: " + obtainedMd5, obtainedMd5);
  }

  // Mess with the content so it doesn't match the MD5.
  String newBlockId = Base64.encode(new byte[] { 55, 44, 33, 22 });
  blob.uploadBlock(newBlockId,
      new ByteArrayInputStream(new byte[] { 6, 45 }), 2);
  blob.commitBlockList(Arrays.asList(new BlockEntry[] { new BlockEntry(
      newBlockId, BlockSearchMode.UNCOMMITTED) }));

  // Now read back the content. If we stored the MD5 for the blob content
  // we should get a data corruption error.
  InputStream inStream = testAccount.getFileSystem().open(testFilePath);
  try {
    byte[] inBuf = new byte[100];
    while (inStream.read(inBuf) > 0){
      //nothing;
    }
    inStream.close();
    if (expectMd5Stored) {
      fail("Should've thrown because of data corruption.");
    }
  } catch (IOException ex) {
    if (!expectMd5Stored) {
      throw ex;
    }
    StorageException cause = (StorageException)ex.getCause();
    assertNotNull(cause);
    assertTrue("Unexpected cause: " + cause,
        cause.getErrorCode().equals(StorageErrorCodeStrings.INVALID_MD5));
  }
}