kryo icon indicating copy to clipboard operation
kryo copied to clipboard

Issue With Deflater*Stream

Open jknair opened this issue 11 years ago • 6 comments

The following code results in IndexOutOfBoundsException. On removing the Deflater classes everything works fine.

kryo version : 3.0.2

public class KryoTest {


    private static final Serializer<Node> nodeSerializer = new Serializer<Node>(){
        @Override
        public void write(Kryo kryo, Output output, Node object) {
            output.writeInt(object.a);
        }

        @Override
        public Node read(Kryo kryo, Input input, Class<Node> type) {
            Node x = new Node();
            x.a = input.readInt();
            return x;
        }
    };

    static class Node {
        int a;

        public Node() {
        }
    }

    public static Kryo getKryoObject(){

        Kryo kryo = new Kryo();
        kryo.register(Node.class, 7);
        return kryo;
    }

    public static void main(String[] args) {
        serialize();
        deserialize();
    }

    private static void deserialize() {
        Kryo kryo = getKryoObject();
        Input input = null;
        try {
            input = new Input(new DeflaterInputStream(new FileInputStream("file.bin")));

            Node y = kryo.readObject(input, Node.class,nodeSerializer );

            input.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    private static void serialize() {
        Kryo kryo = getKryoObject();
        Node x = new Node();
        x.a = 1;
        Output output = null;
        try {
            output = new Output(new DeflaterOutputStream(new FileOutputStream("file.bin")));
            kryo.writeObject(output, x, nodeSerializer);
            output.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }


}

jknair avatar Jul 21 '15 12:07 jknair

Try to get it to work without Kryo first:

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.DeflaterInputStream;
import java.util.zip.DeflaterOutputStream;

public class DeflateTest {
    public static void main (String[] args) throws Exception {
        OutputStream o = new FileOutputStream("file.bin");
        o.write(1);
        o.write(0);
        o.write(0);
        o.write(0);
        o.write(1);
        o.close();

        o = new DeflaterOutputStream(new FileOutputStream("deflate.bin"));
        o.write(1);
        o.write(0);
        o.write(0);
        o.write(0);
        o.write(1);
        o.close();

        InputStream input = new FileInputStream("file.bin");
        InputStream deflateInput = new DeflaterInputStream(new FileInputStream("deflate.bin"));
        while (true) {
            int d = deflateInput.read();
            int i = input.read();
            if (d == -1 && i == -1) break;
            System.out.println(d + " " + i);
        }
    }
}

NathanSweet avatar Jul 21 '15 12:07 NathanSweet

hey sorry but did not get what you want me to try. Running the example you gave results as follows

120 1
156 0
171 0
152 0
147 1
156 -1
146 -1
144 -1
144 -1
192 -1
194 -1
192 -1
192 -1
205 -1
192 -1
12 -1
0 -1
30 -1
218 -1
3 -1
14 -1

jknair avatar Jul 21 '15 14:07 jknair

I expected DeflaterInputStream to decode the deflate.bin bytes to exactly the same as the file.bin bytes.

NathanSweet avatar Jul 21 '15 19:07 NathanSweet

Thats the issue that I wanted to point out :)

jknair avatar Jul 22 '15 10:07 jknair

The code doesn't work when Kryo isn't involved, which means either the JDK or broken or the code is broke. In this case the code is broken. Use InflaterInputStream.

NathanSweet avatar Jul 22 '15 13:07 NathanSweet

:+1: oh my bad. Updating the readme to use InflaterInputStream while decompressing would help somebody else. Thanks

jknair avatar Jul 22 '15 21:07 jknair