-
Notifications
You must be signed in to change notification settings - Fork 186
feat: Support zstd encoding for downloads
#1287
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
Merged
Merged
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
1bd342f
feat: Support zstd encoding
congminh1254 61e1f91
Fix code
congminh1254 84a017a
Update integration tests
congminh1254 715c11d
Merge branch 'main' into java-sdk-zstd
congminh1254 b06694e
Update README.md
congminh1254 d394b00
Update build.gradle
congminh1254 0496bea
Merge branch 'main' into java-sdk-zstd
lukaszsocha2 db72c10
Fix ZSTD
congminh1254 687b239
fix keeping stream in-memory
lukaszsocha2 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
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 |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| package com.box.sdk; | ||
|
|
||
| import com.github.luben.zstd.ZstdInputStream; | ||
| import java.io.ByteArrayOutputStream; | ||
| import java.io.IOException; | ||
| import okhttp3.Interceptor; | ||
| import okhttp3.Request; | ||
| import okhttp3.Response; | ||
| import okhttp3.ResponseBody; | ||
| import org.jetbrains.annotations.NotNull; | ||
|
|
||
| /** | ||
| * Interceptor that adds zstd compression support to API requests. | ||
| * This interceptor adds zstd to the Accept-Encoding header and handles decompression of zstd responses. | ||
| */ | ||
| public class ZstdInterceptor implements Interceptor { | ||
| @NotNull | ||
| @Override | ||
| public Response intercept(Chain chain) throws IOException { | ||
| Request request = chain.request(); | ||
|
|
||
| // Add zstd to the Accept-Encoding header | ||
| String acceptEncoding; | ||
| String acceptEncodingHeader = request.header("Accept-Encoding"); | ||
| if (acceptEncodingHeader == null) { | ||
| acceptEncoding = "zstd"; | ||
| } else { | ||
| acceptEncoding = acceptEncodingHeader + ", zstd"; | ||
| } | ||
|
|
||
| Request compressedRequest = request.newBuilder() | ||
| .removeHeader("Accept-Encoding") | ||
| .addHeader("Accept-Encoding", acceptEncoding) | ||
| .build(); | ||
|
|
||
| Response response = chain.proceed(compressedRequest); | ||
| String contentEncoding = response.header("Content-Encoding"); | ||
|
|
||
| // Only handle zstd encoded responses, let OkHttp handle gzip and others | ||
| if (contentEncoding == null || !contentEncoding.equalsIgnoreCase("zstd")) { | ||
| return response; | ||
| } | ||
|
|
||
| ResponseBody originalBody = response.body(); | ||
| if (originalBody == null) { | ||
| return response; | ||
| } | ||
|
|
||
| // Buffer the entire response body | ||
| ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); | ||
| try (ZstdInputStream zstdStream = new ZstdInputStream(originalBody.byteStream())) { | ||
| byte[] buffer = new byte[8192]; | ||
lukaszsocha2 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| int bytesRead; | ||
| while ((bytesRead = zstdStream.read(buffer)) != -1) { | ||
| outputStream.write(buffer, 0, bytesRead); | ||
| } | ||
| } | ||
| byte[] decompressedBytes = outputStream.toByteArray(); | ||
|
|
||
| // Create a new response body that serves the buffered content | ||
| ResponseBody decompressedBody = ResponseBody.create( | ||
| decompressedBytes, | ||
| originalBody.contentType() | ||
| ); | ||
|
|
||
| return response.newBuilder() | ||
| .body(decompressedBody) | ||
| .addHeader("X-Content-Encoding", contentEncoding) | ||
| .removeHeader("Content-Encoding") | ||
| .removeHeader("Content-Length") | ||
| .build(); | ||
| } | ||
| } | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.