elharo commented on code in PR #548:
URL: https://github.com/apache/commons-io/pull/548#discussion_r1439070485


##########
src/main/java/org/apache/commons/io/input/ChecksumInputStream.java:
##########
@@ -0,0 +1,208 @@
+/*
+ *  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
+ *
+ *      http://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 java.io.IOException;
+import java.io.InputStream;
+import java.util.zip.CheckedInputStream;
+import java.util.zip.Checksum;
+
+import org.apache.commons.io.build.AbstractStreamBuilder;
+
+/**
+ * Automatically verifies a {@link Checksum} value once the stream is 
exhausted.
+ * <p>
+ * If the {@link Checksum} does not meet the expected value when exhausted, 
then the input stream throws a
+ * {@link IOException}.
+ * </p>
+ * <p>
+ * If you do not need the verification or threshold feature, then use a plain 
{@link CheckedInputStream}.
+ * </p>
+ *
+ * @since 2.16.0
+ */
+public final class ChecksumInputStream extends CountingInputStream {
+
+    // @formatter:off
+    /**
+     * Builds a new {@link ChecksumInputStream} instance.
+     * <p>
+     * There is no default {@link Checksum}, you MUST provide one.
+     * </p>
+     * <h2>Using NIO</h2>
+     *
+     * <pre>{@code
+     * ChecksumInputStream s = ChecksumInputStream.builder()
+     *   .setPath(Paths.get("MyFile.xml"))
+     *   .setChecksum(new CRC32())
+     *   .get();
+     * }</pre>
+     *
+     * <h2>Using IO</h2>
+     *
+     * <pre>{@code
+     * ChecksumInputStream s = ChecksumInputStream.builder()
+     *   .setFile(new File("MyFile.xml"))
+     *   .setChecksum(new CRC32())
+     *   .get();
+     * }</pre>
+     */
+    // @formatter:on
+    public static class Builder extends 
AbstractStreamBuilder<ChecksumInputStream, Builder> {
+
+        /**
+         * There is no default checksum, you MUST provide one. This avoids any 
issue with a default {@link Checksum}
+         * being proven deficient or insecure in the future.
+         */
+        private Checksum checksum;
+
+        /**
+         * The count threshold to limit how much input is consumed to update 
the {@link Checksum} before the input
+         * stream validate its value.
+         * <p>
+         * By default, all input updates the {@link Checksum}.
+         * </p>
+         */
+        private long countThreshold = -1;
+
+        /**
+         * The expected {@link Checksum} value.
+         */
+        private long expectedChecksumValue;
+
+        /**
+         * Constructs a new instance.
+         * <p>
+         * This builder use the aspect InputStream.
+         * </p>
+         * <p>
+         * You must provide an origin that can be converted to an InputStream 
by this builder, otherwise, this call will
+         * throw an {@link UnsupportedOperationException}.
+         * </p>
+         *
+         * @return a new instance.
+         * @throws UnsupportedOperationException if the origin cannot provide 
an InputStream.
+         * @see #getInputStream()
+         */
+        @SuppressWarnings("resource")
+        @Override
+        public ChecksumInputStream get() throws IOException {
+            return new ChecksumInputStream(getInputStream(), checksum, 
expectedChecksumValue, countThreshold);
+        }
+
+        /**
+         * Sets the Checksum.
+         *
+         * @param checksum the Checksum.
+         * @return this.
+         */
+        public Builder setChecksum(final Checksum checksum) {
+            this.checksum = checksum;
+            return this;
+        }
+
+        /**
+         * Sets the count threshold to limit how much input is consumed to 
update the {@link Checksum} before the input
+         * stream validate its value.
+         * <p>
+         * By default, all input updates the {@link Checksum}.
+         * </p>
+         *
+         * @param countThreshold the count threshold, a negative number means 
the threshold is unbound.
+         * @return this.
+         */
+        public Builder setCountThreshold(final long countThreshold) {
+            this.countThreshold = countThreshold;
+            return this;
+        }
+
+        /**
+         * The expected {@link Checksum} value once the input is consumed or 
the threshold is reached.
+         *
+         * @param expectedChecksumValue The expected Checksum value.
+         * @return this.
+         */
+        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

Review Comment:
   validates



##########
src/main/java/org/apache/commons/io/input/ChecksumInputStream.java:
##########
@@ -0,0 +1,208 @@
+/*
+ *  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
+ *
+ *      http://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 java.io.IOException;
+import java.io.InputStream;
+import java.util.zip.CheckedInputStream;
+import java.util.zip.Checksum;
+
+import org.apache.commons.io.build.AbstractStreamBuilder;
+
+/**
+ * Automatically verifies a {@link Checksum} value once the stream is 
exhausted.
+ * <p>
+ * If the {@link Checksum} does not meet the expected value when exhausted, 
then the input stream throws a
+ * {@link IOException}.
+ * </p>
+ * <p>
+ * If you do not need the verification or threshold feature, then use a plain 
{@link CheckedInputStream}.
+ * </p>
+ *
+ * @since 2.16.0
+ */
+public final class ChecksumInputStream extends CountingInputStream {
+
+    // @formatter:off
+    /**
+     * Builds a new {@link ChecksumInputStream} instance.
+     * <p>
+     * There is no default {@link Checksum}, you MUST provide one.
+     * </p>
+     * <h2>Using NIO</h2>
+     *
+     * <pre>{@code
+     * ChecksumInputStream s = ChecksumInputStream.builder()
+     *   .setPath(Paths.get("MyFile.xml"))
+     *   .setChecksum(new CRC32())
+     *   .get();
+     * }</pre>
+     *
+     * <h2>Using IO</h2>
+     *
+     * <pre>{@code
+     * ChecksumInputStream s = ChecksumInputStream.builder()
+     *   .setFile(new File("MyFile.xml"))
+     *   .setChecksum(new CRC32())
+     *   .get();
+     * }</pre>
+     */
+    // @formatter:on
+    public static class Builder extends 
AbstractStreamBuilder<ChecksumInputStream, Builder> {
+
+        /**
+         * There is no default checksum, you MUST provide one. This avoids any 
issue with a default {@link Checksum}
+         * being proven deficient or insecure in the future.
+         */
+        private Checksum checksum;
+
+        /**
+         * The count threshold to limit how much input is consumed to update 
the {@link Checksum} before the input
+         * stream validate its value.
+         * <p>
+         * By default, all input updates the {@link Checksum}.
+         * </p>
+         */
+        private long countThreshold = -1;
+
+        /**
+         * The expected {@link Checksum} value.
+         */
+        private long expectedChecksumValue;
+
+        /**
+         * Constructs a new instance.
+         * <p>
+         * This builder use the aspect InputStream.
+         * </p>
+         * <p>
+         * You must provide an origin that can be converted to an InputStream 
by this builder, otherwise, this call will
+         * throw an {@link UnsupportedOperationException}.
+         * </p>
+         *
+         * @return a new instance.
+         * @throws UnsupportedOperationException if the origin cannot provide 
an InputStream.
+         * @see #getInputStream()
+         */
+        @SuppressWarnings("resource")
+        @Override
+        public ChecksumInputStream get() throws IOException {
+            return new ChecksumInputStream(getInputStream(), checksum, 
expectedChecksumValue, countThreshold);
+        }
+
+        /**
+         * Sets the Checksum.
+         *
+         * @param checksum the Checksum.
+         * @return this.
+         */
+        public Builder setChecksum(final Checksum checksum) {
+            this.checksum = checksum;
+            return this;
+        }
+
+        /**
+         * Sets the count threshold to limit how much input is consumed to 
update the {@link Checksum} before the input
+         * stream validate its value.

Review Comment:
   validates



##########
src/main/java/org/apache/commons/io/input/ChecksumInputStream.java:
##########
@@ -0,0 +1,208 @@
+/*
+ *  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
+ *
+ *      http://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 java.io.IOException;
+import java.io.InputStream;
+import java.util.zip.CheckedInputStream;
+import java.util.zip.Checksum;
+
+import org.apache.commons.io.build.AbstractStreamBuilder;
+
+/**
+ * Automatically verifies a {@link Checksum} value once the stream is 
exhausted.
+ * <p>
+ * If the {@link Checksum} does not meet the expected value when exhausted, 
then the input stream throws a

Review Comment:
   throws a --> throws an



##########
src/main/java/org/apache/commons/io/input/ChecksumInputStream.java:
##########
@@ -0,0 +1,208 @@
+/*
+ *  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
+ *
+ *      http://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 java.io.IOException;
+import java.io.InputStream;
+import java.util.zip.CheckedInputStream;
+import java.util.zip.Checksum;
+
+import org.apache.commons.io.build.AbstractStreamBuilder;
+
+/**
+ * Automatically verifies a {@link Checksum} value once the stream is 
exhausted.
+ * <p>
+ * If the {@link Checksum} does not meet the expected value when exhausted, 
then the input stream throws a
+ * {@link IOException}.
+ * </p>
+ * <p>
+ * If you do not need the verification or threshold feature, then use a plain 
{@link CheckedInputStream}.
+ * </p>
+ *
+ * @since 2.16.0
+ */
+public final class ChecksumInputStream extends CountingInputStream {
+
+    // @formatter:off
+    /**
+     * Builds a new {@link ChecksumInputStream} instance.
+     * <p>
+     * There is no default {@link Checksum}, you MUST provide one.
+     * </p>
+     * <h2>Using NIO</h2>
+     *
+     * <pre>{@code
+     * ChecksumInputStream s = ChecksumInputStream.builder()
+     *   .setPath(Paths.get("MyFile.xml"))
+     *   .setChecksum(new CRC32())
+     *   .get();
+     * }</pre>
+     *
+     * <h2>Using IO</h2>
+     *
+     * <pre>{@code
+     * ChecksumInputStream s = ChecksumInputStream.builder()
+     *   .setFile(new File("MyFile.xml"))
+     *   .setChecksum(new CRC32())
+     *   .get();
+     * }</pre>
+     */
+    // @formatter:on
+    public static class Builder extends 
AbstractStreamBuilder<ChecksumInputStream, Builder> {
+
+        /**
+         * There is no default checksum, you MUST provide one. This avoids any 
issue with a default {@link Checksum}
+         * being proven deficient or insecure in the future.
+         */
+        private Checksum checksum;
+
+        /**
+         * The count threshold to limit how much input is consumed to update 
the {@link Checksum} before the input
+         * stream validate its value.
+         * <p>
+         * By default, all input updates the {@link Checksum}.
+         * </p>
+         */
+        private long countThreshold = -1;
+
+        /**
+         * The expected {@link Checksum} value.
+         */
+        private long expectedChecksumValue;
+
+        /**
+         * Constructs a new instance.
+         * <p>
+         * This builder use the aspect InputStream.
+         * </p>
+         * <p>
+         * You must provide an origin that can be converted to an InputStream 
by this builder, otherwise, this call will
+         * throw an {@link UnsupportedOperationException}.
+         * </p>
+         *
+         * @return a new instance.
+         * @throws UnsupportedOperationException if the origin cannot provide 
an InputStream.
+         * @see #getInputStream()
+         */
+        @SuppressWarnings("resource")
+        @Override
+        public ChecksumInputStream get() throws IOException {
+            return new ChecksumInputStream(getInputStream(), checksum, 
expectedChecksumValue, countThreshold);
+        }
+
+        /**
+         * Sets the Checksum.
+         *
+         * @param checksum the Checksum.
+         * @return this.
+         */
+        public Builder setChecksum(final Checksum checksum) {
+            this.checksum = checksum;
+            return this;
+        }
+
+        /**
+         * Sets the count threshold to limit how much input is consumed to 
update the {@link Checksum} before the input
+         * stream validate its value.
+         * <p>
+         * By default, all input updates the {@link Checksum}.
+         * </p>
+         *
+         * @param countThreshold the count threshold, a negative number means 
the threshold is unbound.
+         * @return this.
+         */
+        public Builder setCountThreshold(final long countThreshold) {
+            this.countThreshold = countThreshold;
+            return this;
+        }
+
+        /**
+         * The expected {@link Checksum} value once the input is consumed or 
the threshold is reached.
+         *
+         * @param expectedChecksumValue The expected Checksum value.
+         * @return this.
+         */
+        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
+     * validate its value.
+     * <p>
+     * By default, all input updates the {@link Checksum}.
+     * </p>
+     */
+    private final long countThreshold;
+
+    /**
+     * Constructs a new instance.
+     *
+     * @param in                    the stream to wrap.
+     * @param checksum              Checksum implementation.
+     * @param expectedChecksumValue the expected checksum.
+     * @param countThreshold        the of the stream's content, a negative 
number means the threshold is unbound.
+     */
+    private ChecksumInputStream(final InputStream in, final Checksum checksum, 
final long expectedChecksumValue,
+            final long countThreshold) {
+        super(new CheckedInputStream(in, checksum));
+        this.countThreshold = countThreshold;
+        this.expectedChecksumValue = expectedChecksumValue;
+    }
+
+    @Override
+    protected synchronized void afterRead(final int n) throws IOException {
+        super.afterRead(n);
+        if (getByteCount() >= countThreshold && expectedChecksumValue != 
getChecksum().getValue()) {

Review Comment:
   seems like the checksum might not count the right number of bytes. That is, 
it might not end on the boundary. Immediate problem if there's no threshold. 



##########
src/main/java/org/apache/commons/io/input/ChecksumInputStream.java:
##########
@@ -0,0 +1,208 @@
+/*
+ *  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
+ *
+ *      http://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 java.io.IOException;
+import java.io.InputStream;
+import java.util.zip.CheckedInputStream;
+import java.util.zip.Checksum;
+
+import org.apache.commons.io.build.AbstractStreamBuilder;
+
+/**
+ * Automatically verifies a {@link Checksum} value once the stream is 
exhausted.
+ * <p>
+ * If the {@link Checksum} does not meet the expected value when exhausted, 
then the input stream throws a
+ * {@link IOException}.
+ * </p>
+ * <p>
+ * If you do not need the verification or threshold feature, then use a plain 
{@link CheckedInputStream}.
+ * </p>
+ *
+ * @since 2.16.0
+ */
+public final class ChecksumInputStream extends CountingInputStream {
+
+    // @formatter:off
+    /**
+     * Builds a new {@link ChecksumInputStream} instance.
+     * <p>
+     * There is no default {@link Checksum}, you MUST provide one.
+     * </p>
+     * <h2>Using NIO</h2>
+     *
+     * <pre>{@code
+     * ChecksumInputStream s = ChecksumInputStream.builder()
+     *   .setPath(Paths.get("MyFile.xml"))
+     *   .setChecksum(new CRC32())

Review Comment:
   I think you'd want to supply an actual value here with 
setExpectedChecksumValue, not only a  blank CRC32 object



##########
src/main/java/org/apache/commons/io/input/ChecksumInputStream.java:
##########
@@ -0,0 +1,208 @@
+/*
+ *  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
+ *
+ *      http://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 java.io.IOException;
+import java.io.InputStream;
+import java.util.zip.CheckedInputStream;
+import java.util.zip.Checksum;
+
+import org.apache.commons.io.build.AbstractStreamBuilder;
+
+/**
+ * Automatically verifies a {@link Checksum} value once the stream is 
exhausted.
+ * <p>
+ * If the {@link Checksum} does not meet the expected value when exhausted, 
then the input stream throws a
+ * {@link IOException}.
+ * </p>
+ * <p>
+ * If you do not need the verification or threshold feature, then use a plain 
{@link CheckedInputStream}.
+ * </p>
+ *
+ * @since 2.16.0
+ */
+public final class ChecksumInputStream extends CountingInputStream {
+
+    // @formatter:off
+    /**
+     * Builds a new {@link ChecksumInputStream} instance.
+     * <p>
+     * There is no default {@link Checksum}, you MUST provide one.

Review Comment:
   ; you MUST



##########
src/main/java/org/apache/commons/io/input/ChecksumInputStream.java:
##########
@@ -0,0 +1,208 @@
+/*
+ *  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
+ *
+ *      http://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 java.io.IOException;
+import java.io.InputStream;
+import java.util.zip.CheckedInputStream;
+import java.util.zip.Checksum;
+
+import org.apache.commons.io.build.AbstractStreamBuilder;
+
+/**
+ * Automatically verifies a {@link Checksum} value once the stream is 
exhausted.
+ * <p>
+ * If the {@link Checksum} does not meet the expected value when exhausted, 
then the input stream throws a
+ * {@link IOException}.
+ * </p>
+ * <p>
+ * If you do not need the verification or threshold feature, then use a plain 
{@link CheckedInputStream}.
+ * </p>
+ *
+ * @since 2.16.0
+ */
+public final class ChecksumInputStream extends CountingInputStream {
+
+    // @formatter:off
+    /**
+     * Builds a new {@link ChecksumInputStream} instance.
+     * <p>
+     * There is no default {@link Checksum}, you MUST provide one.
+     * </p>
+     * <h2>Using NIO</h2>
+     *
+     * <pre>{@code
+     * ChecksumInputStream s = ChecksumInputStream.builder()
+     *   .setPath(Paths.get("MyFile.xml"))
+     *   .setChecksum(new CRC32())
+     *   .get();
+     * }</pre>
+     *
+     * <h2>Using IO</h2>
+     *
+     * <pre>{@code
+     * ChecksumInputStream s = ChecksumInputStream.builder()
+     *   .setFile(new File("MyFile.xml"))
+     *   .setChecksum(new CRC32())
+     *   .get();
+     * }</pre>
+     */
+    // @formatter:on
+    public static class Builder extends 
AbstractStreamBuilder<ChecksumInputStream, Builder> {
+
+        /**
+         * There is no default checksum, you MUST provide one. This avoids any 
issue with a default {@link Checksum}
+         * being proven deficient or insecure in the future.
+         */
+        private Checksum checksum;
+
+        /**
+         * The count threshold to limit how much input is consumed to update 
the {@link Checksum} before the input
+         * stream validate its value.
+         * <p>
+         * By default, all input updates the {@link Checksum}.
+         * </p>
+         */
+        private long countThreshold = -1;
+
+        /**
+         * The expected {@link Checksum} value.
+         */
+        private long expectedChecksumValue;
+
+        /**
+         * Constructs a new instance.
+         * <p>
+         * This builder use the aspect InputStream.
+         * </p>
+         * <p>
+         * You must provide an origin that can be converted to an InputStream 
by this builder, otherwise, this call will
+         * throw an {@link UnsupportedOperationException}.
+         * </p>
+         *
+         * @return a new instance.
+         * @throws UnsupportedOperationException if the origin cannot provide 
an InputStream.
+         * @see #getInputStream()
+         */
+        @SuppressWarnings("resource")
+        @Override
+        public ChecksumInputStream get() throws IOException {
+            return new ChecksumInputStream(getInputStream(), checksum, 
expectedChecksumValue, countThreshold);
+        }
+
+        /**
+         * Sets the Checksum.
+         *
+         * @param checksum the Checksum.
+         * @return this.
+         */
+        public Builder setChecksum(final Checksum checksum) {
+            this.checksum = checksum;
+            return this;
+        }
+
+        /**
+         * Sets the count threshold to limit how much input is consumed to 
update the {@link Checksum} before the input
+         * stream validate its value.
+         * <p>
+         * By default, all input updates the {@link Checksum}.
+         * </p>
+         *
+         * @param countThreshold the count threshold, a negative number means 
the threshold is unbound.

Review Comment:
   threshold. A negative



##########
src/main/java/org/apache/commons/io/input/ChecksumInputStream.java:
##########
@@ -0,0 +1,208 @@
+/*
+ *  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
+ *
+ *      http://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 java.io.IOException;
+import java.io.InputStream;
+import java.util.zip.CheckedInputStream;
+import java.util.zip.Checksum;
+
+import org.apache.commons.io.build.AbstractStreamBuilder;
+
+/**
+ * Automatically verifies a {@link Checksum} value once the stream is 
exhausted.
+ * <p>
+ * If the {@link Checksum} does not meet the expected value when exhausted, 
then the input stream throws a
+ * {@link IOException}.
+ * </p>
+ * <p>
+ * If you do not need the verification or threshold feature, then use a plain 
{@link CheckedInputStream}.
+ * </p>
+ *
+ * @since 2.16.0
+ */
+public final class ChecksumInputStream extends CountingInputStream {
+
+    // @formatter:off
+    /**
+     * Builds a new {@link ChecksumInputStream} instance.
+     * <p>
+     * There is no default {@link Checksum}, you MUST provide one.
+     * </p>
+     * <h2>Using NIO</h2>
+     *
+     * <pre>{@code
+     * ChecksumInputStream s = ChecksumInputStream.builder()
+     *   .setPath(Paths.get("MyFile.xml"))
+     *   .setChecksum(new CRC32())
+     *   .get();
+     * }</pre>
+     *
+     * <h2>Using IO</h2>
+     *
+     * <pre>{@code
+     * ChecksumInputStream s = ChecksumInputStream.builder()
+     *   .setFile(new File("MyFile.xml"))
+     *   .setChecksum(new CRC32())
+     *   .get();
+     * }</pre>
+     */
+    // @formatter:on
+    public static class Builder extends 
AbstractStreamBuilder<ChecksumInputStream, Builder> {
+
+        /**
+         * There is no default checksum, you MUST provide one. This avoids any 
issue with a default {@link Checksum}
+         * being proven deficient or insecure in the future.
+         */
+        private Checksum checksum;
+
+        /**
+         * The count threshold to limit how much input is consumed to update 
the {@link Checksum} before the input
+         * stream validate its value.
+         * <p>
+         * By default, all input updates the {@link Checksum}.
+         * </p>
+         */
+        private long countThreshold = -1;
+
+        /**
+         * The expected {@link Checksum} value.
+         */
+        private long expectedChecksumValue;
+
+        /**
+         * Constructs a new instance.
+         * <p>
+         * This builder use the aspect InputStream.
+         * </p>
+         * <p>
+         * You must provide an origin that can be converted to an InputStream 
by this builder, otherwise, this call will
+         * throw an {@link UnsupportedOperationException}.
+         * </p>
+         *
+         * @return a new instance.
+         * @throws UnsupportedOperationException if the origin cannot provide 
an InputStream.
+         * @see #getInputStream()
+         */
+        @SuppressWarnings("resource")
+        @Override
+        public ChecksumInputStream get() throws IOException {
+            return new ChecksumInputStream(getInputStream(), checksum, 
expectedChecksumValue, countThreshold);
+        }
+
+        /**
+         * Sets the Checksum.
+         *
+         * @param checksum the Checksum.
+         * @return this.
+         */
+        public Builder setChecksum(final Checksum checksum) {
+            this.checksum = checksum;
+            return this;
+        }
+
+        /**
+         * Sets the count threshold to limit how much input is consumed to 
update the {@link Checksum} before the input
+         * stream validate its value.
+         * <p>
+         * By default, all input updates the {@link Checksum}.
+         * </p>
+         *
+         * @param countThreshold the count threshold, a negative number means 
the threshold is unbound.
+         * @return this.
+         */
+        public Builder setCountThreshold(final long countThreshold) {
+            this.countThreshold = countThreshold;
+            return this;
+        }
+
+        /**
+         * The expected {@link Checksum} value once the input is consumed or 
the threshold is reached.
+         *
+         * @param expectedChecksumValue The expected Checksum value.
+         * @return this.
+         */
+        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
+     * validate its value.
+     * <p>
+     * By default, all input updates the {@link Checksum}.
+     * </p>
+     */
+    private final long countThreshold;
+
+    /**
+     * Constructs a new instance.
+     *
+     * @param in                    the stream to wrap.
+     * @param checksum              Checksum implementation.
+     * @param expectedChecksumValue the expected checksum.
+     * @param countThreshold        the of the stream's content, a negative 
number means the threshold is unbound.

Review Comment:
   a word is missing?



##########
src/main/java/org/apache/commons/io/input/ChecksumInputStream.java:
##########
@@ -0,0 +1,208 @@
+/*
+ *  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
+ *
+ *      http://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 java.io.IOException;
+import java.io.InputStream;
+import java.util.zip.CheckedInputStream;
+import java.util.zip.Checksum;
+
+import org.apache.commons.io.build.AbstractStreamBuilder;
+
+/**
+ * Automatically verifies a {@link Checksum} value once the stream is 
exhausted.
+ * <p>
+ * If the {@link Checksum} does not meet the expected value when exhausted, 
then the input stream throws a
+ * {@link IOException}.
+ * </p>
+ * <p>
+ * If you do not need the verification or threshold feature, then use a plain 
{@link CheckedInputStream}.
+ * </p>
+ *
+ * @since 2.16.0
+ */
+public final class ChecksumInputStream extends CountingInputStream {
+
+    // @formatter:off
+    /**
+     * Builds a new {@link ChecksumInputStream} instance.
+     * <p>
+     * There is no default {@link Checksum}, you MUST provide one.
+     * </p>
+     * <h2>Using NIO</h2>
+     *
+     * <pre>{@code
+     * ChecksumInputStream s = ChecksumInputStream.builder()
+     *   .setPath(Paths.get("MyFile.xml"))
+     *   .setChecksum(new CRC32())
+     *   .get();
+     * }</pre>
+     *
+     * <h2>Using IO</h2>
+     *
+     * <pre>{@code
+     * ChecksumInputStream s = ChecksumInputStream.builder()
+     *   .setFile(new File("MyFile.xml"))
+     *   .setChecksum(new CRC32())
+     *   .get();
+     * }</pre>
+     */
+    // @formatter:on
+    public static class Builder extends 
AbstractStreamBuilder<ChecksumInputStream, Builder> {
+
+        /**
+         * There is no default checksum, you MUST provide one. This avoids any 
issue with a default {@link Checksum}
+         * being proven deficient or insecure in the future.
+         */
+        private Checksum checksum;
+
+        /**
+         * The count threshold to limit how much input is consumed to update 
the {@link Checksum} before the input
+         * stream validate its value.
+         * <p>
+         * By default, all input updates the {@link Checksum}.
+         * </p>
+         */
+        private long countThreshold = -1;
+
+        /**
+         * The expected {@link Checksum} value.
+         */
+        private long expectedChecksumValue;
+
+        /**
+         * Constructs a new instance.
+         * <p>
+         * This builder use the aspect InputStream.

Review Comment:
   I don't understand this



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to