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