private void downloadOneChunk(String accountId, String vaultName, String jobId, RandomAccessFile output, long currentPosition, long endPosition) {
TreeHashInputStream input;
int retries = 0;
while (true) {
try {
GetJobOutputResult jobOutputResult = glacier.getJobOutput(new GetJobOutputRequest()
.withAccountId(accountId)
.withVaultName(vaultName)
.withRange("bytes=" + Long.toString(currentPosition) + "-" + Long.toString(endPosition))
.withJobId(jobId));
try {
input = new TreeHashInputStream(new BufferedInputStream(jobOutputResult.getBody()));
} catch (NoSuchAlgorithmException e) {
throw new AmazonClientException("Unable to compute hash for data integrity: " + e.getMessage(), e);
}
appendToFile(output, input);
// Only do tree-hash check when the output checksum is returned from Glacier
if (null != jobOutputResult.getChecksum()) {
// Checksum does not match
if (!input.getTreeHash().equalsIgnoreCase(jobOutputResult.getChecksum())) {
throw new IOException("Client side computed hash doesn't match server side hash; possible data corruption");
}
} else {
log.warn("Cannot validate the downloaded output since no tree-hash checksum is returned from Glacier. "
+ "Make sure the InitiateJob and GetJobOutput requests use tree-hash-aligned ranges.");