Coverage Summary for Class: HashingInputStream (com.google.common.hash)

Class Class, % Method, % Line, %
HashingInputStream 0% (0/1) 0% (0/6) 0% (0/13)


1 /* 2  * Copyright (C) 2013 The Guava Authors 3  * 4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5  * in compliance with the License. You may obtain a copy of the License at 6  * 7  * http://www.apache.org/licenses/LICENSE-2.0 8  * 9  * Unless required by applicable law or agreed to in writing, software distributed under the License 10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11  * or implied. See the License for the specific language governing permissions and limitations under 12  * the License. 13  */ 14  15 package com.google.common.hash; 16  17 import static com.google.common.base.Preconditions.checkNotNull; 18  19 import com.google.common.annotations.Beta; 20 import com.google.errorprone.annotations.CanIgnoreReturnValue; 21 import java.io.FilterInputStream; 22 import java.io.IOException; 23 import java.io.InputStream; 24  25 /** 26  * An {@link InputStream} that maintains a hash of the data read from it. 27  * 28  * @author Qian Huang 29  * @since 16.0 30  */ 31 @Beta 32 @ElementTypesAreNonnullByDefault 33 public final class HashingInputStream extends FilterInputStream { 34  private final Hasher hasher; 35  36  /** 37  * Creates an input stream that hashes using the given {@link HashFunction} and delegates all data 38  * read from it to the underlying {@link InputStream}. 39  * 40  * <p>The {@link InputStream} should not be read from before or after the hand-off. 41  */ 42  public HashingInputStream(HashFunction hashFunction, InputStream in) { 43  super(checkNotNull(in)); 44  this.hasher = checkNotNull(hashFunction.newHasher()); 45  } 46  47  /** 48  * Reads the next byte of data from the underlying input stream and updates the hasher with the 49  * byte read. 50  */ 51  @Override 52  @CanIgnoreReturnValue 53  public int read() throws IOException { 54  int b = in.read(); 55  if (b != -1) { 56  hasher.putByte((byte) b); 57  } 58  return b; 59  } 60  61  /** 62  * Reads the specified bytes of data from the underlying input stream and updates the hasher with 63  * the bytes read. 64  */ 65  @Override 66  @CanIgnoreReturnValue 67  public int read(byte[] bytes, int off, int len) throws IOException { 68  int numOfBytesRead = in.read(bytes, off, len); 69  if (numOfBytesRead != -1) { 70  hasher.putBytes(bytes, off, numOfBytesRead); 71  } 72  return numOfBytesRead; 73  } 74  75  /** 76  * mark() is not supported for HashingInputStream 77  * 78  * @return {@code false} always 79  */ 80  @Override 81  public boolean markSupported() { 82  return false; 83  } 84  85  /** mark() is not supported for HashingInputStream */ 86  @Override 87  public void mark(int readlimit) {} 88  89  /** 90  * reset() is not supported for HashingInputStream. 91  * 92  * @throws IOException this operation is not supported 93  */ 94  @Override 95  public void reset() throws IOException { 96  throw new IOException("reset not supported"); 97  } 98  99  /** 100  * Returns the {@link HashCode} based on the data read from this stream. The result is unspecified 101  * if this method is called more than once on the same instance. 102  */ 103  public HashCode hash() { 104  return hasher.hash(); 105  } 106 }