com.microsoft.azure.storage.core.Base64 Java Examples
The following examples show how to use
com.microsoft.azure.storage.core.Base64.
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: CloudQueueMessage.java From azure-storage-android with Apache License 2.0 | 6 votes |
/** * 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 #2
Source File: CloudQueueMessage.java From azure-storage-android with Apache License 2.0 | 6 votes |
/** * Gets the content of the message as a string. * * @return A <code>String</code> which contains the content of the message. * * @throws StorageException * If a storage service error occurred. */ public final String getMessageContentAsString() throws StorageException { if (this.messageType == QueueMessageType.RAW_STRING) { return this.messageContent; } else { if (Utility.isNullOrEmpty(this.messageContent)) { return null; } try { return new String(Base64.decode(this.messageContent), Constants.UTF8_CHARSET); } catch (final UnsupportedEncodingException e) { throw Utility.generateNewUnexpectedStorageException(e); } } }
Example #3
Source File: CloudQueueMessage.java From azure-storage-android with Apache License 2.0 | 6 votes |
/** * Gets the content of the message as a byte array. * * @return A <code>byte</code> array which contains the content of the message. * * @throws StorageException * If a storage service error occurred. */ public final byte[] getMessageContentAsByte() throws StorageException { if (Utility.isNullOrEmpty(this.messageContent)) { return new byte[0]; } if (this.messageType == QueueMessageType.RAW_STRING) { try { return this.messageContent.getBytes(Constants.UTF8_CHARSET); } catch (final UnsupportedEncodingException e) { throw Utility.generateNewUnexpectedStorageException(e); } } else { return Base64.decode(this.messageContent); } }
Example #4
Source File: StorageAccountTests.java From azure-storage-android with Apache License 2.0 | 6 votes |
@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 #5
Source File: StorageAccountTests.java From azure-storage-android with Apache License 2.0 | 6 votes |
@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 #6
Source File: StorageCredentials.java From azure-storage-android with Apache License 2.0 | 6 votes |
/** * Tries to determine the storage credentials from a collection of name/value pairs. * * @param settings * A <code>Map</code> object of the name/value pairs that represent the settings to use to configure * the credentials. * <p> * Either include an account name with an account key (specifying values for * {@link CloudStorageAccount#ACCOUNT_NAME_NAME} and {@link CloudStorageAccount#ACCOUNT_KEY_NAME} ), or a * shared access signature (specifying a value for * {@link CloudStorageAccount#SHARED_ACCESS_SIGNATURE_NAME} ). If you use an account name and account * key, do not include a shared access signature, and vice versa. * * @return A {@link StorageCredentials} object representing the storage credentials determined from the name/value * pairs. * * @throws InvalidKeyException * If the key value specified for {@link CloudStorageAccount#ACCOUNT_KEY_NAME} is not a valid * Base64-encoded string. */ protected static StorageCredentials tryParseCredentials(final Map<String, String> settings) throws InvalidKeyException { final String accountName = settings.get(CloudStorageAccount.ACCOUNT_NAME_NAME) != null ? settings.get(CloudStorageAccount.ACCOUNT_NAME_NAME) : null; final String accountKey = settings.get(CloudStorageAccount.ACCOUNT_KEY_NAME) != null ? settings.get(CloudStorageAccount.ACCOUNT_KEY_NAME) : null; final String sasSignature = settings.get(CloudStorageAccount.SHARED_ACCESS_SIGNATURE_NAME) != null ? settings.get(CloudStorageAccount.SHARED_ACCESS_SIGNATURE_NAME) : null; if (accountName != null && accountKey != null && sasSignature == null) { if (Base64.validateIsBase64String(accountKey)) { return new StorageCredentialsAccountAndKey(accountName, accountKey); } else { throw new InvalidKeyException(SR.INVALID_KEY); } } if (accountKey == null && sasSignature != null) { return new StorageCredentialsSharedAccessSignature(sasSignature); } return null; }
Example #7
Source File: BlobOutputStream.java From azure-storage-android with Apache License 2.0 | 6 votes |
/** * Commits the blob, for block blob this uploads the block list. * * @throws StorageException * An exception representing any error which occurred during the operation. */ @DoesServiceRequest private synchronized void commit() throws StorageException { if (this.options.getStoreBlobContentMD5()) { this.parentBlobRef.getProperties().setContentMD5(Base64.encode(this.md5Digest.digest())); } if (this.streamType == BlobType.BLOCK_BLOB) { // wait for all blocks to finish final CloudBlockBlob blobRef = (CloudBlockBlob) this.parentBlobRef; blobRef.commitBlockList(this.blockList, this.accessCondition, this.options, this.opContext); } else if (this.options.getStoreBlobContentMD5()) { this.parentBlobRef.uploadProperties(this.accessCondition, this.options, this.opContext); } }
Example #8
Source File: AzureProtocol.java From cyberduck with GNU General Public License v3.0 | 5 votes |
@Override public boolean validate(final Credentials credentials, final LoginOptions options) { if(super.validate(credentials, options)) { if(options.password) { return Base64.validateIsBase64String(credentials.getPassword()); } return true; } return false; }
Example #9
Source File: CloudQueueMessage.java From azure-storage-android with Apache License 2.0 | 5 votes |
/** * 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 #10
Source File: StorageAccountTests.java From azure-storage-android with Apache License 2.0 | 5 votes |
@Test public void testCloudStorageAccountExportKey() throws InvalidKeyException, URISyntaxException { String accountKeyString = "abc2564="; String accountString = "BlobEndpoint=http://blobs/;AccountName=test;AccountKey=" + accountKeyString; CloudStorageAccount account = CloudStorageAccount.parse(accountString); StorageCredentialsAccountAndKey accountAndKey = (StorageCredentialsAccountAndKey) account.getCredentials(); String key = accountAndKey.exportBase64EncodedKey(); assertEquals(accountKeyString, key); byte[] keyBytes = accountAndKey.exportKey(); byte[] expectedKeyBytes = Base64.decode(accountKeyString); TestHelper.assertByteArrayEquals(expectedKeyBytes, keyBytes); }
Example #11
Source File: FileOutputStream.java From azure-storage-android with Apache License 2.0 | 5 votes |
/** * Commits the file. * * @throws StorageException * An exception representing any error which occurred during the operation. * @throws URISyntaxException */ @DoesServiceRequest private void commit() throws StorageException, URISyntaxException { if (this.options.getStoreFileContentMD5()) { this.parentFileRef.getProperties().setContentMD5(Base64.encode(this.md5Digest.digest())); } this.parentFileRef.uploadProperties(this.accessCondition, this.options, this.opContext); }
Example #12
Source File: BlobOutputStream.java From azure-storage-android with Apache License 2.0 | 5 votes |
/** * 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 #13
Source File: AzureStorageServiceTests.java From crate with Apache License 2.0 | 4 votes |
private static Settings buildClientCredSettings() { return Settings.builder() .put("account", "myaccount1") .put("key", Base64.encode("mykey1".getBytes(StandardCharsets.UTF_8))) .build(); }
Example #14
Source File: FileInputStream.java From azure-storage-android with Apache License 2.0 | 4 votes |
/** * 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 #15
Source File: BlobInputStream.java From azure-storage-android with Apache License 2.0 | 4 votes |
/** * 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 #16
Source File: CloudFileTests.java From azure-storage-android with Apache License 2.0 | 4 votes |
@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 #17
Source File: TestBlobDataValidation.java From big-c with Apache License 2.0 | 4 votes |
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 #18
Source File: TestBlobDataValidation.java From hadoop with Apache License 2.0 | 4 votes |
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 #19
Source File: StorageCredentialsAccountAndKey.java From azure-storage-android with Apache License 2.0 | 2 votes |
/** * Sets the name of the access key to be used when signing the request. * * @param key * A <code>String</code> that represents the name of the access key to be used when signing the request. */ public synchronized void updateKey(final String key) { this.updateKey(Base64.decode(key)); }
Example #20
Source File: StorageCredentialsAccountAndKey.java From azure-storage-android with Apache License 2.0 | 2 votes |
/** * 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 #21
Source File: StorageCredentialsAccountAndKey.java From azure-storage-android with Apache License 2.0 | 2 votes |
/** * Creates an instance of the <code>StorageCredentialsAccountAndKey</code> class, using the specified storage * account name and access key; the specified access key is stored as a <code>String</code>. * * @param accountName * A <code>String</code> that represents the name of the storage account. * @param key * A <code>String</code> that represents the Base-64-encoded account access key. */ public StorageCredentialsAccountAndKey(final String accountName, final String key) { this(accountName, Base64.decode(key)); }
Example #22
Source File: EntityProperty.java From azure-storage-android with Apache License 2.0 | 2 votes |
/** * Gets the value of this {@link EntityProperty} as a <code>byte</code> array. * * @return * A <code>byte[]</code> representation of the {@link EntityProperty} value, or <code>null</code>. */ public byte[] getValueAsByteArray() { return this.getIsNull() ? null : Base64.decode(this.value); }
Example #23
Source File: EntityProperty.java From azure-storage-android with Apache License 2.0 | 2 votes |
/** * Gets the value of this {@link EntityProperty} as a <code>Byte</code> array. * * @return * A <code>Byte[]</code> representation of the {@link EntityProperty} value, or <code>null</code>. */ public Byte[] getValueAsByteObjectArray() { return this.getIsNull() ? null : Base64.decodeAsByteObjectArray(this.value); }
Example #24
Source File: EntityProperty.java From azure-storage-android with Apache License 2.0 | 2 votes |
/** * 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 #25
Source File: EntityProperty.java From azure-storage-android with Apache License 2.0 | 2 votes |
/** * 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 #26
Source File: AzureBlobStorageTestAccount.java From big-c with Apache License 2.0 | 2 votes |
/** * Sets the mock account key in the given configuration. * * @param conf * The configuration. */ public static void setMockAccountKey(Configuration conf, String accountName) { conf.set(ACCOUNT_KEY_PROPERTY_NAME + accountName, Base64.encode(new byte[] { 1, 2, 3 })); }
Example #27
Source File: AzureBlobStorageTestAccount.java From hadoop with Apache License 2.0 | 2 votes |
/** * Sets the mock account key in the given configuration. * * @param conf * The configuration. */ public static void setMockAccountKey(Configuration conf, String accountName) { conf.set(ACCOUNT_KEY_PROPERTY_NAME + accountName, Base64.encode(new byte[] { 1, 2, 3 })); }