Example usage for com.amazonaws.util Base16Lower encodeAsString

List of usage examples for com.amazonaws.util Base16Lower encodeAsString

Introduction

In this page you can find the example usage for com.amazonaws.util Base16Lower encodeAsString.

Prototype

public static String encodeAsString(byte... bytes) 

Source Link

Document

Returns a base 16 encoded string (in lower case) of the given bytes.

Usage

From source file:com.netflix.hollow.example.consumer.infrastructure.S3BlobRetriever.java

License:Apache License

private File downloadFile(String objectName) throws IOException {
    for (int retryCount = 0; retryCount < 3; retryCount++) {
        try {/* ww  w .  j  av  a 2  s. com*/
            File tempFile = new File(System.getProperty("java.io.tmpdir"), objectName.replace('/', '-'));

            S3Object s3Object = s3.getObject(bucketName, objectName);

            MessageDigest md = MessageDigest.getInstance("MD5");
            try (InputStream is = new DigestInputStream(s3Object.getObjectContent(), md);
                    OutputStream os = new FileOutputStream(tempFile)) {
                IOUtils.copy(is, os);
            }

            String expectedMD5 = s3Object.getObjectMetadata().getETag();
            String actualMD5 = Base16Lower.encodeAsString(md.digest());

            if (!actualMD5.equals(expectedMD5))
                throw new IOException("MD5 sum did not match expected!");

            return tempFile;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    throw new IOException("Unable to successfully retrieve stream from S3 after 3 retries");
}