【问题标题】:Streaming contents from lz4 file using InputStreamReader - Stream Corrupted - Java使用 InputStreamReader 从 lz4 文件流式传输内容 - Stream Corrupted - Java
【发布时间】: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)

谁能帮我解决我做错了什么。

【问题讨论】:

标签: java compression lz4


【解决方案1】:

这段代码运行良好

更新:更新代码以尊重第一条评论

import net.jpountz.lz4.LZ4BlockInputStream;
import net.jpountz.lz4.LZ4BlockOutputStream;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;

public class Lz4TestRead {

    public static void main(String[] args) throws IOException {

        //generate an LZ4 encrypted file
        byte[] buf = new byte[1024];
        try (LZ4BlockOutputStream out = new LZ4BlockOutputStream(new FileOutputStream("/tmp/text.lz4"), 1024);
             ByteArrayInputStream in = new ByteArrayInputStream("hello world this is a LZ4 compressed text".getBytes())) {
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
        }

        StringBuilder sb = new StringBuilder();
        try (ByteArrayInputStream bis = new ByteArrayInputStream(Files.readAllBytes(Path.of("/tmp/text.lz4")));
             LZ4BlockInputStream lz4BlockInputStream = new LZ4BlockInputStream(bis);
             InputStreamReader streamReader = new InputStreamReader(lz4BlockInputStream, StandardCharsets.UTF_8)) {
            while (true) {
                int read = streamReader.read();
                if (read < 0) {
                    break;
                }
                sb.append((char) read);
            }
        }

        System.out.println(sb.toString());

    }
}

但请记住,您必须使用 LZ4BlockOutputStream 压缩数据才能使用 LZ4BlockInputStream 对其进行解压缩

【讨论】:

  • 对我来说,我必须使用 ByteArrayInputStream。所以我将文件读取为 BytearrayInputStream 并尝试读取它。它仍然在 lz4BlockInputStream.read(buf) 处引发相同的异常。而且我使用了正确的 lz4 文件。而且我还有逐字阅读的要求。这就是我使用 InpuytStreamReader 的原因。
  • 我明白了,然后我更新了代码以使用您的方式。
  • 看起来我的 lz4 文件有问题。感谢您的帮助。
猜你喜欢
  • 2014-05-04
  • 1970-01-01
  • 1970-01-01
  • 2011-01-30
  • 2021-11-01
  • 2022-06-10
  • 2016-06-30
  • 2019-05-23
  • 2015-05-14
相关资源
最近更新 更多