0

原因はわかっていると思いますが、コードでいつ発生しているのかを特定できないため、これは正直イライラします。基本的に、この割り当てでは、入力ストリームを読み取り、それを 128 バイトのブロックに分割し、前のブロックの最後の 32 バイトを辞書として使用しながら各ブロックを圧縮することになっています。

import java.io.*;
import java.util.zip.*;

public class TestCase
{
    protected static final int BLOCK_SIZE = 128;
    protected static final int DICT_SIZE = 32;

    public static void main(String[] args)
    {
        BufferedInputStream inBytes = new BufferedInputStream(System.in);
        byte[] buff = new byte[BLOCK_SIZE];
        byte[] dict = new byte[DICT_SIZE];
        int bytesRead = 0;

        try
        {
            DGZIPOutputStream compressor = new DGZIPOutputStream(System.out);
            bytesRead = inBytes.read(buff);

            if (bytesRead >= DICT_SIZE)
            {
                System.arraycopy(buff, 0, dict, 0, DICT_SIZE);
            }

            while(bytesRead != -1) 
            {
                compressor.write(buff, 0, bytesRead);              
                if (bytesRead == BLOCK_SIZE)
                {
                    System.arraycopy(buff, BLOCK_SIZE-DICT_SIZE, dict, 0, DICT_SIZE);
                    compressor.setDictionary(dict);
                }

                bytesRead = inBytes.read(buff);
            }
            compressor.flush();         
            compressor.close();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        System.exit(-1);
        }
    }

    public static class DGZIPOutputStream extends GZIPOutputStream
    {
        public DGZIPOutputStream(OutputStream out) throws IOException
        {
            super(out);
        }

        public void setDictionary(byte[] b)
        {
            def.setDictionary(b);
        }

        public void updateCRC(byte[] input)
        {
            crc.update(input);
            System.out.println("Called!");
        }                       
    }
}

私は文字通り1バイトずれています。write() を呼び出すと、後でバイト配列の crc が更新されることがわかっていると思います。何らかの理由でupdateCRCが2回呼び出されていると思いますが、一生どこにあるのかわかりません。または多分私は完全にオフです。しかし、これは1バイトですが、辞書を外すと問題なく動作するので....よくわかりません。

編集:だから、コンパイルしてテストすると:

$cat file.txt

hello world, how are you? 123efd4
KEYBOARDSMASHR#@Q)KF@_{KFSKFDS
000000000000000000000000000000000000000000000000000
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
pwfprejgewojgw
12345678901234567890
!@#$%^&*(!@#$%^&*(A

cat file.txt | java TestCase | gzip -d | cmp file.txt ; echo $?

gzip: stdin: invalid compressed data--crc error
file.txt - differ: byte 1, line 1
1

(ファイルの選択は無視してください。昨夜は眠かったです)

編集:解決

4

0 に答える 0