001/*
002 * Copyright (C) 2013 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005 * in compliance with the License. You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software distributed under the License
010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011 * or implied. See the License for the specific language governing permissions and limitations under
012 * the License.
013 */
014
015package com.google.common.hash;
016
017import static com.google.common.base.Preconditions.checkNotNull;
018
019import com.google.common.annotations.Beta;
020import com.google.errorprone.annotations.CanIgnoreReturnValue;
021import java.io.FilterInputStream;
022import java.io.IOException;
023import java.io.InputStream;
024
025/**
026 * An {@link InputStream} that maintains a hash of the data read from it.
027 *
028 * @author Qian Huang
029 * @since 16.0
030 */
031@Beta
032@ElementTypesAreNonnullByDefault
033public final class HashingInputStream extends FilterInputStream {
034  private final Hasher hasher;
035
036  /**
037   * Creates an input stream that hashes using the given {@link HashFunction} and delegates all data
038   * read from it to the underlying {@link InputStream}.
039   *
040   * <p>The {@link InputStream} should not be read from before or after the hand-off.
041   */
042  public HashingInputStream(HashFunction hashFunction, InputStream in) {
043    super(checkNotNull(in));
044    this.hasher = checkNotNull(hashFunction.newHasher());
045  }
046
047  /**
048   * Reads the next byte of data from the underlying input stream and updates the hasher with the
049   * byte read.
050   */
051  @Override
052  @CanIgnoreReturnValue
053  public int read() throws IOException {
054    int b = in.read();
055    if (b != -1) {
056      hasher.putByte((byte) b);
057    }
058    return b;
059  }
060
061  /**
062   * Reads the specified bytes of data from the underlying input stream and updates the hasher with
063   * the bytes read.
064   */
065  @Override
066  @CanIgnoreReturnValue
067  public int read(byte[] bytes, int off, int len) throws IOException {
068    int numOfBytesRead = in.read(bytes, off, len);
069    if (numOfBytesRead != -1) {
070      hasher.putBytes(bytes, off, numOfBytesRead);
071    }
072    return numOfBytesRead;
073  }
074
075  /**
076   * mark() is not supported for HashingInputStream
077   *
078   * @return {@code false} always
079   */
080  @Override
081  public boolean markSupported() {
082    return false;
083  }
084
085  /** mark() is not supported for HashingInputStream */
086  @Override
087  public void mark(int readlimit) {}
088
089  /**
090   * reset() is not supported for HashingInputStream.
091   *
092   * @throws IOException this operation is not supported
093   */
094  @Override
095  public void reset() throws IOException {
096    throw new IOException("reset not supported");
097  }
098
099  /**
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}