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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package java.util.zip;
20 import java.io.IOException;
21 import java.io.InputStream;
24 * The {@code CheckedInputStream} class is used to maintain a checksum at the
25 * same time as the data, on which the checksum is computed, is read from a
26 * stream. The purpose of this checksum is to establish data integrity,
27 * comparing the computed checksum against a published checksum value.
29 public class CheckedInputStream extends java.io.FilterInputStream {
31 private final Checksum check;
34 * Constructs a new {@code CheckedInputStream} on {@code InputStream}
35 * {@code is}. The checksum will be calculated using the algorithm
36 * implemented by {@code csum}.
38 * <p><strong>Warning:</strong> passing a null source creates an invalid
39 * {@code CheckedInputStream}. All operations on such a stream will fail.
42 * the input stream to calculate checksum from.
44 * an entity implementing the checksum algorithm.
46 public CheckedInputStream(InputStream is, Checksum csum) {
52 * Reads one byte of data from the underlying input stream and updates the
53 * checksum with the byte data.
55 * @return {@code -1} at the end of the stream, a single byte value
58 * if an {@code IOException} occurs.
61 public int read() throws IOException {
70 * Reads up to n bytes of data from the underlying input stream, storing it
71 * into {@code buf}, starting at offset {@code off}. The checksum is
72 * updated with the bytes read.
75 * the byte array in which to store the bytes read.
77 * the initial position in {@code buf} to store the bytes read
80 * the maximum number of bytes to store in {@code buf}.
81 * @return the number of bytes actually read or {@code -1} if arrived at the
82 * end of the filtered stream while reading the data.
84 * if this stream is closed or some I/O error occurs.
87 public int read(byte[] buf, int off, int nbytes) throws IOException {
88 int x = in.read(buf, off, nbytes);
90 check.update(buf, off, x);
96 * Returns the checksum calculated on the stream read so far.
98 * @return the updated checksum.
100 public Checksum getChecksum() {
105 * Skip up to n bytes of data on the underlying input stream. Any skipped
106 * bytes are added to the running checksum value.
109 * the number of bytes to skip.
110 * @throws IOException
111 * if this stream is closed or another I/O error occurs.
112 * @return the number of bytes skipped.
115 public long skip(long nbytes) throws IOException {
120 byte[] b = new byte[(int)Math.min(nbytes, 2048L)];
122 while (skipped != nbytes) {
124 (v = (int) (nbytes - skipped)) > b.length ? b.length : v);
128 check.update(b, 0, x);