Java Examples for com.microsoft.azure.storage.core.Base64

The following java examples will help you to understand the usage of com.microsoft.azure.storage.core.Base64. These source code samples are taken from different open source projects.

Example 1
Project: data-pipeline-storm-master  File: BlobWriter.java View source code
public static void upload(String blobname, String blockIdStr, String data) {
    InputStream stream = null;
    try {
        if (LogSetting.LOG_BLOBWRITER) {
            logger.info("upload Begin");
            logger.info("upload blobname = " + blobname);
            logger.info("upload blockIdStr = " + blockIdStr);
        }
        if (LogSetting.LOG_BLOBWRITERDATA) {
            logger.info("upload data= \r\n" + data);
        }
        CloudBlockBlob blockBlob = container.getBlockBlobReference(blobname);
        BlobRequestOptions blobOptions = new BlobRequestOptions();
        stream = new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8));
        BlockEntry newBlock = new BlockEntry(Base64.encode(blockIdStr.getBytes()), BlockSearchMode.UNCOMMITTED);
        ArrayList<BlockEntry> blocksBeforeUpload = new ArrayList<BlockEntry>();
        if (blockBlob.exists(AccessCondition.generateEmptyCondition(), blobOptions, null)) {
            blocksBeforeUpload = blockBlob.downloadBlockList(BlockListingFilter.COMMITTED, null, blobOptions, null);
        }
        if (LogSetting.LOG_BLOBWRITER) {
            int i = 0;
            String id = null;
            for (BlockEntry e : blocksBeforeUpload) {
                i++;
                id = e.getId();
                logger.info("BlockEntry Before Upload id=" + id + ", Index = " + i);
            }
            if (id != null) {
                logger.info("BlockEntry Before Upload id=" + id + ", Index = " + i + " --last before");
            }
        }
        blockBlob.uploadBlock(newBlock.getId(), stream, -1);
        if (!blocksBeforeUpload.contains(newBlock)) {
            blocksBeforeUpload.add(newBlock);
        }
        if (LogSetting.LOG_BLOBWRITER) {
            int i = 0;
            String id = null;
            for (BlockEntry e : blocksBeforeUpload) {
                i++;
                id = e.getId();
                logger.info("BlockEntry After Upload id=" + id + ", Index = " + i);
            }
            if (id != null) {
                logger.info("BlockEntry After Upload id=" + id + ", Index = " + i + " --last after");
            }
        }
        blockBlob.commitBlockList(blocksBeforeUpload);
    } catch (Exception e) {
        throw new FailedException(e.getMessage());
    } finally {
        if (stream != null) {
            try {
                stream.close();
            } catch (Exception e) {
                logger.error("failed to close the stream that upload to azrue blob");
            }
        }
    }
    if (LogSetting.LOG_BLOBWRITER) {
        logger.info("upload End");
    }
}
Example 2
Project: athere-android-master  File: CloudBlob.java View source code
@Override
public Integer postProcessResponse(HttpURLConnection connection, CloudBlob blob, CloudBlobClient client, OperationContext context, Integer storageObject) throws Exception {
    final String contentLength = connection.getHeaderField(Constants.HeaderConstants.CONTENT_LENGTH);
    final long expectedLength = Long.parseLong(contentLength);
    Logger.info(context, String.format(SR.CREATING_NETWORK_STREAM, expectedLength));
    final NetworkInputStream sourceStream = new NetworkInputStream(connection.getInputStream(), expectedLength);
    try {
        int totalRead = 0;
        int nextRead = buffer.length - bufferOffset;
        int count = sourceStream.read(buffer, bufferOffset, nextRead);
        while (count > 0) {
            // if maximum execution time would be exceeded
            if (Utility.validateMaxExecutionTimeout(options.getOperationExpiryTimeInMs())) {
                // throw an exception
                TimeoutException timeoutException = new TimeoutException(SR.MAXIMUM_EXECUTION_TIMEOUT_EXCEPTION);
                throw Utility.initIOException(timeoutException);
            }
            totalRead += count;
            this.setCurrentRequestByteCount(this.getCurrentRequestByteCount() + count);
            nextRead = buffer.length - (bufferOffset + totalRead);
            if (nextRead == 0) {
                // check for case where more data is returned
                if (sourceStream.read(new byte[1], 0, 1) != -1) {
                    throw new StorageException(StorageErrorCodeStrings.OUT_OF_RANGE_INPUT, SR.CONTENT_LENGTH_MISMATCH, Constants.HeaderConstants.HTTP_UNUSED_306, null, null);
                }
            }
            count = sourceStream.read(buffer, bufferOffset + totalRead, nextRead);
        }
        if (totalRead != expectedLength) {
            throw new StorageException(StorageErrorCodeStrings.OUT_OF_RANGE_INPUT, SR.CONTENT_LENGTH_MISMATCH, Constants.HeaderConstants.HTTP_UNUSED_306, null, null);
        }
    } finally {
        // Close the stream. Closing an already closed stream is harmless. So its fine to try
        // to drain the response and close the stream again in the executor.
        sourceStream.close();
    }
    final Boolean validateMD5 = !options.getDisableContentMD5Validation() && !Utility.isNullOrEmpty(this.getContentMD5());
    if (validateMD5) {
        try {
            final MessageDigest digest = MessageDigest.getInstance("MD5");
            digest.update(buffer, bufferOffset, (int) this.getCurrentRequestByteCount());
            final String calculatedMD5 = Base64.encode(digest.digest());
            if (!this.getContentMD5().equals(calculatedMD5)) {
                throw new StorageException(StorageErrorCodeStrings.INVALID_MD5, String.format(SR.BLOB_HASH_MISMATCH, this.getContentMD5(), calculatedMD5), Constants.HeaderConstants.HTTP_UNUSED_306, null, null);
            }
        } catch (final NoSuchAlgorithmException e) {
            throw Utility.generateNewUnexpectedStorageException(e);
        }
    }
    return (int) this.getCurrentRequestByteCount();
}
Example 3
Project: hadoop-release-2.6.0-master  File: TestBlobDataValidation.java View source code
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 4
Project: azure-storage-java-master  File: CloudFile.java View source code
/**
     * 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) {
            throw Utility.generateNewUnexpectedStorageException(e);
        }
    }
    this.putRangeInternal(range, FileRangeOperationType.UPDATE, data, length, md5, accessCondition, options, opContext);
}
Example 5
Project: azure-storage-android-master  File: CloudFile.java View source code
/**
     * 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.
     */
@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.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) {
            throw Utility.generateNewUnexpectedStorageException(e);
        }
    }
    this.putRangeInternal(range, FileRangeOperationType.UPDATE, data, length, md5, accessCondition, options, opContext);
}
Example 6
Project: hadoop-master  File: TestBlobDataValidation.java View source code
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
Project: camel-master  File: QueueServiceUtilTest.java View source code
private StorageCredentials newAccountKeyCredentials() {
    return new StorageCredentialsAccountAndKey("camelazure", Base64.encode("key".getBytes()));
}