}
Access access = new Access();
/* Get the metadata for the given file */
GetObjectResult result = null;
/* Try a few times to get the object */
/* Put the file, We'll try a few times */
int attemptCount = 0;
while (attemptCount < MAX_S3_READWRITE_ATTEMPTS)
{
attemptCount++;
try
{
long startTime = System.currentTimeMillis();
AmazonS3_ServiceLocator locator = new AmazonS3_ServiceLocator();
AmazonS3SoapBindingStub binding = new AmazonS3SoapBindingStub(new URL(locator.getAmazonS3Address()), locator);
result = binding.getObject(getBucketName(),
key,
false,
true,
false,
access.getAccessKey(),
access.getAccessCalendar(),
access.generateSignature("GetObject"),
null);
long endTime = System.currentTimeMillis();
/* Get the attachments. Note, the getAttachments() method will ONLY return the object[] on the first call. Subsiquent calls will return null */
Object[] attachments = binding.getAttachments();
if (attachments.length != 1)
{
throw new Exception("The S3 Object returned [" + attachments.length + "] when we expected exactly 1");
}
/* Setup the MD5 digest */
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
/* Get the attachment, and pipe it's data to the buffer */
OutputStream os = createBufferOutputStream();
AttachmentPart part = (AttachmentPart) attachments[0];
InputStream attachmentStream = part.getDataHandler().getInputStream();
long byteCount = 0;
int b = 0;
while ((b = attachmentStream.read()) != -1)
{
byteCount++;
messageDigest.update((byte)b);
os.write(b);
if (byteCount % 1000 == 0)
{
Log.info("\r" + byteCount + " bytes read...");
}
}
Log.info("\r" + byteCount + " bytes read...");
os.flush();
os.close();
this.kBytesProcessed_ += ((double)byteCount / 1000.0);
Log.info(String.format("%6.02f Kb/s\n", (((double)((double)byteCount * (double)Byte.SIZE)) / 1000D) / ((endTime - startTime) / 1000)));
Log.debug(byteCount + " bytes written to buffer\n");
/* Calculate the MD5 value */
String md5 = Common.toHex(messageDigest.digest());
/* compare md5 hashes */
if (md5.equals(result.getETag().replaceAll("\"", "")) == false)
{
throw new Exception("After getting the S3 object [" + key + "], we compared the md5 hash codes. They did not match\n" + "original: [" + md5 + "]\nS3: [" + result.getETag() + "]");
}
/* Now, stream the file to stdout */
byteCount = 0;
InputStream is = createBufferInputStream();