Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2012-2015 Amazon Technologies, Inc.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at:
   *
   *    http://aws.amazon.com/apache2.0
   *
  * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
  * OR CONDITIONS OF ANY KIND, either express or implied. See the
  * License for the specific language governing permissions and
  * limitations under the License.
  */
 package com.amazonaws.services.glacier.internal;
 
 import java.util.List;
 
Filter input stream that enables tree hash computation on the bytes that it streams.
 
 public class TreeHashInputStream extends SdkFilterInputStream {
 
         super(new SdkDigestInputStream(in, MessageDigest.getInstance("SHA-256")));
         this. = (DigestInputStreamsuper.;
     }

    
The private DigestInputStream we use to wrap the source input stream and compute a client-side checksum for every part of the message.
 
     private final DigestInputStream digestInputStream;

    
The checksums of megabyte-sized parts of the bytes streamed.
 
     private final List<byte[]> checksums = new ArrayList<byte[]>();
 
     private boolean closed = false;

    
Returns the list of one-megabyte checksums for this input stream. Only valid once the stream has been closed.

Throws:
java.io.IOException If the stream hasn't been closed.
 
     public List<byte[]> getChecksums() throws IOException {
         if ( ! ) {
             throw new IOException("Stream must be closed before getting checksums");
         }
         return Collections.unmodifiableList();
     }
 
     public String getTreeHash() throws IOException {
         if ( ! ) {
             throw new IOException("Stream must be closed before getting the tree hash");
         }
         return TreeHashGenerator.calculateTreeHash(getChecksums());
     }
 
     private int byteOffset = 0;
     private static final int MB = 1024 * 1024;
 
     @Override
     public int read() throws IOException {
         int read = super.read();
         if ( read >= 0 )
             ++;
 
         digestPart();
         return read;
     }
 
     @Override
     public int read(byte[] bint offint lenthrows IOException {
         int totalBytesRead = 0;
         int lastBytesRead = 0;
         while ( totalBytesRead < len && lastBytesRead > -1 ) {
             int bytesToRead = Math.min( - len - totalBytesRead);
 
             lastBytesRead = super.read(b, off + totalBytesReadbytesToRead);
             if ( lastBytesRead > 0 ) {
                 totalBytesRead += lastBytesRead;
                  += lastBytesRead;
             }
            digestPart();
            
            if (lastBytesRead == -1 && totalBytesRead == 0) return -1;
        }
        return totalBytesRead;
    }

    
Digests the current part of the message, if necessary, and resets digest state.
    private void digestPart() {
        if (  >=  ) {
             = 0;
            .getMessageDigest().reset();
        }
    }
    @Override
    public void close() throws IOException {
        super.close();
         = true;
        if (  > 0 ) {
        }
    }
New to GrepCode? Check out our FAQ X