Java Code Examples for com.microsoft.azure.storage.core.Base64#encode()

The following examples show how to use com.microsoft.azure.storage.core.Base64#encode() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StorageAccountTests.java    From azure-storage-android with Apache License 2.0 6 votes vote down vote up
@Test
public void testStorageCredentialsSharedKey() throws URISyntaxException, StorageException {
    StorageCredentialsAccountAndKey cred = new StorageCredentialsAccountAndKey(ACCOUNT_NAME, ACCOUNT_KEY);

    assertEquals(ACCOUNT_NAME, cred.getAccountName());

    URI testUri = new URI("http://test/abc?querya=1");
    assertEquals(testUri, cred.transformUri(testUri));

    assertEquals(ACCOUNT_KEY, cred.exportBase64EncodedKey());
    byte[] dummyKey = { 0, 1, 2 };
    String base64EncodedDummyKey = Base64.encode(dummyKey);
    cred = new StorageCredentialsAccountAndKey(ACCOUNT_NAME, base64EncodedDummyKey);
    assertEquals(base64EncodedDummyKey, cred.exportBase64EncodedKey());

    dummyKey[0] = 3;
    base64EncodedDummyKey = Base64.encode(dummyKey);
    cred = new StorageCredentialsAccountAndKey(ACCOUNT_NAME, base64EncodedDummyKey);
    assertEquals(base64EncodedDummyKey, cred.exportBase64EncodedKey());
}
 
Example 2
Source File: StorageAccountTests.java    From azure-storage-android with Apache License 2.0 6 votes vote down vote up
@Test
public void testStorageCredentialsSharedKeyUpdateKey() throws URISyntaxException, StorageException {
    StorageCredentialsAccountAndKey cred = new StorageCredentialsAccountAndKey(ACCOUNT_NAME, ACCOUNT_KEY);
    assertEquals(ACCOUNT_KEY, cred.exportBase64EncodedKey());

    // Validate update with byte array
    byte[] dummyKey = { 0, 1, 2 };
    cred.updateKey(dummyKey);
    String base64EncodedDummyKey = Base64.encode(dummyKey);
    assertEquals(base64EncodedDummyKey, cred.exportBase64EncodedKey());

    // Validate update with string
    dummyKey[0] = 3;
    base64EncodedDummyKey = Base64.encode(dummyKey);
    cred.updateKey(base64EncodedDummyKey);
    assertEquals(base64EncodedDummyKey, cred.exportBase64EncodedKey());
}
 
Example 3
Source File: CloudQueueMessage.java    From azure-storage-android with Apache License 2.0 6 votes vote down vote up
/**
 * Gets the content of the message for transfer (internal use only).
 * 
 * @return A <code>String</code> which contains the content of the message.
 * 
 * @throws StorageException
 *         If a storage service error occurred.
 */
protected final String getMessageContentForTransfer(final boolean shouldEncodeMessage) throws StorageException {
    String result = null;
    if (this.messageType == QueueMessageType.RAW_STRING && shouldEncodeMessage) {
        result = Base64.encode(this.getMessageContentAsByte());
    }
    else {
        result = this.messageContent;
    }

    if (result != null && result.length() > QueueConstants.MAX_MESSAGE_SIZE) {
        throw new IllegalArgumentException(
                String.format(SR.INVALID_MESSAGE_LENGTH, QueueConstants.MAX_MESSAGE_SIZE));
    }

    return result;
}
 
Example 4
Source File: BlobOutputStream.java    From azure-storage-android with Apache License 2.0 5 votes vote down vote up
/**
 * Generates a new block ID to be used for PutBlock.
 * 
 * @return Base64 encoded block ID
 * @throws IOException
 */
private String getCurrentBlockId() throws IOException
{
    String blockIdSuffix = String.format("%06d", this.blockList.size());
    
    byte[] blockIdInBytes;
    try {
        blockIdInBytes = (this.blockIdPrefix + blockIdSuffix).getBytes(Constants.UTF8_CHARSET);
    } catch (UnsupportedEncodingException e) {
        // this should never happen, UTF8 is a default charset
        throw new IOException(e);
    }
    
    return Base64.encode(blockIdInBytes);
}
 
Example 5
Source File: CloudQueueMessage.java    From azure-storage-android with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the content of the message as a <code>byte</code> array.
 * 
 * @param content
 *        A <code>byte</code> array which contains the content of the message.
 */
public final void setMessageContent(final byte[] content) {
    Utility.assertNotNull("content", content);

    this.messageContent = Base64.encode(content);
    this.messageType = QueueMessageType.BASE_64_ENCODED;
}
 
Example 6
Source File: TestBlobDataValidation.java    From hadoop with Apache License 2.0 4 votes vote down vote up
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));
  }
}
 
Example 7
Source File: TestBlobDataValidation.java    From big-c with Apache License 2.0 4 votes vote down vote up
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));
  }
}
 
Example 8
Source File: CloudFileTests.java    From azure-storage-android with Apache License 2.0 4 votes vote down vote up
@Test
public void testFileUploadMD5Validation() throws URISyntaxException, StorageException, IOException,
        NoSuchAlgorithmException {
    final String fileName = FileTestHelper.generateRandomFileName();
    final CloudFile fileRef = this.share.getRootDirectoryReference().getFileReference(fileName);

    final int length = 4 * 1024;

    byte[] src = FileTestHelper.getRandomBuffer(length);
    ByteArrayInputStream srcStream = new ByteArrayInputStream(src);
    FileRequestOptions options = new FileRequestOptions();
    options.setDisableContentMD5Validation(false);
    options.setStoreFileContentMD5(false);

    fileRef.upload(srcStream, length, null, options, null);
    fileRef.downloadAttributes();
    fileRef.getProperties().setContentMD5("MDAwMDAwMDA=");
    fileRef.uploadProperties(null, options, null);

    try {
        fileRef.download(new ByteArrayOutputStream(), null, options, null);
        fail();
    }
    catch (StorageException ex) {
        assertEquals(306, ex.getHttpStatusCode());
        assertEquals("InvalidMd5", ex.getErrorCode());
    }

    options.setDisableContentMD5Validation(true);
    fileRef.download(new ByteArrayOutputStream(), null, options, null);

    options.setDisableContentMD5Validation(false);
    final MessageDigest digest = MessageDigest.getInstance("MD5");
    final String calculatedMD5 = Base64.encode(digest.digest(src));

    fileRef.downloadAttributes();
    fileRef.getProperties().setContentMD5(calculatedMD5);
    fileRef.uploadProperties(null, options, null);
    fileRef.download(new ByteArrayOutputStream(), null, options, null);
    fileRef.downloadToByteArray(new byte[4096], 0);

    FileInputStream stream = fileRef.openRead();
    stream.mark(length);
    stream.read(new byte[4096]);
    try {
        HashMap<String, String> metadata = new HashMap<String, String>();
        metadata.put("a", "value");
        fileRef.setMetadata(metadata);
        fileRef.uploadMetadata();
        stream.reset();
        stream.read(new byte[4096]);
    }
    catch (IOException e) {
        assertEquals(e.getCause().getMessage(),
                "The conditionals specified for this operation did not match server.");
    }

    final CloudFile fileRef2 = this.share.getRootDirectoryReference().getFileReference(fileName);
    assertNull(fileRef2.getProperties().getContentMD5());

    byte[] target = new byte[4];
    fileRef2.downloadRangeToByteArray(0L, 4L, target, 0);
    assertEquals(calculatedMD5, fileRef2.getProperties().getContentMD5());
}
 
Example 9
Source File: BlobInputStream.java    From azure-storage-android with Apache License 2.0 4 votes vote down vote up
/**
 * Performs internal read to the given byte buffer.
 * 
 * @param b
 *            A <code>byte</code> array which represents the buffer into which the data is read.
 * @param off
 *            An <code>int</code> which represents the start offset in the <code>byte</code> array <code>b</code> at
 *            which the data is written.
 * @param len
 *            An <code>int</code> which represents the maximum number of bytes to read.
 * 
 * @return An <code>int</code> which represents the total number of bytes read into the buffer, or -1 if
 *         there is no more data because the end of the stream has been reached.
 * 
 * @throws IOException
 *             If the first byte cannot be read for any reason other than end of file, or if the input stream has
 *             been closed, or if some other I/O error occurs.
 */
@DoesServiceRequest
private synchronized int readInternal(final byte[] b, final int off, int len) throws IOException {
    this.checkStreamState();

    // if buffer is empty do next get operation
    if ((this.currentBuffer == null || this.currentBuffer.available() == 0)
            && this.currentAbsoluteReadPosition < this.streamLength) {
        this.dispatchRead((int) Math.min(this.readSize, this.streamLength - this.currentAbsoluteReadPosition));
    }

    len = Math.min(len, this.readSize);

    // do read from buffer
    final int numberOfBytesRead = this.currentBuffer.read(b, off, len);

    if (numberOfBytesRead > 0) {
        this.currentAbsoluteReadPosition += numberOfBytesRead;

        if (this.validateBlobMd5) {
            this.md5Digest.update(b, off, numberOfBytesRead);

            if (this.currentAbsoluteReadPosition == this.streamLength) {
                // Reached end of stream, validate md5.
                final String calculatedMd5 = Base64.encode(this.md5Digest.digest());
                if (!calculatedMd5.equals(this.retrievedContentMD5Value)) {
                    this.lastError = Utility
                            .initIOException(new StorageException(
                                    StorageErrorCodeStrings.INVALID_MD5,
                                    String.format(
                                            "Blob data corrupted (integrity check failed), Expected value is %s, retrieved %s",
                                            this.retrievedContentMD5Value, calculatedMd5),
                                    Constants.HeaderConstants.HTTP_UNUSED_306, null, null));
                    this.streamFaulted = true;
                    throw this.lastError;
                }
            }
        }
    }

    // update markers
    if (this.markExpiry > 0 && this.markedPosition + this.markExpiry < this.currentAbsoluteReadPosition) {
        this.markedPosition = 0;
        this.markExpiry = 0;
    }

    return numberOfBytesRead;
}
 
Example 10
Source File: FileInputStream.java    From azure-storage-android with Apache License 2.0 4 votes vote down vote up
/**
 * Performs internal read to the given byte buffer.
 * 
 * @param b
 *            A <code>byte</code> array which represents the buffer into which the data is read.
 * @param off
 *            An <code>int</code> which represents the start offset in the <code>byte</code> array <code>b</code> at
 *            which the data is written.
 * @param len
 *            An <code>int</code> which represents the maximum number of bytes to read.
 * 
 * @return An <code>int</code> which represents the total number of bytes read into the buffer, or -1 if
 *         there is no more data because the end of the stream has been reached.
 * 
 * @throws IOException
 *             If the first byte cannot be read for any reason other than end of file, or if the input stream has
 *             been closed, or if some other I/O error occurs.
 */
@DoesServiceRequest
private synchronized int readInternal(final byte[] b, final int off, int len) throws IOException {
    this.checkStreamState();

    // if buffer is empty do next get operation
    if ((this.currentBuffer == null || this.currentBuffer.available() == 0)
            && this.currentAbsoluteReadPosition < this.streamLength) {
        this.dispatchRead((int) Math.min(this.readSize, this.streamLength - this.currentAbsoluteReadPosition));
    }

    len = Math.min(len, this.readSize);

    // do read from buffer
    final int numberOfBytesRead = this.currentBuffer.read(b, off, len);

    if (numberOfBytesRead > 0) {
        this.currentAbsoluteReadPosition += numberOfBytesRead;

        if (this.validateFileMd5) {
            this.md5Digest.update(b, off, numberOfBytesRead);

            if (this.currentAbsoluteReadPosition == this.streamLength) {
                // Reached end of stream, validate md5.
                final String calculatedMd5 = Base64.encode(this.md5Digest.digest());
                if (!calculatedMd5.equals(this.retrievedContentMD5Value)) {
                    this.lastError = Utility
                            .initIOException(new StorageException(
                                    StorageErrorCodeStrings.INVALID_MD5,
                                    String.format(
                                            "File data corrupted (integrity check failed), Expected value is %s, retrieved %s",
                                            this.retrievedContentMD5Value, calculatedMd5),
                                    Constants.HeaderConstants.HTTP_UNUSED_306, null, null));
                    this.streamFaulted = true;
                    throw this.lastError;
                }
            }
        }
    }

    // update markers
    if (this.markExpiry > 0 && this.markedPosition + this.markExpiry < this.currentAbsoluteReadPosition) {
        this.markedPosition = 0;
        this.markExpiry = 0;
    }

    return numberOfBytesRead;
}
 
Example 11
Source File: StorageCredentialsAccountAndKey.java    From azure-storage-android with Apache License 2.0 2 votes vote down vote up
/**
 * Exports the value of the access key to a Base64-encoded string.
 * 
 * @return A <code>String</code> that represents the Base64-encoded access key.
 */
public String exportBase64EncodedKey() {
    return Base64.encode(this.key);
}
 
Example 12
Source File: EntityProperty.java    From azure-storage-android with Apache License 2.0 2 votes vote down vote up
/**
 * Sets this {@link EntityProperty} using the serialized <code>byte[]</code> value.
 * 
 * @param value
 *            The <code>byte[]</code> value to set as the {@link EntityProperty} value. This value may be
 *            <code>null</code>.
 */
public synchronized final void setValue(final byte[] value) {
    this.edmType = EdmType.BINARY;
    this.type = byte[].class;
    this.value = value == null ? null : Base64.encode(value);
}
 
Example 13
Source File: EntityProperty.java    From azure-storage-android with Apache License 2.0 2 votes vote down vote up
/**
 * Sets this {@link EntityProperty} using the serialized <code>Byte[]</code> value.
 * 
 * @param value
 *            The <code>Byte[]</code> value to set as the {@link EntityProperty} value. This value may be
 *            <code>null</code>.
 */
public synchronized final void setValue(final Byte[] value) {
    this.edmType = EdmType.BINARY;
    this.type = Byte[].class;
    this.value = value == null ? null : Base64.encode(value);
}