kryo icon indicating copy to clipboard operation
kryo copied to clipboard

in kryo i am using Compress Serialization(Deflated Serializer) ,It is not getting deserialized properly ,

Open gzagarwal opened this issue 11 years ago • 4 comments

In kryo i am using Compress Serialization(Deflated Serializer) ,It is not getting deserialized properly , But if i change the register proces to normal kryo serialization, it works properly for us.Could any help what is the issue with kryo compress deserialization process.

public static void register(Class classes) { kryo.register(Order.class,new DeflateSerializer(kryo.getDefaultSerializer(Order.class))); }

public static byte[] writes(Object obj){ //ByteArrayOutputStream os= new ByteArrayOutputStream(); Output output = new Output(100,Integer.MAX_VALUE); kryo.writeClassAndObject(output, obj); byte[] b= output.toBytes(); output.close(); output.flush(); int i =b.length; System.out.println("compressed object "+i); return b; }

public static Order reads(byte[] bytes){
    //ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
    Input inputs = new Input(bytes);
    Order obj =(Order) kryo.readClassAndObject(inputs);
    inputs.close();
    return obj;
}

public class Order {

@SuppressWarnings("unused")
private int orderID;

/**
 * @return the orderID
 */
public int getOrderID() {
    return orderID;
}

/**
 * @return the quantity
 */
public int getQuantity() {
    return quantity;
}

/**
 * @return the product
 */
public List<String> getProduct() {
    return product;
}

/**
 * @return the products
 */
public Map<String, String> getProducts() {
    return products;
}
@SuppressWarnings("unused")
private int quantity;
private List<String> product;
private Map<String,String> products;


public Order() {
    // TODO Auto-generated constructor stub
}
public Order(int orderID, int quantity, List<String> product, Map<String, String> products) {
    this.orderID = orderID;
    this.quantity = quantity;
    this.product = product;
    this.products = products;
}

@Override
public boolean equals(Object obj) {
    // TODO Auto-generated method stub
    return true;
}

@Override
public int hashCode() {
    // TODO Auto-generated method stub
    return 5;
}

public class TestKryo {

@Test
public void testKryo() throws FileNotFoundException{


    List<String> list = new ArrayList<String>();
    for(int i =0;i<10000;i++){
    list.add("i"+i);
    }
    Map<String,String> map = new HashMap<String,String>();
    for(int i =0;i<10000;i++){
        map.put(new String("i"+i),new String("i"+i));
        }

    Order order = new Order(1, 1,list,map);

KryoSerializer.register(Order.class); byte[] compresed = KryoSerializer.writes(order); Order ordr =(Order)KryoSerializer.reads(compresed); System.out.println(ordr.getOrderID()); }

gzagarwal avatar Apr 07 '15 13:04 gzagarwal

https://github.com/libgdx/libgdx/wiki/Getting-help Read with emphasis on the executable part of executable example code.

http://pastebin.com/8jniuT3u Code works fine.

On Tue, Apr 7, 2015 at 3:44 PM, gzagarwal [email protected] wrote:

In kryo i am using Compress Serialization(Deflated Serializer) ,It is not getting deserialized properly , But if i change the register proces to normal kryo serialization, it works properly for us.Could any help what is the issue with kryo compress deserialization process.

public static void register(Class classes) { kryo.register(Order.class,new DeflateSerializer(kryo.getDefaultSerializer(Order.class))); }

public static byte[] writes(Object obj){ //ByteArrayOutputStream os= new ByteArrayOutputStream(); Output output = new Output(100,Integer.MAX_VALUE); kryo.writeClassAndObject(output, obj); byte[] b= output.toBytes(); output.close(); output.flush(); int i =b.length; System.out.println("compressed object "+i); return b; }

public static Order reads(byte[] bytes){ //ByteArrayInputStream stream = new ByteArrayInputStream(bytes); Input inputs = new Input(bytes); Order obj =(Order) kryo.readClassAndObject(inputs); inputs.close(); return obj; }

public class Order {

@SuppressWarnings("unused") private int orderID;

/**

  • @return the orderID */ public int getOrderID() { return orderID; }

/**

  • @return the quantity */ public int getQuantity() { return quantity; }

/**

  • @return the product */ public List getProduct() { return product; }

/**

  • @return the products */ public Map<String, String> getProducts() { return products; } @SuppressWarnings("unused") private int quantity; private List product; private Map<String,String> products;

public Order() { // TODO Auto-generated constructor stub } public Order(int orderID, int quantity, List product, Map<String, String> products) { this.orderID = orderID; this.quantity = quantity; this.product = product; this.products = products; }

@Override public boolean equals(Object obj) { // TODO Auto-generated method stub return true; }

@Override public int hashCode() { // TODO Auto-generated method stub return 5; }

public class TestKryo {

@Test public void testKryo() throws FileNotFoundException{

List<String> list = new ArrayList<String>();
for(int i =0;i<10000;i++){
list.add("i"+i);
}
Map<String,String> map = new HashMap<String,String>();
for(int i =0;i<10000;i++){
    map.put(new String("i"+i),new String("i"+i));
    }

Order order = new Order(1, 1,list,map);

KryoSerializer.register(Order.class); byte[] compresed = KryoSerializer.writes(order); Order ordr =(Order)KryoSerializer.reads(compresed); System.out.println(ordr.getOrderID()); }

— Reply to this email directly or view it on GitHub https://github.com/EsotericSoftware/kryo/issues/300.

NathanSweet avatar Apr 07 '15 14:04 NathanSweet

Hi Nathan

I tried your code in kryo-2.21.jar , Still getting the null object of Order .and got null Pointer exception .Please elp and let us know is there any change in jar required.

gzagarwal avatar Apr 07 '15 17:04 gzagarwal

Is this still relevant?

magro avatar Mar 28 '16 23:03 magro

Please reopen if still relevant.

magro avatar Apr 08 '16 23:04 magro