DeflaterOutputStream:写入超出流的末尾

时间:2013-01-09 14:28:02

标签: java

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

错误:

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

1 个答案:

答案 0 :(得分:8)

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