Repository navigation
Add dictionary support to Zstd and Brotli compressor input streams #821
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
arturobernalg
wants to merge
1
commit into
apache:master
Choose a base branch
from
arturobernalg:compression-dictionaries
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,7 @@ | |
| import org.apache.commons.compress.compressors.CompressorInputStream; | ||
| import org.apache.commons.compress.utils.InputStreamStatistics; | ||
| import org.apache.commons.io.IOUtils; | ||
| import org.apache.commons.io.build.AbstractStreamBuilder; | ||
| import org.apache.commons.io.input.BoundedInputStream; | ||
|
|
||
| import com.github.luben.zstd.BufferPool; | ||
|
|
@@ -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; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should the builder track a defensive copy? |
||
| 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()); | ||
|
|
||
| if (builder.dict != null) { | ||
| decIS.setDict(builder.dict); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Constructs a new input stream that decompresses zstd-compressed data from the specific input stream. | ||
| * | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| <div class="product-card"><span class="price">0.99 EUR</span><a href="/catalog/item/0">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">1.99 EUR</span><a href="/catalog/item/1">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">2.99 EUR</span><a href="/catalog/item/2">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">3.99 EUR</span><a href="/catalog/item/3">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">4.99 EUR</span><a href="/catalog/item/4">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">5.99 EUR</span><a href="/catalog/item/5">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">6.99 EUR</span><a href="/catalog/item/6">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">7.99 EUR</span><a href="/catalog/item/7">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">8.99 EUR</span><a href="/catalog/item/8">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">9.99 EUR</span><a href="/catalog/item/9">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">10.99 EUR</span><a href="/catalog/item/10">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">11.99 EUR</span><a href="/catalog/item/11">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">12.99 EUR</span><a href="/catalog/item/12">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">13.99 EUR</span><a href="/catalog/item/13">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">14.99 EUR</span><a href="/catalog/item/14">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">15.99 EUR</span><a href="/catalog/item/15">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">16.99 EUR</span><a href="/catalog/item/16">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">17.99 EUR</span><a href="/catalog/item/17">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">18.99 EUR</span><a href="/catalog/item/18">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">19.99 EUR</span><a href="/catalog/item/19">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">20.99 EUR</span><a href="/catalog/item/20">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">21.99 EUR</span><a href="/catalog/item/21">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">22.99 EUR</span><a href="/catalog/item/22">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">23.99 EUR</span><a href="/catalog/item/23">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">24.99 EUR</span><a href="/catalog/item/24">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">25.99 EUR</span><a href="/catalog/item/25">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">26.99 EUR</span><a href="/catalog/item/26">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">27.99 EUR</span><a href="/catalog/item/27">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">28.99 EUR</span><a href="/catalog/item/28">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">29.99 EUR</span><a href="/catalog/item/29">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">30.99 EUR</span><a href="/catalog/item/30">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">31.99 EUR</span><a href="/catalog/item/31">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">32.99 EUR</span><a href="/catalog/item/32">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">33.99 EUR</span><a href="/catalog/item/33">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">34.99 EUR</span><a href="/catalog/item/34">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">35.99 EUR</span><a href="/catalog/item/35">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">36.99 EUR</span><a href="/catalog/item/36">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">37.99 EUR</span><a href="/catalog/item/37">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">38.99 EUR</span><a href="/catalog/item/38">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">39.99 EUR</span><a href="/catalog/item/39">Add to cart</a></div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| <div class="product-card"><span class="price">20.99 EUR</span><a href="/catalog/item/20">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">21.99 EUR</span><a href="/catalog/item/21">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">22.99 EUR</span><a href="/catalog/item/22">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">23.99 EUR</span><a href="/catalog/item/23">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">24.99 EUR</span><a href="/catalog/item/24">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">25.99 EUR</span><a href="/catalog/item/25">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">26.99 EUR</span><a href="/catalog/item/26">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">27.99 EUR</span><a href="/catalog/item/27">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">28.99 EUR</span><a href="/catalog/item/28">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">29.99 EUR</span><a href="/catalog/item/29">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">30.99 EUR</span><a href="/catalog/item/30">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">31.99 EUR</span><a href="/catalog/item/31">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">32.99 EUR</span><a href="/catalog/item/32">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">33.99 EUR</span><a href="/catalog/item/33">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">34.99 EUR</span><a href="/catalog/item/34">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">35.99 EUR</span><a href="/catalog/item/35">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">36.99 EUR</span><a href="/catalog/item/36">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">37.99 EUR</span><a href="/catalog/item/37">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">38.99 EUR</span><a href="/catalog/item/38">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">39.99 EUR</span><a href="/catalog/item/39">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">40.99 EUR</span><a href="/catalog/item/40">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">41.99 EUR</span><a href="/catalog/item/41">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">42.99 EUR</span><a href="/catalog/item/42">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">43.99 EUR</span><a href="/catalog/item/43">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">44.99 EUR</span><a href="/catalog/item/44">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">45.99 EUR</span><a href="/catalog/item/45">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">46.99 EUR</span><a href="/catalog/item/46">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">47.99 EUR</span><a href="/catalog/item/47">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">48.99 EUR</span><a href="/catalog/item/48">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">49.99 EUR</span><a href="/catalog/item/49">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">50.99 EUR</span><a href="/catalog/item/50">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">51.99 EUR</span><a href="/catalog/item/51">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">52.99 EUR</span><a href="/catalog/item/52">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">53.99 EUR</span><a href="/catalog/item/53">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">54.99 EUR</span><a href="/catalog/item/54">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">55.99 EUR</span><a href="/catalog/item/55">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">56.99 EUR</span><a href="/catalog/item/56">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">57.99 EUR</span><a href="/catalog/item/57">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">58.99 EUR</span><a href="/catalog/item/58">Add to cart</a></div> | ||
| <div class="product-card"><span class="price">59.99 EUR</span><a href="/catalog/item/59">Add to cart</a></div> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should the builder track a defensive copy?