001/**
002 * Copyright 2015 DuraSpace, Inc.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.fcrepo.kernel.impl.utils;
017
018import java.io.InputStream;
019import java.security.DigestInputStream;
020import java.security.MessageDigest;
021
022import org.apache.commons.io.input.CountingInputStream;
023
024/**
025 * An InputStream wrapper that calculates the size and digest
026 * while reading from the stream.
027 * @author Chris Beer
028 * @since Mar 12, 2013
029 */
030public class FixityInputStream extends CountingInputStream {
031
032    /**
033     * Creates a <code>FilterInputStream</code> by assigning the
034     * argument <code>in</code> to the field <code>this.in</code>
035     * so as to remember it for later use.
036     *
037     * @param in the underlying input stream, or <code>null</code> if
038     *           this instance is to be created without an underlying stream.
039     * @param digest the given digest
040     */
041    public FixityInputStream(final InputStream in, final MessageDigest digest) {
042        super(new DigestInputStream(in, digest));
043    }
044
045    /**
046     * Retrieve the calculated digest for the input stream
047     * @return digest for this input stream
048     */
049    public MessageDigest getMessageDigest() {
050        return ((DigestInputStream) in).getMessageDigest();
051    }
052
053}