/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * https://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.commons.io.input; import static org.apache.commons.io.IOUtils.EOF; import java.io.IOException; import java.io.InputStream; import java.util.Objects; import java.util.zip.CheckedInputStream; import java.util.zip.Checksum; /** * Automatically verifies a {@link Checksum} value once the stream is exhausted or the count threshold is reached. *
* If the {@link Checksum} does not meet the expected value when exhausted, then the input stream throws an * {@link IOException}. *
** If you do not need the verification or threshold feature, then use a plain {@link CheckedInputStream}. *
** To build an instance, use {@link Builder}. *
* * @see Builder * @since 2.16.0 */ public final class ChecksumInputStream extends CountingInputStream { // @formatter:off /** * Builds a new {@link ChecksumInputStream}. * ** There is no default {@link Checksum}; you MUST provide one. This avoids any issue with a default {@link Checksum} being proven deficient or insecure * in the future. *
*{@code
* ChecksumInputStream s = ChecksumInputStream.builder()
* .setPath(Paths.get("MyFile.xml"))
* .setChecksum(new CRC32())
* .setExpectedChecksumValue(12345)
* .get();
* }
* {@code
* ChecksumInputStream s = ChecksumInputStream.builder()
* .setFile(new File("MyFile.xml"))
* .setChecksum(new CRC32())
* .setExpectedChecksumValue(12345)
* .get();
* }
* * The following validates the first 100 bytes of the given input. *
*{@code
* ChecksumInputStream s = ChecksumInputStream.builder()
* .setPath(Paths.get("MyFile.xml"))
* .setChecksum(new CRC32())
* .setExpectedChecksumValue(12345)
* .setCountThreshold(100)
* .get();
* }
* * To validate input after the beginning of a stream, build an instance with an InputStream starting where you want to validate. *
*{@code
* InputStream inputStream = ...;
* inputStream.read(...);
* inputStream.skip(...);
* ChecksumInputStream s = ChecksumInputStream.builder()
* .setInputStream(inputStream)
* .setChecksum(new CRC32())
* .setExpectedChecksumValue(12345)
* .setCountThreshold(100)
* .get();
* }
*
* @see #get()
*/
// @formatter:on
public static class Builder extends AbstractBuilder* By default, all input updates the {@link Checksum}. *
*/ private long countThreshold = -1; /** * The expected {@link Checksum} value once the stream is exhausted or the count threshold is reached. */ private long expectedChecksumValue; /** * Constructs a new builder of {@link ChecksumInputStream}. */ public Builder() { // empty } /** * Builds a new {@link ChecksumInputStream}. ** You must set an aspect that supports {@link #getInputStream()}, otherwise, this method throws an exception. *
** This builder uses the following aspects: *
** By default, all input updates the {@link Checksum}. *
* * @param countThreshold the count threshold. A negative number means the threshold is unbound. * @return {@code this} instance. */ public Builder setCountThreshold(final long countThreshold) { this.countThreshold = countThreshold; return this; } /** * The expected {@link Checksum} value once the stream is exhausted or the count threshold is reached. * * @param expectedChecksumValue The expected Checksum value. * @return {@code this} instance. */ public Builder setExpectedChecksumValue(final long expectedChecksumValue) { this.expectedChecksumValue = expectedChecksumValue; return this; } } /** * Constructs a new {@link Builder}. * * @return a new {@link Builder}. */ public static Builder builder() { return new Builder(); } /** The expected checksum. */ private final long expectedChecksumValue; /** * The count threshold to limit how much input is consumed to update the {@link Checksum} before the input stream * validates its value. ** By default, all input updates the {@link Checksum}. *
*/ private final long countThreshold; /** * Constructs a new instance. * * @param builder build parameters. */ @SuppressWarnings("resource") private ChecksumInputStream(final Builder builder) throws IOException { super(new CheckedInputStream(builder.getInputStream(), Objects.requireNonNull(builder.checksum, "builder.checksum")), builder); this.countThreshold = builder.countThreshold; this.expectedChecksumValue = builder.expectedChecksumValue; } @Override protected synchronized void afterRead(final int n) throws IOException { super.afterRead(n); if ((countThreshold > 0 && getByteCount() >= countThreshold || n == EOF) && expectedChecksumValue != getChecksum().getValue()) { // Validate when past the threshold or at EOF throw new IOException("Checksum verification failed."); } } /** * Gets the current checksum value. * * @return the current checksum value. */ private Checksum getChecksum() { return ((CheckedInputStream) in).getChecksum(); } /** * Gets the byte count remaining to read. * * @return bytes remaining to read, a negative number means the threshold is unbound. */ public long getRemaining() { return countThreshold - getByteCount(); } }