Skip to content

Commit

Permalink
Add optional support for brotli content encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastian-nagel committed Nov 8, 2024
1 parent b216255 commit 5c6171f
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.brotli</groupId>
<artifactId>dec</artifactId>
<version>0.1.2</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
</dependencies>

<properties>
Expand Down
22 changes: 22 additions & 0 deletions src/org/netpreserve/jwarc/BrotliUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* SPDX-License-Identifier: Apache-2.0
* Copyright (C) 2024 National Library of Australia and the jwarc contributors
*/

package org.netpreserve.jwarc;

import java.io.IOException;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;

import org.brotli.dec.BrotliInputStream;

/**
* Utility class to read brotli-encoded data, based on org.brotli:dec.
*/
public final class BrotliUtils {

public static ReadableByteChannel brotliChannel(ReadableByteChannel brotli) throws IOException {
return Channels.newChannel(new BrotliInputStream(Channels.newInputStream(brotli)));
}
}
7 changes: 6 additions & 1 deletion src/org/netpreserve/jwarc/DecodedBody.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ private DecodedBody(ReadableByteChannel channel, Encoding encoding) throws IOExc
this.channel = IOUtils.gunzipChannel(channel);
break;
case BROTLI:
throw new IOException("Brotli encoding not yet supported");
try {
this.channel = BrotliUtils.brotliChannel(channel);
} catch (NoClassDefFoundError e) {
throw new IOException("Brotli decoder not found, please install org.brotli:dec", e);
}
break;
default:
throw new IOException("Unsupported encoding");
}
Expand Down
2 changes: 2 additions & 0 deletions src/org/netpreserve/jwarc/HttpMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public MessageBody bodyDecoded() throws IOException {
} else if (contentEncodings.get(0).equalsIgnoreCase("gzip")
|| contentEncodings.get(0).equalsIgnoreCase("x-gzip")) {
return DecodedBody.create(payload, DecodedBody.Encoding.GZIP);
} else if (contentEncodings.get(0).equalsIgnoreCase("br")) {
return DecodedBody.create(payload, DecodedBody.Encoding.BROTLI);
} else if (contentEncodings.get(0).equalsIgnoreCase("deflate")) {
return DecodedBody.create(payload, DecodedBody.Encoding.DEFLATE);
} else {
Expand Down

0 comments on commit 5c6171f

Please sign in to comment.