Issue With Deflater*Stream
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();
}
}
}
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);
}
}
}
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
I expected DeflaterInputStream to decode the deflate.bin bytes to exactly the same as the file.bin bytes.
Thats the issue that I wanted to point out :)
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.
:+1: oh my bad. Updating the readme to use InflaterInputStream while decompressing would help somebody else. Thanks