Fix bug with primitive wrappers.

This commit is contained in:
Andrew Lalis 2023-09-22 09:19:08 -04:00
parent ebc7818e00
commit c32524387a
2 changed files with 9 additions and 2 deletions

View File

@ -3,6 +3,7 @@ package com.andrewlalis.record_net;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Set;
import java.util.UUID;
/**
@ -12,6 +13,12 @@ import java.util.UUID;
public final class IOUtil {
private IOUtil() {}
public static boolean isPrimitiveOrWrapper(Class<?> type) {
final Set<Class<?>> types = Set.of(Byte.class, Short.class, Integer.class, Character.class, Float.class, Double.class, Long.class, Boolean.class);
return type.isPrimitive() || types.contains(type);
}
public static Object readPrimitive(Class<?> type, DataInputStream dIn) throws IOException {
if (type.equals(Integer.class) || type.equals(int.class)) return dIn.readInt();
if (type.equals(Short.class) || type.equals(short.class)) return dIn.readShort();

View File

@ -89,7 +89,7 @@ public class RecordMappedSerializer implements RecordSerializer {
if (type.equals(String.class)) {
return IOUtil.readString(dIn);
}
if (type.isPrimitive()) {
if (IOUtil.isPrimitiveOrWrapper(type)) {
return IOUtil.readPrimitive(type, dIn);
}
throw new UnsupportedMessageTypeException(type);
@ -132,7 +132,7 @@ public class RecordMappedSerializer implements RecordSerializer {
IOUtil.writeUUID((UUID) obj, dOut);
} else if (type.equals(String.class)) {
IOUtil.writeString((String) obj, dOut);
} else if (type.isPrimitive()) {
} else if (IOUtil.isPrimitiveOrWrapper(type)) {
IOUtil.writePrimitive(obj, dOut);
} else {
throw new UnsupportedMessageTypeException(type);