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