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 messageDigest;
039
040        /**
041         * Creates an MessageDigestMaintainingObserver for the given MessageDigest.
042         * @param messageDigest the message digest to use
043         */
044        public MessageDigestMaintainingObserver(final MessageDigest messageDigest) {
045            this.messageDigest = messageDigest;
046        }
047
048        @Override
049        public void data(final int input) throws IOException {
050            messageDigest.update((byte) input);
051        }
052
053        @Override
054        public void data(final byte[] input, final int offset, final int length) throws IOException {
055            messageDigest.update(input, offset, length);
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 inputStream the stream to calculate the message digest for
064     * @param MessageDigest the message digest to use
065     */
066    public MessageDigestCalculatingInputStream(final InputStream inputStream, final MessageDigest MessageDigest) {
067        super(inputStream);
068        this.messageDigest = MessageDigest;
069        add(new MessageDigestMaintainingObserver(MessageDigest));
070    }
071
072    /**
073     * Creates a new instance, which calculates a signature on the given stream, using a {@link MessageDigest} with the
074     * given algorithm.
075     * 
076     * @param inputStream the stream to calculate the message digest for
077     * @param algorithm the name of the algorithm to use
078     * @throws NoSuchAlgorithmException if no Provider supports a MessageDigestSpi implementation for the specified
079     *         algorithm.
080     */
081    public MessageDigestCalculatingInputStream(final InputStream inputStream, final String algorithm)
082        throws NoSuchAlgorithmException {
083        this(inputStream, MessageDigest.getInstance(algorithm));
084    }
085
086    /**
087     * Creates a new instance, which calculates a signature on the given stream, using a {@link MessageDigest} with the
088     * "MD5" algorithm.
089     * 
090     * @param inputStream the stream to calculate the message digest for
091     * @throws NoSuchAlgorithmException if no Provider supports a MessageDigestSpi implementation for the specified
092     *         algorithm.
093     */
094    public MessageDigestCalculatingInputStream(final InputStream inputStream) throws NoSuchAlgorithmException {
095        this(inputStream, MessageDigest.getInstance("MD5"));
096    }
097
098    /** Returns the {@link MessageDigest}, which is being used for generating the
099     * checksum.
100     * <em>Note</em>: The checksum will only reflect the data, which has been read so far.
101     * This is probably not, what you expect. Make sure, that the complete data has been
102     * read, if that is what you want. The easiest way to do so is by invoking
103     * {@link #consume()}.
104     * @return the message digest used
105     */
106    public MessageDigest getMessageDigest() {
107        return messageDigest;
108    }
109}