Skip to content

com.esotericsoftware.kryo.KryoException: Encountered unregistered class ID: <negative_number> #340

Closed
@max-k7v

Description

@max-k7v

During read objects with big class ID, I've got exceptions about negative value in ID
Kryo version 3.0.3

package com.vaultmr.app.patientchart.repository.serializer;

import java.io.ByteArrayOutputStream;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.Arrays;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.UnsafeInput;
import com.esotericsoftware.kryo.io.UnsafeOutput;

@RunWith(Parameterized.class)
public class KryoTest {

    public static class Base implements Serializable {
        private final int field;

        public Base(int field) {
            this.field = field;
        }

        @Override
        public boolean equals(Object obj) {
            if (obj == null || !(this.getClass().equals(obj.getClass()))) {
                return false;
            }
            return this.field == ((Base) obj).field;
        }
    }

    public static class T1 extends Base {
        public static final int serialVersionUID = 1000;

        public T1() {
            super(10);
        }
    };

    public static class T2 extends Base {
        public static final int serialVersionUID = 1000 * 1000;

        public T2() {
            super(20);
        }
    };

    public static class T3 extends Base {
        public static final int serialVersionUID = 1000 * 1000 * 1000;

        public T3() {
            super(30);
        }
    };

    public static class T4 extends Base {
        public static final int serialVersionUID = Integer.MAX_VALUE - 1;

        public T4() {
            super(40);
        }
    };

    public static class T5 extends Base {
        public static final int serialVersionUID = Integer.MAX_VALUE;

        public T5() {
            super(50);
        }
    };

    @Parameters(name= "{index}: type[{0}]")
    public static Iterable<Object[]> data() {
        return Arrays.asList(new Object[][] {
                {T1.class},
                {T2.class},
                {T3.class},
                {T4.class},
                {T5.class}
        });
    }

    private final Class<?> type;

    public KryoTest(Class<?> type) {
        this.type = type;
    }

    @Test
    public void test() throws Exception {
        Kryo kryo = new Kryo();

        Field classUIDField = type.getField("serialVersionUID");
        int typeId = classUIDField.getInt(null);
        kryo.register(type, typeId);

        ByteArrayOutputStream buff = new ByteArrayOutputStream();
        Object origObj = type.newInstance();

        UnsafeOutput out = new UnsafeOutput(buff);
        kryo.writeClassAndObject(out, origObj);
        out.flush();

        Object copyObj = kryo.readClassAndObject(new UnsafeInput(buff.toByteArray()));

        Assert.assertEquals(origObj, copyObj);
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions