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 *
017 */
018package org.apache.commons.compress.utils;
019
020import java.io.IOException;
021import java.io.InputStream;
022import java.util.zip.Checksum;
023
024/**
025 * A stream that calculates the checksum of the data read.
026 * @NotThreadSafe
027 * @since 1.14
028 */
029public class ChecksumCalculatingInputStream extends InputStream {
030    private final InputStream in;
031    private final Checksum checksum;
032
033    public ChecksumCalculatingInputStream(final Checksum checksum, final InputStream in) {
034
035        if ( checksum == null ){
036            throw new NullPointerException("Parameter checksum must not be null");
037        }
038
039        if ( in == null ){
040            throw new NullPointerException("Parameter in must not be null");
041        }
042
043        this.checksum = checksum;
044        this.in = in;
045    }
046
047    /**
048     * Reads a single byte from the stream
049     * @throws IOException if the underlying stream throws or the
050     * stream is exhausted and the Checksum doesn't match the expected
051     * value
052     */
053    @Override
054    public int read() throws IOException {
055        final int ret = in.read();
056        if (ret >= 0) {
057            checksum.update(ret);
058        }
059        return ret;
060    }
061
062    /**
063     * Reads a byte array from the stream
064     * @throws IOException if the underlying stream throws or the
065     * stream is exhausted and the Checksum doesn't match the expected
066     * value
067     */
068    @Override
069    public int read(final byte[] b) throws IOException {
070        return read(b, 0, b.length);
071    }
072
073    /**
074     * Reads from the stream into a byte array.
075     * @throws IOException if the underlying stream throws or the
076     * stream is exhausted and the Checksum doesn't match the expected
077     * value
078     */
079    @Override
080    public int read(final byte[] b, final int off, final int len) throws IOException {
081        final int ret = in.read(b, off, len);
082        if (ret >= 0) {
083            checksum.update(b, off, ret);
084        }
085        return ret;
086    }
087
088    @Override
089    public long skip(final long n) throws IOException {
090        // Can't really skip, we have to hash everything to verify the checksum
091        if (read() >= 0) {
092            return 1;
093        }
094        return 0;
095    }
096
097    /**
098     * Returns the calculated checksum.
099     * @return the calculated checksum.
100     */
101    public long getValue() {
102        return checksum.getValue();
103    }
104
105}