001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.io.input; 018 019import java.io.IOException; 020import java.io.InputStream; 021import java.security.MessageDigest; 022import java.security.NoSuchAlgorithmException; 023 024 025/** 026 * This class is an example for using an {@link ObservableInputStream}. It 027 * creates its own {@link org.apache.commons.io.input.ObservableInputStream.Observer}, 028 * which calculates a checksum using a MessageDigest, for example an MD5 sum. 029 * <em>Note</em>: Neither {@link ObservableInputStream}, nor {@link MessageDigest}, 030 * are thread safe. So is {@link MessageDigestCalculatingInputStream}. 031 */ 032public class MessageDigestCalculatingInputStream extends ObservableInputStream { 033 034 /** 035 * Maintains the message digest. 036 */ 037 public static class MessageDigestMaintainingObserver extends Observer { 038 private final MessageDigest md; 039 040 /** 041 * Creates an MessageDigestMaintainingObserver for the given MessageDigest. 042 * @param pMd the message digest to use 043 */ 044 public MessageDigestMaintainingObserver(final MessageDigest pMd) { 045 md = pMd; 046 } 047 048 @Override 049 void data(final int pByte) throws IOException { 050 md.update((byte) pByte); 051 } 052 053 @Override 054 void data(final byte[] pBuffer, final int pOffset, final int pLength) throws IOException { 055 md.update(pBuffer, pOffset, pLength); 056 } 057 } 058 059 private final MessageDigest messageDigest; 060 061 /** Creates a new instance, which calculates a signature on the given stream, 062 * using the given {@link MessageDigest}. 063 * @param pStream the stream to calculate the message digest for 064 * @param pDigest the message digest to use 065 */ 066 public MessageDigestCalculatingInputStream(final InputStream pStream, final MessageDigest pDigest) { 067 super(pStream); 068 messageDigest = pDigest; 069 add(new MessageDigestMaintainingObserver(pDigest)); 070 } 071 072 /** Creates a new instance, which calculates a signature on the given stream, 073 * using a {@link MessageDigest} with the given algorithm. 074 * @param pStream the stream to calculate the message digest for 075 * @param pAlgorithm the name of the algorithm to use 076 * @throws NoSuchAlgorithmException if no Provider supports a MessageDigestSpi implementation for the specified algorithm. 077 */ 078 public MessageDigestCalculatingInputStream(final InputStream pStream, final String pAlgorithm) throws NoSuchAlgorithmException { 079 this(pStream, MessageDigest.getInstance(pAlgorithm)); 080 } 081 082 /** Creates a new instance, which calculates a signature on the given stream, 083 * using a {@link MessageDigest} with the "MD5" algorithm. 084 * @param pStream the stream to calculate the message digest for 085 * @throws NoSuchAlgorithmException if no Provider supports a MessageDigestSpi implementation for the specified algorithm. 086 */ 087 public MessageDigestCalculatingInputStream(final InputStream pStream) throws NoSuchAlgorithmException { 088 this(pStream, MessageDigest.getInstance("MD5")); 089 } 090 091 /** Returns the {@link MessageDigest}, which is being used for generating the 092 * checksum. 093 * <em>Note</em>: The checksum will only reflect the data, which has been read so far. 094 * This is probably not, what you expect. Make sure, that the complete data has been 095 * read, if that is what you want. The easiest way to do so is by invoking 096 * {@link #consume()}. 097 * @return the message digest used 098 */ 099 public MessageDigest getMessageDigest() { 100 return messageDigest; 101 } 102}