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