> +   }
> +
> +   /**
> +    * Build the Hash and the TreeHash values of the payload.
> +    */
> +   public void buildHashes() throws IOException {
> +      InputStream is = payload.openStream();
> +      Hasher hasher = Hashing.sha256().newHasher();
> +      Queue<HashCode> q = Lists.newLinkedList();
> +
> +      //Divide the payload in chunks and queue them.
> +      byte[] buffer = new byte[CHUNK_SIZE];
> +      int bytesRead;
> +      while ((bytesRead = is.read(buffer)) >= 0) {
> +         q.offer(Hashing.sha256().hashBytes(buffer, 0, bytesRead));
> +         hasher.putBytes(buffer, 0, bytesRead);

Try chaining the `HashedInputStream` for efficiency:

```
HashingInputStream linearHis = new HashingInputStream(Hashing.sha256(), is);    
                                                                               
while (true) {                                                                  
                                                                               
    HashingInputStream.chunkedHis = new HashingInputStream(                     
                                                                               
            Hashing.sha256(), ByteStreams.limit(linearHis, CHUNK_SIZE));        
                                                                               
    int count = ByteStreams.copy(chunkedHis, ByteStreams.nullOutputStream());   
                                                                               
    if (count == 0) {                                                           
                                                                               
        break;                                                                  
                                                                               
    }                                                                           
                                                                               
    q.offer(chunkedHis.hash());                                        
} 
```

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-labs-aws/pull/9/files#r13771573

Reply via email to