【发布时间】:2020-12-02 07:30:05
【问题描述】:
我正在尝试从 lz4 压缩文件中流式传输数据并将其写入StringBuilder。但我得到 Stream is corrupted 异常。下面是我的代码。
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import net.jpountz.lz4.LZ4BlockInputStream;
import org.apache.commons.io.FileUtils;
public class Lz4TestRead {
public static void main(String[] args) throws IOException {
byte[] data = FileUtils.readFileToByteArray(new File("D:\\sparker\\input\\payload.lz4"));
try(ByteArrayInputStream bis = new ByteArrayInputStream(data);
LZ4BlockInputStream lz4BlockInputStream = new LZ4BlockInputStream(bis)) {
InputStreamReader streamReader = new InputStreamReader(lz4BlockInputStream, StandardCharsets.UTF_8);
StringBuilder sb = new StringBuilder();
while (true) {
int read = streamReader.read();
if (read < 0) {
break;
}
sb.append((char) read);
}
streamReader.close();
System.out.println(sb.toString());
}catch (Exception ex) {
ex.printStackTrace();
}
}
}
我遇到了异常
java.io.IOException: Stream is corrupted
at net.jpountz.lz4.LZ4BlockInputStream.refill(LZ4BlockInputStream.java:202)
at net.jpountz.lz4.LZ4BlockInputStream.read(LZ4BlockInputStream.java:157)
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 sun.nio.cs.StreamDecoder.read0(StreamDecoder.java:127)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:112)
at java.io.InputStreamReader.read(InputStreamReader.java:168)
at com.test.Lz4TestRead.main(Lz4TestRead.java:21)
谁能帮我解决我做错了什么。
【问题讨论】:
-
也许这篇文章对你有帮助stackoverflow.com/questions/36012183/…
标签: java compression lz4