我从一个网站获取了一段经过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流,不支持BrotliInputStream在其HttpConnection类中。对此有什么建议吗?
BrotliInputStream
的两个示例不相同呢? - Scary WombatHttpResponse
返回的InputStream
中读取,而在另一个例子中,我首先将字符串转换为InputStream
以供BrotliInputStream
使用。但是当我尝试使用字符串时,它不起作用。 - Abhash Upadhyaya