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        final int ret = in.read(b, off, len);
086        if (ret >= 0) {
087            checksum.update(b, off, ret);
088            bytesRemaining -= ret;
089        }
090        if (bytesRemaining <= 0 && expectedChecksum != checksum.getValue()) {
091            throw new IOException("Checksum verification failed");
092        }
093        return ret;
094    }
095
096    @Override
097    public long skip(final long n) throws IOException {
098        // Can't really skip, we have to hash everything to verify the checksum
099        if (read() >= 0) {
100            return 1;
101        }
102        return 0;
103    }
104
105    @Override
106    public void close() throws IOException {
107        in.close();
108    }
109}