View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one or more
3    *  contributor license agreements.  See the NOTICE file distributed with
4    *  this work for additional information regarding copyright ownership.
5    *  The ASF licenses this file to You under the Apache License, Version 2.0
6    *  (the "License"); you may not use this file except in compliance with
7    *  the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  package org.apache.commons.compress.utils;
18  
19  import java.io.IOException;
20  import java.io.InputStream;
21  import java.util.zip.CheckedInputStream;
22  import java.util.zip.Checksum;
23  
24  /**
25   * Verifies the checksum of the data read once the stream is exhausted.
26   *
27   * @NotThreadSafe
28   * @since 1.7
29   */
30  public class ChecksumVerifyingInputStream extends CheckedInputStream {
31      // TODO Use Commons IO 2.16.0 ChecksumInputStream
32  
33      private long remaining;
34      private final long expected;
35  
36      /**
37       * Constructs a new instance.
38       *
39       * @param checksum         Checksum implementation.
40       * @param in               the stream to wrap
41       * @param size             the of the stream's content
42       * @param expectedChecksum the expected checksum
43       */
44      public ChecksumVerifyingInputStream(final Checksum checksum, final InputStream in, final long size, final long expectedChecksum) {
45          super(in, checksum);
46          this.expected = expectedChecksum;
47          this.remaining = size;
48      }
49  
50      /**
51       * Gets the byte count remaining to read.
52       *
53       * @return bytes remaining to read.
54       * @since 1.21
55       */
56      public long getBytesRemaining() {
57          return remaining;
58      }
59  
60      /**
61       * Reads a single byte from the stream
62       *
63       * @throws IOException if the underlying stream throws or the stream is exhausted and the Checksum doesn't match the expected value
64       */
65      @Override
66      public int read() throws IOException {
67          if (remaining <= 0) {
68              return -1;
69          }
70          final int data = super.read();
71          if (data >= 0) {
72              --remaining;
73          }
74          verify();
75          return data;
76      }
77  
78      /**
79       * Reads from the stream into a byte array.
80       *
81       * @throws IOException if the underlying stream throws or the stream is exhausted and the Checksum doesn't match the expected value
82       */
83      @Override
84      public int read(final byte[] b, final int off, final int len) throws IOException {
85          if (len == 0) {
86              return 0;
87          }
88          final int readCount = super.read(b, off, len);
89          if (readCount >= 0) {
90              remaining -= readCount;
91          }
92          verify();
93          return readCount;
94      }
95  
96      private void verify() throws IOException {
97          if (remaining <= 0 && expected != getChecksum().getValue()) {
98              throw new IOException("Checksum verification failed");
99          }
100     }
101 }