Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When I define a custom Serializer for key, the 'available' parameter in the deserialize method shows up as -1. #1021

Open
galfordliu opened this issue May 21, 2023 · 0 comments

Comments

@galfordliu
Copy link

When I define a custom KeySerializer, the 'available' parameter in the deserialize method shows up as -1.
What could be the cause of this?

When I serialize keys using kryo or Fst, this exception occurs. My custom Serializer is fine because value also uses the same Serializer

However, in the deserialize method, available will be -1, and the cause of the exception cannot be found. I must only use Serializor.Java to serialize keys.

···
public class KryoUtil {

static final KryoTool INSTANCE = newInstance();

public static KryoTool newInstance(){
    return new KryoTool();
}
public static void setInitListener(Consumer<Kryo> initListener) {
    INSTANCE.setInitListener(initListener);
}


public static Kryo getLocalInstance(){
    return INSTANCE.getLocalInstance();
}

public static <T extends Serializable> byte[] serialize(@Nullable T obj) {
   return INSTANCE.serialize(obj);
}

public static @Nullable  <T extends Serializable> T deserialize(byte[] bytes) {
    return INSTANCE.deserialize(bytes);
}

}

···

···
public class DefaultSerializer implements Serializer {

public DefaultSerializer() {
}

@Override
public void serialize(@NotNull DataOutput2 out, @NotNull E value) throws IOException {
    out.write(KryoUtil.serialize(value));
}

@Override
public E deserialize(@NotNull DataInput2 input, int available) throws IOException {
    if (available == 0) {
        return null;
    }
    byte[] buffer = new byte[available];
    input.readFully(buffer);
    return KryoUtil.deserialize(buffer);
}

}

···

TestKey and MapDbTest implements Serializable. Only one field : String name;

DefaultSerializer<TestKey> keySerializer=new DefaultSerializer<>();
DefaultSerializer<MapDbTest> valueSerializer=new DefaultSerializer<>();
Map<TestKey,MapDbTest> test=db.hashMap(mapName,keySerializer,valueSerializer).createOrOpen()
test.put(TestKey.of("1"),new MapDbTest("1"));
 
Exception in thread "main" java.lang.NegativeArraySizeException, becase the 'available' parameter in the deserialize method shows up as -1. 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant