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