1

我从 HTTP 请求的响应开始:

InputStream responseInputStream = response.getEntityInputStream()

我需要对该响应进行 gzip 压缩,以便将其上传到 s3 并将其压缩保存:

this.s3.putObject(new PutObjectRequest(bucketName, key, gzippedResponseInputStream, meta));

我知道我可以将byte[]数组取出,responseInputStream然后将它们压缩成一个新的InputStream. 但是,对于大量数据,这可能非常低效。

我知道在 SO 上也有人问过类似的问题,但我还没有找到任何似乎可以解决从 a 开始InputStream并以 gzipped 结束的特定需求InputStream

谢谢你的帮助!

4

2 回答 2

3

我认为您正在寻找PipedInputStream

这是如何做到的。

public InputStrema getGZipStream() {
    final PipedOutputStream pos = new PipedOutputStream();
    PipedInputStream pis = new PipedInputStream();

    try (final InputStream responseInputStream = response.getEntityInputStream();
    ){
        pis.connect(pos);

        Thread thread = new Thread() {
            public void run () {
                startWriting(pos, responseInputStream);
            }
        };
        thread.start();
    } catch(Exception e) {
        e.printStackTrace();
    }

    return pis;
}

public void startWriting(OutputStream out, InputStream in) {
    try (GZIPOutputStream gOut = GZIPOutputStream(out);) {
        byte[] buffer = new byte[10240];
        int len = -1;
        while ((len = in.read(buffer)) != -1) {
            gOut.write(buffer, 0, len);
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        try {
            out.close();
        } catch( Exception e) {
            e.printStackTrace();
        }
    }
}

我还没有测试过这个代码,如果它有效,请告诉我。

于 2016-08-12T19:49:50.373 回答
3
public final class Example {
    public static void main(String[] args) throws IOException, InterruptedException {
        final PipedInputStream inputStream = new PipedInputStream();
        final PipedOutputStream outputStream = new PipedOutputStream(inputStream);

        Thread compressorThread = new Thread() {
            @Override
            public void run() {
                try (FileInputStream dataSource = new FileInputStream(args[0])) {
                    try (GZIPOutputStream sink = new GZIPOutputStream(outputStream)) {
                        final byte[] buffer = new byte[8 * 1024];
                        for (int bytesRead = dataSource.read(buffer); bytesRead >= 0; bytesRead = dataSource.read(buffer)) {
                            sink.write(buffer, 0, bytesRead);
                        }
                    }
                } catch (IOException ex) {
                    //TODO handle exception -> maybe use callable + executor
                }
            }
        };
        compressorThread.start();

        try (FileOutputStream destination = new FileOutputStream(args[1])) {
            final byte[] buffer = new byte[8 * 1024];
            for (int bytesRead = inputStream.read(buffer); bytesRead >= 0; bytesRead = inputStream.read(buffer)) {
                destination.write(buffer, 0, bytesRead);
            }
        }

        compressorThread.join();
    }

}

你是对的,我之前的例子是错误的。您可以使用管道流。这里的问题是您不能使用来自同一线程的输入和输出流。也不要忘记join()在写作线程上。您可以通过提供两个参数来测试我的示例:

  • args[0] -> 源文件
  • args[1] -> 写入压缩内容的目的地

PS:@11thdimension 使用他的管道流解决方案快了几分钟,所以如果你觉得这有帮助,请接受他的回答

于 2016-08-12T18:50:50.083 回答