Copilot commented on code in PR #821:
URL: https://github.com/apache/commons-compress/pull/821#discussion_r4205945759
##########
src/main/java/org/apache/commons/compress/compressors/zstandard/ZstdCompressorInputStream.java:
##########
@@ -44,9 +45,80 @@
*/
public class ZstdCompressorInputStream extends CompressorInputStream
implements InputStreamStatistics {
+ // @formatter:off
+ /**
+ * Builds a new {@link ZstdCompressorInputStream}.
+ *
+ * <p>
+ * For example:
+ * </p>
+ * <pre>{@code
+ * ZstdCompressorInputStream s = ZstdCompressorInputStream.builder()
+ * .setPath(path)
+ * .setDict(dictionary)
+ * .get();
+ * }
+ * </pre>
+ *
+ * @see #get()
+ * @since 1.29.0
+ */
+ // @formatter:on
+ public static final class Builder extends
AbstractStreamBuilder<ZstdCompressorInputStream, Builder> {
+
+ private byte[] dict;
+
+ /**
+ * Constructs a new builder of {@link ZstdCompressorInputStream}.
+ */
+ public Builder() {
+ // empty
+ }
+
+ @Override
+ public ZstdCompressorInputStream get() throws IOException {
+ return new ZstdCompressorInputStream(this);
+ }
+
+ /**
+ * Sets the dictionary used to decompress the input.
+ * <p>
+ * The input must have been compressed with the same dictionary, for
example with {@link ZstdCompressorOutputStream.Builder#setDict(byte[])}. The
+ * dictionary may be a trained Zstandard dictionary or raw content.
The value {@code null} means no dictionary.
+ * </p>
+ *
+ * @param dict The dictionary buffer.
+ * @return {@code this} instance.
+ * @see <a
href="https://facebook.github.io/zstd/zstd_manual.html#Chapter14">Zstd manual
Chapter14</a>
+ */
+ public Builder setDict(final byte[] dict) {
+ this.dict = dict;
+ return this;
+ }
+ }
+
+ /**
+ * Constructs a new builder of {@link ZstdCompressorInputStream}.
+ *
+ * @return a new builder of {@link ZstdCompressorInputStream}.
+ * @since 1.29.0
+ */
+ public static Builder builder() {
+ return new Builder();
+ }
+
private final BoundedInputStream countingStream;
private final ZstdInputStream decIS;
+ @SuppressWarnings("resource") // Caller closes
+ private ZstdCompressorInputStream(final Builder builder) throws
IOException {
+ this.decIS = new ZstdInputStream(
+ countingStream =
BoundedInputStream.builder().setInputStream(builder.getInputStream()).setPropagateClose(false).get());
Review Comment:
`setPropagateClose(false)` prevents `decIS.close()` from closing the stream
returned by `builder.getInputStream()`. With the documented `.setPath(...)`
usage, that file stream has no other owner or reference, so closing each
compressor leaks its file descriptor. Let this builder-created wrapper
propagate close (as the other compressor builders do).
--
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]