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
025import javax.annotation.CheckReturnValue;
026
027/**
028 * An {@link InputStream} that maintains a hash of the data read from it.
029 *
030 * @author Qian Huang
031 * @since 16.0
032 */
033@Beta
034public final class HashingInputStream extends FilterInputStream {
035  private final Hasher hasher;
036
037  /**
038   * Creates an input stream that hashes using the given {@link HashFunction} and delegates all data
039   * read from it to the underlying {@link InputStream}.
040   *
041   * <p>The {@link InputStream} should not be read from before or after the hand-off.
042   */
043  public HashingInputStream(HashFunction hashFunction, InputStream in) {
044    super(checkNotNull(in));
045    this.hasher = checkNotNull(hashFunction.newHasher());
046  }
047
048  /**
049   * Reads the next byte of data from the underlying input stream and updates the hasher with
050   * the byte read.
051   */
052  @Override
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  public int read(byte[] bytes, int off, int len) throws IOException {
067    int numOfBytesRead = in.read(bytes, off, len);
068    if (numOfBytesRead != -1) {
069      hasher.putBytes(bytes, off, numOfBytesRead);
070    }
071    return numOfBytesRead;
072  }
073
074  /**
075   * mark() is not supported for HashingInputStream
076   * @return {@code false} always
077   */
078  @Override
079  @CheckReturnValue
080  public boolean markSupported() {
081    return false;
082  }
083
084  /**
085   * mark() is not supported for HashingInputStream
086   */
087  @Override
088  public void mark(int readlimit) {}
089
090  /**
091   * reset() is not supported for HashingInputStream.
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
101   * unspecified if this method is called more than once on the same instance.
102   */
103  @CheckReturnValue
104  public HashCode hash() {
105    return hasher.hash();
106  }
107}