使用InputStreamReader从lz4文件流式传输内容-流损坏-Java

使用InputStreamReader从lz4文件流式传输内容-流损坏-Java,java,compression,lz4,Java,Compression,Lz4,我正在尝试从lz4压缩文件流式传输数据,并将其写入StringBuilder。但我得到的流是损坏的例外。下面是我的代码 import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; import java.io.InputStreamReader; import java.nio.charset.StandardCharsets; impo

我正在尝试从lz4压缩文件流式传输数据,并将其写入
StringBuilder
。但我得到的流是损坏的例外。下面是我的代码

    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();
                }
        
        
            }
        }
谁能帮我找出我做错了什么。

这段代码很好用

更新:更新代码以尊重第一条注释

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());

    }
}
import net.jpountz.lz4.LZ4BlockInputStream;
导入net.jpountz.lz4.LZ4BlockOutputStream;
导入java.io.*;
导入java.nio.charset.StandardCharset;
导入java.nio.file.Files;
导入java.nio.file.Path;
公共级Lz4TestRead{
公共静态void main(字符串[]args)引发IOException{
//生成LZ4加密文件
字节[]buf=新字节[1024];
try(LZ4BlockOutputStream out=newlz4blockoutputstream(newfileoutputstream(“/tmp/text.lz4”),1024);
ByteArrayInputStream in=new ByteArrayInputStream(“你好,世界,这是一个LZ4压缩文本“.getBytes())){
内伦;
而((len=in.read(buf))>0){
out.write(buf,0,len);
}
}
StringBuilder sb=新的StringBuilder();
try(ByteArrayInputStream bis=newbytearrayinputstream(Files.readAllBytes(Path.of(“/tmp/text.lz4”)));
LZ4BlockInputStream LZ4BlockInputStream=新LZ4BlockInputStream(bis);
InputStreamReader streamReader=新的InputStreamReader(lz4BlockInputStream,StandardCharsets.UTF_8)){
while(true){
int read=streamReader.read();
如果(读取<0){
打破
}
某人追加((字符)读);
}
}
System.out.println(sb.toString());
}
}

但是请记住,为了能够使用LZ4BlockInputStream解压数据,您必须使用LZ4BlockOutputStream压缩数据

也许这篇文章对我有帮助,我必须使用ByteArrayInputStream。所以我以BytearrayInputStream的形式读取文件,并尝试读取它。它仍然在lz4BlockInputStream.read(buf)上引发相同的异常。而且我使用了正确的lz4文件。我也有逐字阅读的要求。这就是我使用InpuytStreamReader的原因。我明白了,然后我更新了代码以使用您的方式。看起来是lz4文件的问题。谢谢你的帮助。
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());

    }
}