本文整理汇总了Java中com.microsoft.azure.storage.core.Base64.encode方法的典型用法代码示例。如果您正苦于以下问题:Java Base64.encode方法的具体用法?Java Base64.encode怎么用?Java Base64.encode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.microsoft.azure.storage.core.Base64
的用法示例。
在下文中一共展示了Base64.encode方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: computeMacSha256
import com.microsoft.azure.storage.core.Base64; //导入方法依赖的package包/类
/**
* Computes a signature for the specified string using the HMAC-SHA256 algorithm.
*
* @param storageKey
* A <code>StorageKey</code> object that represents the storage key to use.
* @param stringToSign
* The UTF-8-encoded string to sign.
*
* @return A <code>String</code> that contains the HMAC-SHA256-encoded signature.
*
* @throws IllegalArgumentException
* If the string to sign is not a valid Base64-encoded string.
* @throws InvalidKeyException
* If the key is not a valid storage key.
*/
public static synchronized String computeMacSha256(final StorageKey storageKey, final String stringToSign)
throws InvalidKeyException {
if (storageKey.hmacSha256 == null) {
storageKey.initHmacSha256();
}
byte[] utf8Bytes = null;
try {
utf8Bytes = stringToSign.getBytes(Constants.UTF8_CHARSET);
}
catch (final UnsupportedEncodingException e) {
throw new IllegalArgumentException(e);
}
return Base64.encode(storageKey.hmacSha256.doFinal(utf8Bytes));
}
示例2: computeMacSha512
import com.microsoft.azure.storage.core.Base64; //导入方法依赖的package包/类
/**
* Computes a signature for the specified string using the HMAC-SHA512 algorithm.
*
* @param storageKey
* A <code>StorageKey</code> object that represents the storage key to use.
* @param stringToSign
* The UTF-8-encoded string to sign.
*
* @return A <code>String</code> that contains the HMAC-SHA512-encoded signature.
*
* @throws IllegalArgumentException
* If the string to sign is not a valid Base64-encoded string.
* @throws InvalidKeyException
* If the key is not a valid storage key.
*/
public static synchronized String computeMacSha512(final StorageKey storageKey, final String stringToSign)
throws InvalidKeyException {
if (storageKey.hmacSha512 == null) {
storageKey.initHmacSha512();
}
byte[] utf8Bytes = null;
try {
utf8Bytes = stringToSign.getBytes(Constants.UTF8_CHARSET);
}
catch (final UnsupportedEncodingException e) {
throw new IllegalArgumentException(e);
}
return Base64.encode(storageKey.hmacSha512.doFinal(utf8Bytes));
}
示例3: getMessageContentForTransfer
import com.microsoft.azure.storage.core.Base64; //导入方法依赖的package包/类
/**
* 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;
}
示例4: testStorageCredentialsSharedKey
import com.microsoft.azure.storage.core.Base64; //导入方法依赖的package包/类
@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());
}
示例5: testStorageCredentialsSharedKeyUpdateKey
import com.microsoft.azure.storage.core.Base64; //导入方法依赖的package包/类
@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());
}
示例6: testQueueAddUpdateEncryptedEncodedMessage
import com.microsoft.azure.storage.core.Base64; //导入方法依赖的package包/类
@Test
public void testQueueAddUpdateEncryptedEncodedMessage() throws StorageException, InvalidKeyException,
NoSuchAlgorithmException, NoSuchPaddingException {
// Create the Key to be used for wrapping.
SymmetricKey aesKey = TestHelper.getSymmetricKey();
byte[] messageBytes = new byte[100];
Random rand = new Random();
rand.nextBytes(messageBytes);
String inputMessage = Base64.encode(messageBytes);
CloudQueueMessage message = new CloudQueueMessage(inputMessage);
this.queue.setShouldEncodeMessage(false);
QueueRequestOptions options = new QueueRequestOptions();
options.setEncryptionPolicy(new QueueEncryptionPolicy(aesKey, null));
// add message
this.queue.addMessage(message, 0, 0, options, null);
// Retrieve message
CloudQueueMessage retrMessage = this.queue.retrieveMessage(30, options, null);
assertEquals(inputMessage, retrMessage.getMessageContentAsString());
}
示例7: uploadRange
import com.microsoft.azure.storage.core.Base64; //导入方法依赖的package包/类
/**
* Uploads a range to a file using the specified lease ID, request options, and operation context.
*
* @param sourceStream
* An {@link IntputStream} object which represents the input stream to write to the file.
* @param offset
* A <code>long</code> which represents the offset, in number of bytes, at which to begin writing the
* data.
* @param length
* A <code>long</code> which represents the length, in bytes, of the data to write.
* @param accessCondition
* An {@link AccessCondition} object which represents the access conditions for the file.
* @param options
* A {@link FileRequestOptions} object that specifies any additional options for the request. Specifying
* <code>null</code> will use the default request options from the associated service client (
* {@link CloudFileClient}).
* @param opContext
* An {@link OperationContext} object which represents the context for the current operation. This object
* is used to track requests to the storage service, and to provide additional runtime information about
* the operation.
*
* @throws IOException
* If an I/O exception occurred.
* @throws StorageException
* If a storage service error occurred.
*/
@DoesServiceRequest
public void uploadRange(final InputStream sourceStream, final long offset, final long length,
final AccessCondition accessCondition, FileRequestOptions options, OperationContext opContext)
throws StorageException, IOException {
if (opContext == null) {
opContext = new OperationContext();
}
options = FileRequestOptions.applyDefaults(options, this.fileServiceClient);
final FileRange range = new FileRange(offset, offset + length - 1);
final byte[] data = new byte[(int) length];
String md5 = null;
int count = 0;
int total = 0;
while (total < length) {
count = sourceStream.read(data, total, (int) Math.min(length - total, Integer.MAX_VALUE));
total += count;
}
if (options.getUseTransactionalContentMD5()) {
try {
final MessageDigest digest = MessageDigest.getInstance("MD5");
digest.update(data, 0, data.length);
md5 = Base64.encode(digest.digest());
}
catch (final NoSuchAlgorithmException e) {
// This wont happen, throw fatal.
throw Utility.generateNewUnexpectedStorageException(e);
}
}
this.putRangeInternal(range, FileRangeOperationType.UPDATE, data, length, md5, accessCondition, options,
opContext);
}
示例8: setValue
import com.microsoft.azure.storage.core.Base64; //导入方法依赖的package包/类
/**
* 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;
if (value == null) {
this.value = null;
this.isNull = true;
return;
}
else {
this.isNull = false;
}
this.value = Base64.encode(value);
}
示例9: setMessageContent
import com.microsoft.azure.storage.core.Base64; //导入方法依赖的package包/类
/**
* 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;
}
示例10: uploadRange
import com.microsoft.azure.storage.core.Base64; //导入方法依赖的package包/类
/**
* Uploads a range to a file using the specified lease ID, request options, and operation context.
*
* @param sourceStream
* An {@link InputStream} object which represents the input stream to write to the file.
* @param offset
* A <code>long</code> which represents the offset, in number of bytes, at which to begin writing the
* data.
* @param length
* A <code>long</code> which represents the length, in bytes, of the data to write.
* @param accessCondition
* An {@link AccessCondition} object which represents the access conditions for the file.
* @param options
* A {@link FileRequestOptions} object that specifies any additional options for the request. Specifying
* <code>null</code> will use the default request options from the associated service client (
* {@link CloudFileClient}).
* @param opContext
* An {@link OperationContext} object which represents the context for the current operation. This object
* is used to track requests to the storage service, and to provide additional runtime information about
* the operation.
*
* @throws IOException
* If an I/O exception occurred.
* @throws StorageException
* If a storage service error occurred.
* @throws URISyntaxException
*/
@DoesServiceRequest
public void uploadRange(final InputStream sourceStream, final long offset, final long length,
final AccessCondition accessCondition, FileRequestOptions options, OperationContext opContext)
throws StorageException, IOException, URISyntaxException {
if (opContext == null) {
opContext = new OperationContext();
}
this.getShare().assertNoSnapshot();
options = FileRequestOptions.populateAndApplyDefaults(options, this.fileServiceClient);
final FileRange range = new FileRange(offset, offset + length - 1);
final byte[] data = new byte[(int) length];
String md5 = null;
int count = 0;
int total = 0;
while (total < length) {
count = sourceStream.read(data, total, (int) Math.min(length - total, Integer.MAX_VALUE));
total += count;
}
if (options.getUseTransactionalContentMD5()) {
try {
final MessageDigest digest = MessageDigest.getInstance("MD5");
digest.update(data, 0, data.length);
md5 = Base64.encode(digest.digest());
}
catch (final NoSuchAlgorithmException e) {
// This wont happen, throw fatal.
throw Utility.generateNewUnexpectedStorageException(e);
}
}
this.putRangeInternal(range, FileRangeOperationType.UPDATE, data, length, md5, accessCondition, options,
opContext);
}
示例11: getCurrentBlockId
import com.microsoft.azure.storage.core.Base64; //导入方法依赖的package包/类
/**
* 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);
}
示例12: getMessageContentForTransfer
import com.microsoft.azure.storage.core.Base64; //导入方法依赖的package包/类
/**
* Gets the content of the message for transfer (internal use only).
*
* @param shouldEncodeMessage
* Indicates if the message should be encoded.
* @param options
* A {@link QueueRequestOptions} object that specifies additional options for the request.
*
* @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, QueueRequestOptions options)
throws StorageException {
String result = null;
if (options.getEncryptionPolicy() != null)
{
// Create an encrypted message that will hold the message contents along with encryption related metadata and return it.
// The encrypted message is already Base 64 encoded. So no need to process further in this method.
String encryptedMessageString = options.getEncryptionPolicy().encryptMessage(this.getMessageContentAsByte());
// the size of Base64 encoded string is the number of bytes this message will take up on server.
if (encryptedMessageString.length() > QueueConstants.MAX_MESSAGE_SIZE)
{
throw new IllegalArgumentException(
String.format(SR.ENCRYPTED_MESSAGE_TOO_LARGE, QueueConstants.MAX_MESSAGE_SIZE));
}
return encryptedMessageString;
}
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;
}
示例13: testStoreBlobMd5
import com.microsoft.azure.storage.core.Base64; //导入方法依赖的package包/类
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));
}
}
示例14: readInternal
import com.microsoft.azure.storage.core.Base64; //导入方法依赖的package包/类
/**
* 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;
}
示例15: readInternal
import com.microsoft.azure.storage.core.Base64; //导入方法依赖的package包/类
/**
* 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;
}