• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

box / box-java-sdk / #4423

19 Feb 2025 11:15AM UTC coverage: 71.967% (+0.04%) from 71.932%
#4423

Pull #1287

github

web-flow
Merge db72c1054 into 29b651987
Pull Request #1287: feat: Support `zstd` encoding

34 of 42 new or added lines in 3 files covered. (80.95%)

3 existing lines in 3 files now uncovered.

8192 of 11383 relevant lines covered (71.97%)

0.72 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.55
/src/main/java/com/box/sdk/ZstdInterceptor.java
1
package com.box.sdk;
2

3
import com.github.luben.zstd.ZstdInputStream;
4
import java.io.ByteArrayOutputStream;
5
import java.io.IOException;
6
import okhttp3.Interceptor;
7
import okhttp3.Request;
8
import okhttp3.Response;
9
import okhttp3.ResponseBody;
10
import org.jetbrains.annotations.NotNull;
11

12
/**
13
 * Interceptor that adds zstd compression support to API requests.
14
 * This interceptor adds zstd to the Accept-Encoding header and handles decompression of zstd responses.
15
 */
16
public class ZstdInterceptor implements Interceptor {
1✔
17
    @NotNull
18
    @Override
19
    public Response intercept(Chain chain) throws IOException {
20
        Request request = chain.request();
1✔
21

22
        // Add zstd to the Accept-Encoding header
23
        String acceptEncoding;
24
        String acceptEncodingHeader = request.header("Accept-Encoding");
1✔
25
        if (acceptEncodingHeader == null) {
1✔
NEW
26
            acceptEncoding = "zstd";
×
27
        } else {
28
            acceptEncoding = acceptEncodingHeader + ", zstd";
1✔
29
        }
30

31
        Request compressedRequest = request.newBuilder()
1✔
32
            .removeHeader("Accept-Encoding")
1✔
33
            .addHeader("Accept-Encoding", acceptEncoding)
1✔
34
            .build();
1✔
35

36
        Response response = chain.proceed(compressedRequest);
1✔
37
        String contentEncoding = response.header("Content-Encoding");
1✔
38

39
        // Only handle zstd encoded responses, let OkHttp handle gzip and others
40
        if (contentEncoding == null || !contentEncoding.equalsIgnoreCase("zstd")) {
1✔
41
            return response;
1✔
42
        }
43

44
        ResponseBody originalBody = response.body();
1✔
45
        if (originalBody == null) {
1✔
NEW
46
            return response;
×
47
        }
48

49
        // Buffer the entire response body
50
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
1✔
51
        try (ZstdInputStream zstdStream = new ZstdInputStream(originalBody.byteStream())) {
1✔
52
            byte[] buffer = new byte[8192];
1✔
53
            int bytesRead;
54
            while ((bytesRead = zstdStream.read(buffer)) != -1) {
1✔
55
                outputStream.write(buffer, 0, bytesRead);
1✔
56
            }
57
        }
58
        byte[] decompressedBytes = outputStream.toByteArray();
1✔
59

60
        // Create a new response body that serves the buffered content
61
        ResponseBody decompressedBody = ResponseBody.create(
1✔
62
            decompressedBytes,
63
            originalBody.contentType()
1✔
64
        );
65

66
        return response.newBuilder()
1✔
67
            .body(decompressedBody)
1✔
68
            .addHeader("X-Content-Encoding", contentEncoding)
1✔
69
            .removeHeader("Content-Encoding")
1✔
70
            .removeHeader("Content-Length")
1✔
71
            .build();
1✔
72
    }
73
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc