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 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 the 048 * byte read. 049 */ 050 @Override 051 @CanIgnoreReturnValue 052 public int read() throws IOException { 053 int b = in.read(); 054 if (b != -1) { 055 hasher.putByte((byte) b); 056 } 057 return b; 058 } 059 060 /** 061 * Reads the specified bytes of data from the underlying input stream and updates the hasher with 062 * the bytes read. 063 */ 064 @Override 065 @CanIgnoreReturnValue 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 * 077 * @return {@code false} always 078 */ 079 @Override 080 public boolean markSupported() { 081 return false; 082 } 083 084 /** mark() is not supported for HashingInputStream */ 085 @Override 086 public void mark(int readlimit) {} 087 088 /** 089 * reset() is not supported for HashingInputStream. 090 * 091 * @throws IOException this operation is not supported 092 */ 093 @Override 094 public void reset() throws IOException { 095 throw new IOException("reset not supported"); 096 } 097 098 /** 099 * Returns the {@link HashCode} based on the data read from this stream. The result is unspecified 100 * if this method is called more than once on the same instance. 101 */ 102 public HashCode hash() { 103 return hasher.hash(); 104 } 105}