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