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;
020
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
032public final class HashingInputStream extends FilterInputStream {
033  private final Hasher hasher;
034
035  /**
036   * Creates an input stream that hashes using the given {@link HashFunction} and delegates all data
037   * read from it to the underlying {@link InputStream}.
038   *
039   * <p>The {@link InputStream} should not be read from before or after the hand-off.
040   */
041  public HashingInputStream(HashFunction hashFunction, InputStream in) {
042    super(checkNotNull(in));
043    this.hasher = checkNotNull(hashFunction.newHasher());
044  }
045
046  /**
047   * Reads the next byte of data from the underlying input stream and updates the hasher with
048   * the byte read.
049   */
050  @Override
051  public int read() throws IOException {
052    int b = in.read();
053    if (b != -1) {
054      hasher.putByte((byte) b);
055    }
056    return b;
057  }
058
059  /**
060   * Reads the specified bytes of data from the underlying input stream and updates the hasher with
061   * the bytes read.
062   */
063  @Override
064  public int read(byte[] bytes, int off, int len) throws IOException {
065    int numOfBytesRead = in.read(bytes, off, len);
066    if (numOfBytesRead != -1) {
067      hasher.putBytes(bytes, off, numOfBytesRead);
068    }
069    return numOfBytesRead;
070  }
071
072  /**
073   * mark() is not supported for HashingInputStream
074   * @return {@code false} always
075   */
076  @Override
077  public boolean markSupported() {
078    return false;
079  }
080
081  /**
082   * mark() is not supported for HashingInputStream
083   */
084  @Override
085  public void mark(int readlimit) {}
086
087  /**
088   * reset() is not supported for HashingInputStream.
089   * @throws IOException this operation is not supported
090   */
091  @Override
092  public void reset() throws IOException {
093    throw new IOException("reset not supported");
094  }
095
096  /**
097   * Returns the {@link HashCode} based on the data read from this stream. The result is
098   * unspecified if this method is called more than once on the same instance.
099   */
100  public HashCode hash() {
101    return hasher.hash();
102  }
103}