3

deflaterstream 处理了“超出流结束写入”错误。我跟踪了错误,它显示流显然已经完成。但是,在我的功能开始时,将流创建为全新的,并且不要在最后调用关闭或完成..

错误:

SEVERE: null
java.io.IOException: write beyond end of stream
    at java.util.zip.DeflaterOutputStream.write(DeflaterOutputStream.java:102)
    at evidence.SaveStateManager.saveIncremental(SaveStateManager.java:82)

代码:

public static void saveIncremental(int textureId, int layer, int shot) {
    try {
        bOut = new FileOutputStream("saves/p-" + layer + "-" + shot + ".gz");
        gzipOut = new DeflaterOutputStream (bOut, deflater);

        glPushAttrib(GL_ALL_ATTRIB_BITS);

        glBindTexture(GL_TEXTURE_2D, textureId);

        int bpp = 4; 
        ByteBuffer buffer = BufferUtils.createByteBuffer(SAVE_WIDTH * SAVE_HEIGHT * bpp);
        glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer );

        byte[] bytes = new byte[1024];
        while (buffer.hasRemaining()) {
            int len = Math.min(buffer.remaining(), bytes.length);
            buffer.get(bytes, 0, len);
            try {
                gzipOut.write(bytes, 0, len);
            } catch (IOException ex) {
                Logger.getLogger(SaveStateManager.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        glBindTexture(GL_TEXTURE_2D, 0);
        glPopAttrib();

        gzipOut.finish();
        gzipOut.close();
        bOut.close();
    } catch (Exception ex) {
        Logger.getLogger(SaveStateManager.class.getName()).log(Level.SEVERE, null, ex);
    }
}
4

1 回答 1

10

Deflator 是有状态的,你正在重用它。

gzipOut = new DeflaterOutputStream (bOut, deflater);

来自 DeflaterOutputStream 的来源

public void write(byte[] b, int off, int len) throws IOException {
    if (def.finished()) {
        throw new IOException("write beyond end of stream");
于 2013-01-09T14:33:51.973 回答