Source:
public static void downloadToFile(S3Object s3Object, final File dstfile, boolean performIntegrityCheck, final boolean appendData, final long expectedFileLength) { createParentDirectoryIfNecessary(dstfile); if (!FileLocks.lock(dstfile)) { throw new FileLockException("Fail to lock " + dstfile + " for appendData=" + appendData); } OutputStream outputStream = null; try { final long actualLen = dstfile.length(); if (appendData && actualLen != expectedFileLength) { throw new IllegalStateException( "Expected file length to append is " + expectedFileLength + " but actual length is " + actualLen + " for file " + dstfile); } outputStream = new BufferedOutputStream(new FileOutputStream( dstfile, appendData)); byte[] buffer = new byte[8192]; int bytesRead; while ((bytesRead = s3Object.getObjectContent().read(buffer)) > -1) { outputStream.write(buffer, 0, bytesRead); } } catch (IOException e) { s3Object.getObjectContent().abort(); throw new SdkClientException( "Unable to store object contents to disk: " + e.getMessage(), e); } finally { closeQuietly(outputStream, LOG); FileLocks.unlock(dstfile); closeQuietly(s3Object.getObjectContent(), LOG); } if (performIntegrityCheck) { byte[] clientSideHash = null; byte[] serverSideHash = null; try { final ObjectMetadata metadata = s3Object.getObjectMetadata(); if (!skipMd5CheckStrategy.skipClientSideValidationPerGetResponse(metadata)) { clientSideHash = Md5Utils.computeMD5Hash(new FileInputStream(dstfile)); serverSideHash = BinaryUtils.fromHex(metadata.getETag()); } } catch (Exception e) { LOG.warn("Unable to calculate MD5 hash to validate download: " + e.getMessage(), e); } if (clientSideHash != null && serverSideHash != null && !Arrays.equals(clientSideHash, serverSideHash)) { throw new SdkClientException("Unable to verify integrity of data download. " + "Client calculated content hash didn't match hash calculated by Amazon S3. " + "The data stored in '" + dstfile.getAbsolutePath() + "' may be corrupt."); } } }

Target:
public static void downloadToFile(S3Object s3Object, final File dstfile, boolean performIntegrityCheck, final boolean appendData, final long expectedFileLength) { createParentDirectoryIfNecessary(dstfile); if (!FileLocks.lock(dstfile)) { throw new FileLockException("Fail to lock " + dstfile + " for appendData=" + appendData); } OutputStream outputStream = null; try { final long actualLen = dstfile.length(); if (appendData && actualLen != expectedFileLength) { throw new IllegalStateException( "Expected file length to append is " + expectedFileLength + " but actual length is " + actualLen + " for file " + dstfile); } outputStream = new BufferedOutputStream(new FileOutputStream( dstfile, appendData)); byte[] buffer = new byte[DEFAULT_BUFFER_SIZE]; int bytesRead; while ((bytesRead = s3Object.getObjectContent().read(buffer)) > -1) { outputStream.write(buffer, 0, bytesRead); } } catch (IOException e) { s3Object.getObjectContent().abort(); throw new SdkClientException( "Unable to store object contents to disk: " + e.getMessage(), e); } finally { closeQuietly(outputStream, LOG); FileLocks.unlock(dstfile); closeQuietly(s3Object.getObjectContent(), LOG); } if (performIntegrityCheck) { byte[] clientSideHash = null; byte[] serverSideHash = null; try { final ObjectMetadata metadata = s3Object.getObjectMetadata(); if (!skipMd5CheckStrategy.skipClientSideValidationPerGetResponse(metadata)) { clientSideHash = Md5Utils.computeMD5Hash(new FileInputStream(dstfile)); serverSideHash = BinaryUtils.fromHex(metadata.getETag()); } } catch (Exception e) { LOG.warn("Unable to calculate MD5 hash to validate download: " + e.getMessage(), e); } if (clientSideHash != null && serverSideHash != null && !Arrays.equals(clientSideHash, serverSideHash)) { throw new SdkClientException("Unable to verify integrity of data download. " + "Client calculated content hash didn't match hash calculated by Amazon S3. " + "The data stored in '" + dstfile.getAbsolutePath() + "' may be corrupt."); } } }