强制刷新java中的GZIPOutputStream

时间:2010-09-03 22:49:15

标签: java gzip gzipoutputstream

我们正在开发一个程序,我们需要刷新(强制压缩和发送数据)GZIPOutputStream。问题是,GZIPOutputStream的flush方法不能按预期工作(强制压缩和发送数据),而是Stream等待更多数据进行有效的数据压缩。

当你调用完成后,数据被压缩并通过输出流发送,但GZIPOutputStream(不是底层流)将被关闭,所以我们不能写更多的数据,直到我们创建一个新的GZIPOutputStream,这需要时间和性能。

希望任何人都可以提供帮助。

最好的问候。

6 个答案:

答案 0 :(得分:10)

我还没有尝试过这个,在我们掌握Java 7之前,这个建议没有用,但是GZIPOutputStream的{​​{3}}方法的文档继承自{{1}依赖于在构造时指定的刷新模式 flush()(与DeflaterOutputStream相关)来决定是否刷新要压缩的待处理数据。 Deflater#SYNC_FLUSH在施工时也接受syncFlush参数。

听起来你想要使用the syncFlush argument或甚至Deflator#SYNC_FLUSH,但是,在深入挖掘这一点之前,首先尝试使用Deflater#FULL_FLUSHthe two-argument并通过GZIPOutputStream参数的true。这将激活你想要的冲洗行为。

答案 1 :(得分:9)

我没有找到另一个工作的答案。它仍然拒绝刷新,因为GZIPOutputStream正在使用的本机代码保留在数据上。

值得庆幸的是,我发现有人在Apache Tomcat项目中实现了FlushableGZIPOutputStream。这是神奇的部分:

@Override
public synchronized void flush() throws IOException {
    if (hasLastByte) {
        // - do not allow the gzip header to be flushed on its own
        // - do not do anything if there is no data to send

        // trick the deflater to flush
        /**
         * Now this is tricky: We force the Deflater to flush its data by
         * switching compression level. As yet, a perplexingly simple workaround
         * for
         * http://developer.java.sun.com/developer/bugParade/bugs/4255743.html
         */
        if (!def.finished()) {
            def.setLevel(Deflater.NO_COMPRESSION);
            flushLastByte();
            flagReenableCompression = true;
        }
    }
    out.flush();
}

你可以在这个jar中找到整个类(如果你使用Maven):

<dependency>
    <groupId>org.apache.tomcat</groupId>
    <artifactId>tomcat-coyote</artifactId>
    <version>7.0.8</version>
</dependency>

或者只是去抓取源代码FlushableGZIPOutputStream.java

它是在Apache-2.0许可下发布的。

答案 2 :(得分:1)

Bug ID 4813885处理此问题。 2006年9月9日提交的“DamonHD”评论(大约是bug报告的一半)包含FlushableGZIPOutputStream的一个例子,他在Jazzlib's net.sf.jazzlib.DeflaterOutputStream之上建立了/** * Substitute for GZIPOutputStream that maximises compression and has a usable * flush(). This is also more careful about its output writes for efficiency, * and indeed buffers them to minimise the number of write()s downstream which * is especially useful where each write() has a cost such as an OS call, a disc * write, or a network packet. */ public class FlushableGZIPOutputStream extends net.sf.jazzlib.DeflaterOutputStream { private final CRC32 crc = new CRC32(); private final static int GZIP_MAGIC = 0x8b1f; private final OutputStream os; /** Set when input has arrived and not yet been compressed and flushed downstream. */ private boolean somethingWritten; public FlushableGZIPOutputStream(final OutputStream os) throws IOException { this(os, 8192); } public FlushableGZIPOutputStream(final OutputStream os, final int bufsize) throws IOException { super(new FilterOutputStream(new BufferedOutputStream(os, bufsize)) { /** Suppress inappropriate/inefficient flush()es by DeflaterOutputStream. */ @Override public void flush() { } }, new net.sf.jazzlib.Deflater(net.sf.jazzlib.Deflater.BEST_COMPRESSION, true)); this.os = os; writeHeader(); crc.reset(); } public synchronized void write(byte[] buf, int off, int len) throws IOException { somethingWritten = true; super.write(buf, off, len); crc.update(buf, off, len); } /** * Flush any accumulated input downstream in compressed form. We overcome * some bugs/misfeatures here so that: * <ul> * <li>We won't allow the GZIP header to be flushed on its own without real compressed * data in the same write downstream. * <li>We ensure that any accumulated uncompressed data really is forced through the * compressor. * <li>We prevent spurious empty compressed blocks being produced from successive * flush()es with no intervening new data. * </ul> */ @Override public synchronized void flush() throws IOException { if (!somethingWritten) { return; } // We call this to get def.flush() called, // but suppress the (usually premature) out.flush() called internally. super.flush(); // Since super.flush() seems to fail to reliably force output, // possibly due to over-cautious def.needsInput() guard following def.flush(), // we try to force the issue here by bypassing the guard. int len; while((len = def.deflate(buf, 0, buf.length)) > 0) { out.write(buf, 0, len); } // Really flush the stream below us... os.flush(); // Further flush()es ignored until more input data data written. somethingWritten = false; } public synchronized void close() throws IOException { if (!def.finished()) { def.finish(); do { int len = def.deflate(buf, 0, buf.length); if (len <= 0) { break; } out.write(buf, 0, len); } while (!def.finished()); } // Write trailer out.write(generateTrailer()); out.close(); } // ... }

供参考,这是一个(重新格式化)提取物:

{{1}}

您可能会发现它很有用。

答案 3 :(得分:1)

Android也有同样的问题。接受者答案不起作用,因为def.setLevel(Deflater.NO_COMPRESSION);抛出异常。根据{{​​1}}方法,它会更改flush的压缩级别。所以我想在编写数据之前应该调用压缩,但我不确定。

还有其他2个选项:

  • 如果您的应用的API级别高于19,那么您可以尝试使用构造函数with syncFlush param
  • 另一个解决方案是使用jzlib

答案 4 :(得分:1)

此代码在我的应用程序中对我很有用。

public class StreamingGZIPOutputStream extends GZIPOutputStream {

    public StreamingGZIPOutputStream(OutputStream out) throws IOException {
        super(out);
    }

    @Override
    protected void deflate() throws IOException {
        // SYNC_FLUSH is the key here, because it causes writing to the output
        // stream in a streaming manner instead of waiting until the entire
        // contents of the response are known.  for a large 1 MB json example
        // this took the size from around 48k to around 50k, so the benefits
        // of sending data to the client sooner seem to far outweigh the
        // added data sent due to less efficient compression
        int len = def.deflate(buf, 0, buf.length, Deflater.SYNC_FLUSH);
        if (len > 0) {
            out.write(buf, 0, len);
        }
    }

}

答案 5 :(得分:0)

正如@seh所说,这很好用:

ByteArrayOutputStream stream = new ByteArrayOutputStream();

// the second param need to be true
GZIPOutputStream gzip = new GZIPOutputStream(stream,  true);
gzip.write( .. );
gzip.flush();

...
gzip.close()
相关问题