|
7 | 7 | import com.google.common.io.BaseEncoding; |
8 | 8 | import com.google.common.net.HostAndPort; |
9 | 9 | import com.google.common.net.MediaType; |
| 10 | +import org.brotli.dec.BrotliInputStream; |
10 | 11 | import io.netty.buffer.ByteBuf; |
11 | 12 | import io.netty.handler.codec.http.HttpHeaders; |
12 | 13 | import io.netty.handler.codec.http.HttpRequest; |
|
16 | 17 | import org.slf4j.Logger; |
17 | 18 | import org.slf4j.LoggerFactory; |
18 | 19 |
|
| 20 | +import java.io.InputStream; |
19 | 21 | import java.io.ByteArrayInputStream; |
20 | 22 | import java.io.ByteArrayOutputStream; |
21 | 23 | import java.io.IOException; |
@@ -79,11 +81,11 @@ public static long getHeaderSize(HttpHeaders headers) { |
79 | 81 | /** |
80 | 82 | * Decompresses the gzipped byte stream. |
81 | 83 | * |
82 | | - * @param fullMessage gzipped byte stream to decomress |
| 84 | + * @param fullMessage gzipped byte stream to decompress |
83 | 85 | * @return decompressed bytes |
84 | 86 | * @throws DecompressionException thrown if the fullMessage cannot be read or decompressed for any reason |
85 | 87 | */ |
86 | | - public static byte[] decompressContents(byte[] fullMessage) throws DecompressionException { |
| 88 | + public static byte[] decompressGZIPContents(byte[] fullMessage) throws DecompressionException { |
87 | 89 | InflaterInputStream gzipReader = null; |
88 | 90 | ByteArrayOutputStream uncompressed; |
89 | 91 | try { |
@@ -113,6 +115,42 @@ public static byte[] decompressContents(byte[] fullMessage) throws Decompression |
113 | 115 | } |
114 | 116 |
|
115 | 117 | /** |
| 118 | + * Decompresses the brotli byte stream |
| 119 | + * |
| 120 | + * @param fullMessage brotli byte stream to decompress |
| 121 | + * @return decompressed bytes |
| 122 | + * @throws DecompressionException thrown if the fullMessage cannot be read or decompressed for any reason |
| 123 | + */ |
| 124 | + public static byte[] decompressBrotliContents(byte[] fullMessage) throws DecompressionException { |
| 125 | + InputStream brotliReader = null; |
| 126 | + ByteArrayOutputStream uncompressed; |
| 127 | + try { |
| 128 | + brotliReader = new BrotliInputStream(new ByteArrayInputStream(fullMessage)); |
| 129 | + |
| 130 | + uncompressed = new ByteArrayOutputStream(fullMessage.length); |
| 131 | + |
| 132 | + byte[] decompressBuffer = new byte[DECOMPRESS_BUFFER_SIZE]; |
| 133 | + int bytesRead; |
| 134 | + while ((bytesRead = brotliReader.read(decompressBuffer)) > -1) { |
| 135 | + uncompressed.write(decompressBuffer, 0, bytesRead); |
| 136 | + } |
| 137 | + |
| 138 | + fullMessage = uncompressed.toByteArray(); |
| 139 | + } catch (IOException e) { |
| 140 | + throw new DecompressionException("Unable to decompress response", e); |
| 141 | + } finally { |
| 142 | + try { |
| 143 | + if (brotliReader != null) { |
| 144 | + brotliReader.close(); |
| 145 | + } |
| 146 | + } catch (IOException e) { |
| 147 | + log.warn("Unable to close brotli stream", e); |
| 148 | + } |
| 149 | + } |
| 150 | + return fullMessage; |
| 151 | + } |
| 152 | + |
| 153 | + /** |
116 | 154 | * Returns true if the content type string indicates textual content. Currently these are any Content-Types that start with one of the |
117 | 155 | * following: |
118 | 156 | * <pre> |
|
0 commit comments