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.IOException;
020import java.io.InputStream;
021import java.util.zip.CheckedInputStream;
022import java.util.zip.Checksum;
023
024/**
025 * Verifies the checksum of the data read once the stream is exhausted.
026 *
027 * @NotThreadSafe
028 * @since 1.7
029 */
030public class ChecksumVerifyingInputStream extends CheckedInputStream {
031    // TODO Use Commons IO 2.16.0 ChecksumInputStream
032
033    private long remaining;
034    private final long expected;
035
036    /**
037     * Constructs a new instance.
038     *
039     * @param checksum         Checksum implementation.
040     * @param in               the stream to wrap
041     * @param size             the of the stream's content
042     * @param expectedChecksum the expected checksum
043     */
044    public ChecksumVerifyingInputStream(final Checksum checksum, final InputStream in, final long size, final long expectedChecksum) {
045        super(in, checksum);
046        this.expected = expectedChecksum;
047        this.remaining = size;
048    }
049
050    /**
051     * Gets the byte count remaining to read.
052     *
053     * @return bytes remaining to read.
054     * @since 1.21
055     */
056    public long getBytesRemaining() {
057        return remaining;
058    }
059
060    /**
061     * Reads a single byte from the stream
062     *
063     * @throws IOException if the underlying stream throws or the stream is exhausted and the Checksum doesn't match the expected value
064     */
065    @Override
066    public int read() throws IOException {
067        if (remaining <= 0) {
068            return -1;
069        }
070        final int data = super.read();
071        if (data >= 0) {
072            --remaining;
073        }
074        verify();
075        return data;
076    }
077
078    /**
079     * Reads from the stream into a byte array.
080     *
081     * @throws IOException if the underlying stream throws or the stream is exhausted and the Checksum doesn't match the expected 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 readCount = super.read(b, off, len);
089        if (readCount >= 0) {
090            remaining -= readCount;
091        }
092        verify();
093        return readCount;
094    }
095
096    private void verify() throws IOException {
097        if (remaining <= 0 && expected != getChecksum().getValue()) {
098            throw new IOException("Checksum verification failed");
099        }
100    }
101}