    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 verifies the checksum of the data read once the stream is
    026 * exhausted.
    027 * @NotThreadSafe
    028 * @since 1.7
    029 */
    030public class ChecksumVerifyingInputStream extends InputStream {
    031    private final InputStream in;
    032    private long bytesRemaining;
    033    private final long expectedChecksum;
    034    private final Checksum checksum;
    035
    036    public ChecksumVerifyingInputStream(final Checksum checksum, final InputStream in,
    037                                        final long size, final long expectedChecksum) {
    038        this.checksum = checksum;
    039        this.in = in;
    040        this.expectedChecksum = expectedChecksum;
    041        this.bytesRemaining = size;
    042    }
    043
    044    /**
    045     * Reads a single byte from the stream
    046     * @throws IOException if the underlying stream throws or the
    047     * stream is exhausted and the Checksum doesn't match the expected
    048     * value
    049     */
    050    @Override
    051    public int read() throws IOException {
    052        if (bytesRemaining <= 0) {
    053            return -1;
    054        }
    055        final int ret = in.read();
    056        if (ret >= 0) {
    057            checksum.update(ret);
    058            --bytesRemaining;
    059        }
    060        if (bytesRemaining == 0 && expectedChecksum != checksum.getValue()) {
    061            throw new IOException("Checksum verification failed");
    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            bytesRemaining -= ret;
    092        }
    093        if (bytesRemaining <= 0 && expectedChecksum != checksum.getValue()) {
    094            throw new IOException("Checksum verification failed");
    095        }
    096        return ret;
    097    }
    098
    099    @Override
    100    public long skip(final long n) throws IOException {
    101        // Can't really skip, we have to hash everything to verify the checksum
    102        if (read() >= 0) {
    103            return 1;
    104        }
    105        return 0;
    106    }
    107
    108    @Override
    109    public void close() throws IOException {
    110        in.close();
    111    }
    112
    113    /**
    114     * @return bytes remaining to read
    115     * @since 1.21
    116     */
    117    public long getBytesRemaining() {
    118        return bytesRemaining;
    119    }
    120}