Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DIFFERENCES.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ Additional to `GZip`, this version includes [Brotli](https://en.wikipedia.org/wi
which is [widely supported](https://caniuse.com/brotli) by modern browsers.

In order to use this compression you will need first to add the
[com.nixxcode.jvmbrotli](https://mvnrepository.com/artifact/com.nixxcode.jvmbrotli) dependency
[com.aayushatharva.brotli4j](https://mvnrepository.com/artifact/com.aayushatharva.brotli4j) dependency
in your project (Gradle, Maven, etc).

**NOTE**: If you are using Gradle, you may need to also include the native library according to your
Expand Down
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,9 @@

<!-- SPARK DEPENDENCIES -->
<dependency>
<groupId>com.nixxcode.jvmbrotli</groupId>
<artifactId>jvmbrotli</artifactId>
<version>0.2.0</version>
<groupId>com.aayushatharva.brotli4j</groupId>
<artifactId>brotli4j</artifactId>
<version>1.12.0</version>
<optional>true</optional>
</dependency>
</dependencies>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/spark/Response.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public enum Compression {
GZIP_COMPRESSED,
// It will request Spark to compress the output (brotli) and set the BROTLI header
// NOTE: Brotli is a successor to gzip, it is supported by all major web browsers. It provides better compression than gzip.
// Brotli Library is included with `com.nixxcode.jvmbrotli` library
// Brotli Library is included with `com.aayushatharva.brotli4j` library
BROTLI_COMPRESS,
// It will notify Spark that the output is already BROTLI compressed.
// In such case, Spark won't compress the output and will ensure the BROTLI header is set.
Expand Down
29 changes: 17 additions & 12 deletions src/main/java/spark/utils/CompressUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,19 @@ public class CompressUtil {

private static final String GZIP = "gzip";
private static final String BROTLI = "br";

private static final boolean IS_BROTLI_AVAILABLE;
static {
boolean available = false;
try {
Class<?> brotli = Class.forName("com.aayushatharva.brotli4j.Brotli4jLoader");
available = (Boolean) brotli.getMethod("isAvailable").invoke(null);
} catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException |
InvocationTargetException e) {
LOG.debug("Brotli was not found: {} Cause: {}", e.getMessage(), e.getCause());
}
IS_BROTLI_AVAILABLE = available;
}
// Hide constructor
private CompressUtil() {}

Expand Down Expand Up @@ -83,7 +96,7 @@ public static OutputStream checkAndWrap(HttpServletRequest httpRequest, HttpServ
}
/**
* Checks if the HTTP request/response accepts and wants to compress outputStream and i that case wraps the response output stream in a
* {@link java.util.zip.GZIPOutputStream} or 'com.nixxcode.jvmbrotli.enc.BrotliOutputStream'.
* {@link java.util.zip.GZIPOutputStream} or 'com.aayushatharva.brotli4j.encoder.BrotliOutputStream'.
*
* @param httpRequest the HTTP servlet request.
* @param httpResponse the HTTP servlet response.
Expand Down Expand Up @@ -175,15 +188,7 @@ private static void addContentEncodingHeader(HttpServletResponse response, Strin
* @return true if brotli is available
*/
public static boolean isBrotliAvailable() {
boolean available = false;
try {
Class<?> brotli = Class.forName("com.nixxcode.jvmbrotli.common.BrotliLoader");
available = (Boolean) brotli.getMethod("isBrotliAvailable").invoke(null);
} catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException |
InvocationTargetException e) {
LOG.debug("Brotli was not found: {} Cause: {}", e.getMessage(), e.getCause());
}
return available;
return IS_BROTLI_AVAILABLE;
}

/**
Expand All @@ -199,8 +204,8 @@ private static OutputStream getBrotliOutputStream(OutputStream response, int q)
if (q <= 0) q = 10;
if (q > 11) q = 11;
try {
Class<?> brotliOutputStream = Class.forName("com.nixxcode.jvmbrotli.enc.BrotliOutputStream");
Class<?> encoderParams = Class.forName("com.nixxcode.jvmbrotli.enc.Encoder$Parameters");
Class<?> brotliOutputStream = Class.forName("com.aayushatharva.brotli4j.encoder.BrotliOutputStream");
Class<?> encoderParams = Class.forName("com.aayushatharva.brotli4j.encoder.Encoder$Parameters");
Object encoderParamsInstance = encoderParams.getDeclaredConstructor().newInstance();
Object params = encoderParams.getMethod("setQuality", int.class).invoke(encoderParamsInstance, q);
responseStream = (OutputStream) brotliOutputStream.getConstructor(OutputStream.class, encoderParams)
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/spark/examples/brotli/BrotliClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import spark.utils.IOUtils;

import com.nixxcode.jvmbrotli.dec.BrotliInputStream;
import com.aayushatharva.brotli4j.decoder.BrotliInputStream;

/**
* Created by A.Lepe (2022-07-27) based on GzipClient
Expand Down