如何读取 brotli 压缩字符串?
How to read a brotli compressed string?
我从网站上得到一个 brotli 压缩的 json 字符串。我要解压阅读
当我使用来自响应的输入流时,我能够使用
正确读取它
new BufferedReader(new InputStreamReader(new BrotliInputStream(response.getEntity().getContent())));
而当我将响应保存在字符串中并读取它时
BufferedReader rd = new BufferedReader(new InputStreamReader(new BrotliInputStream(IOUtils.toInputStream(responseAsString, "UTF-8"))));
StringBuilder result = new StringBuilder();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
System.out.println(result);
我遇到以下异常:
Exception in thread "main" java.io.IOException: Brotli stream decoding failed
at org.brotli.dec.BrotliInputStream.read(BrotliInputStream.java:167)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.BufferedReader.fill(BufferedReader.java:161)
at java.io.BufferedReader.readLine(BufferedReader.java:324)
at java.io.BufferedReader.readLine(BufferedReader.java:389)
at com.brotli.test.BrotliStringTest.main(BrotliStringTest.java:113)
Caused by: org.brotli.dec.BrotliRuntimeException: Unused space
at org.brotli.dec.Decode.readHuffmanCodeLengths(Decode.java:226)
at org.brotli.dec.Decode.readHuffmanCode(Decode.java:296)
at org.brotli.dec.HuffmanTreeGroup.decode(HuffmanTreeGroup.java:53)
at org.brotli.dec.Decode.readMetablockHuffmanCodesAndContextMaps(Decode.java:528)
at org.brotli.dec.Decode.decompress(Decode.java:621)
at org.brotli.dec.BrotliInputStream.read(BrotliInputStream.java:161)
... 8 more
编辑 1:
我尝试使用 Jsoup,发现它仅支持 Gzipped 流,不支持其 HttpConnection class 中的 BrotliInputStream。对此有任何指示吗?
我是这样解决的:-
import org.brotli.dec.BrotliInputStream;
if(response.getLastHeader("content-encoding").getValue().equals("br")) { // check if getting brotli compressed stream
rd = new BufferedReader(new InputStreamReader(new BrotliInputStream(response.getEntity().getContent())));
}
else {
rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
}
这适用于 brotli/non-brotli(gzipped 等)流。
我从网站上得到一个 brotli 压缩的 json 字符串。我要解压阅读
当我使用来自响应的输入流时,我能够使用
正确读取它new BufferedReader(new InputStreamReader(new BrotliInputStream(response.getEntity().getContent())));
而当我将响应保存在字符串中并读取它时
BufferedReader rd = new BufferedReader(new InputStreamReader(new BrotliInputStream(IOUtils.toInputStream(responseAsString, "UTF-8"))));
StringBuilder result = new StringBuilder();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
System.out.println(result);
我遇到以下异常:
Exception in thread "main" java.io.IOException: Brotli stream decoding failed
at org.brotli.dec.BrotliInputStream.read(BrotliInputStream.java:167)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.BufferedReader.fill(BufferedReader.java:161)
at java.io.BufferedReader.readLine(BufferedReader.java:324)
at java.io.BufferedReader.readLine(BufferedReader.java:389)
at com.brotli.test.BrotliStringTest.main(BrotliStringTest.java:113)
Caused by: org.brotli.dec.BrotliRuntimeException: Unused space
at org.brotli.dec.Decode.readHuffmanCodeLengths(Decode.java:226)
at org.brotli.dec.Decode.readHuffmanCode(Decode.java:296)
at org.brotli.dec.HuffmanTreeGroup.decode(HuffmanTreeGroup.java:53)
at org.brotli.dec.Decode.readMetablockHuffmanCodesAndContextMaps(Decode.java:528)
at org.brotli.dec.Decode.decompress(Decode.java:621)
at org.brotli.dec.BrotliInputStream.read(BrotliInputStream.java:161)
... 8 more
编辑 1:
我尝试使用 Jsoup,发现它仅支持 Gzipped 流,不支持其 HttpConnection class 中的 BrotliInputStream。对此有任何指示吗?
我是这样解决的:-
import org.brotli.dec.BrotliInputStream;
if(response.getLastHeader("content-encoding").getValue().equals("br")) { // check if getting brotli compressed stream
rd = new BufferedReader(new InputStreamReader(new BrotliInputStream(response.getEntity().getContent())));
}
else {
rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
}
这适用于 brotli/non-brotli(gzipped 等)流。