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 032 private final InputStream in; 033 private long bytesRemaining; 034 private final long expectedChecksum; 035 private final Checksum checksum; 036 037 /** 038 * Constructs a new instance. 039 * 040 * @param checksum Checksum implementation. 041 * @param in the stream to wrap 042 * @param size the of the stream's content 043 * @param expectedChecksum the expected checksum 044 */ 045 public ChecksumVerifyingInputStream(final Checksum checksum, final InputStream in, 046 final long size, final long expectedChecksum) { 047 this.checksum = checksum; 048 this.in = in; 049 this.expectedChecksum = expectedChecksum; 050 this.bytesRemaining = size; 051 } 052 053 @Override 054 public void close() throws IOException { 055 in.close(); 056 } 057 058 /** 059 * @return bytes remaining to read 060 * @since 1.21 061 */ 062 public long getBytesRemaining() { 063 return bytesRemaining; 064 } 065 066 /** 067 * Reads a single byte 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() throws IOException { 074 if (bytesRemaining <= 0) { 075 return -1; 076 } 077 final int ret = in.read(); 078 if (ret >= 0) { 079 checksum.update(ret); 080 --bytesRemaining; 081 } 082 verify(); 083 return ret; 084 } 085 086 /** 087 * Reads a byte array from the stream 088 * @throws IOException if the underlying stream throws or the 089 * stream is exhausted and the Checksum doesn't match the expected 090 * value 091 */ 092 @Override 093 public int read(final byte[] b) throws IOException { 094 return read(b, 0, b.length); 095 } 096 097 /** 098 * Reads from the stream into a byte array. 099 * @throws IOException if the underlying stream throws or the 100 * stream is exhausted and the Checksum doesn't match the expected 101 * value 102 */ 103 @Override 104 public int read(final byte[] b, final int off, final int len) throws IOException { 105 if (len == 0) { 106 return 0; 107 } 108 final int ret = in.read(b, off, len); 109 if (ret >= 0) { 110 checksum.update(b, off, ret); 111 bytesRemaining -= ret; 112 } 113 verify(); 114 return ret; 115 } 116 117 @Override 118 public long skip(final long n) throws IOException { 119 // Can't really skip, we have to hash everything to verify the checksum 120 return read() >= 0 ? 1 : 0; 121 } 122 123 private void verify() throws IOException { 124 if (bytesRemaining <= 0 && expectedChecksum != checksum.getValue()) { 125 throw new IOException("Checksum verification failed"); 126 } 127 } 128}