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.compress.utils;
018
019import java.io.FilterInputStream;
020import java.io.IOException;
021import java.io.InputStream;
022import java.util.Objects;
023import java.util.zip.Checksum;
024
025/**
026 * Calculates the checksum of the data read.
027 *
028 * @NotThreadSafe
029 * @since 1.14
030 */
031public class ChecksumCalculatingInputStream extends FilterInputStream {
032    private final Checksum checksum;
033
034    public ChecksumCalculatingInputStream(final Checksum checksum, final InputStream inputStream) {
035        super(Objects.requireNonNull(inputStream, "inputStream"));
036        this.checksum = Objects.requireNonNull(checksum, "checksum");
037    }
038
039    /**
040     * Returns the calculated checksum.
041     * @return the calculated checksum.
042     */
043    public long getValue() {
044        return checksum.getValue();
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 from the stream into a byte array.
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, final int off, final int len) throws IOException {
070        if (len == 0) {
071            return 0;
072        }
073        final int ret = in.read(b, off, len);
074        if (ret >= 0) {
075            checksum.update(b, off, ret);
076        }
077        return ret;
078    }
079
080    @Override
081    public long skip(final long n) throws IOException {
082        // Can't really skip, we have to hash everything to verify the checksum
083        if (read() >= 0) {
084            return 1;
085        }
086        return 0;
087    }
088
089}