Added more expansive support for primitives and more efficient reading/writing.
This commit is contained in:
parent
c25232c3ec
commit
90a27a97a4
2
pom.xml
2
pom.xml
|
@ -6,7 +6,7 @@
|
|||
|
||||
<groupId>nl.andrewl</groupId>
|
||||
<artifactId>record-net</artifactId>
|
||||
<version>1.1.0</version>
|
||||
<version>1.2.0</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
|
|
|
@ -88,14 +88,17 @@ public class Serializer {
|
|||
* constructed for the incoming data.
|
||||
*/
|
||||
public Message readMessage(InputStream i) throws IOException {
|
||||
ExtendedDataInputStream d = new ExtendedDataInputStream(this, i);
|
||||
byte typeId = d.readByte();
|
||||
return readMessage(new ExtendedDataInputStream(this, i));
|
||||
}
|
||||
|
||||
public Message readMessage(ExtendedDataInputStream in) throws IOException {
|
||||
byte typeId = in.readByte();
|
||||
var type = messageTypes.get(typeId);
|
||||
if (type == null) {
|
||||
throw new IOException("Unsupported message type: " + typeId);
|
||||
}
|
||||
try {
|
||||
return type.reader().read(d);
|
||||
return type.reader().read(in);
|
||||
} catch (IOException e) {
|
||||
throw new IOException("Could not instantiate new message object of type " + type.getClass().getSimpleName(), e);
|
||||
}
|
||||
|
@ -123,14 +126,17 @@ public class Serializer {
|
|||
* to write is not supported by this serializer.
|
||||
*/
|
||||
public <T extends Message> void writeMessage(T msg, OutputStream o) throws IOException {
|
||||
DataOutputStream d = new DataOutputStream(o);
|
||||
writeMessage(msg, new ExtendedDataOutputStream(this, o));
|
||||
}
|
||||
|
||||
public <T extends Message> void writeMessage(T msg, ExtendedDataOutputStream out) throws IOException {
|
||||
Byte typeId = inverseMessageTypes.get(msg.getTypeSerializer(this));
|
||||
if (typeId == null) {
|
||||
throw new IOException("Unsupported message type: " + msg.getClass().getSimpleName());
|
||||
}
|
||||
d.writeByte(typeId);
|
||||
msg.getTypeSerializer(this).writer().write(msg, new ExtendedDataOutputStream(this, d));
|
||||
d.flush();
|
||||
out.writeByte(typeId);
|
||||
msg.getTypeSerializer(this).writer().write(msg, out);
|
||||
out.flush();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -72,21 +72,29 @@ public class ExtendedDataInputStream extends DataInputStream {
|
|||
@SuppressWarnings("unchecked")
|
||||
public Object readObject(Class<?> type) throws IOException {
|
||||
if (type.equals(Integer.class) || type.equals(int.class)) {
|
||||
return this.readInt();
|
||||
return readInt();
|
||||
} else if (type.equals(Short.class) || type.equals(short.class)) {
|
||||
return readShort();
|
||||
} else if (type.equals(Byte.class) || type.equals(byte.class)) {
|
||||
return (byte) read();
|
||||
} else if (type.equals(Long.class) || type.equals(long.class)) {
|
||||
return this.readLong();
|
||||
return readLong();
|
||||
} else if (type.equals(Float.class) || type.equals(float.class)) {
|
||||
return readFloat();
|
||||
} else if (type.equals(Double.class) || type.equals(double.class)) {
|
||||
return readDouble();
|
||||
} else if (type.equals(String.class)) {
|
||||
return this.readString();
|
||||
return readString();
|
||||
} else if (type.equals(UUID.class)) {
|
||||
return this.readUUID();
|
||||
return readUUID();
|
||||
} else if (type.isEnum()) {
|
||||
return this.readEnum((Class<? extends Enum<?>>) type);
|
||||
return readEnum((Class<? extends Enum<?>>) type);
|
||||
} else if (type.isAssignableFrom(byte[].class)) {
|
||||
int length = this.readInt();
|
||||
return this.readNBytes(length);
|
||||
return readNBytes(length);
|
||||
} else if (type.isArray() && Message.class.isAssignableFrom(type.getComponentType())) {
|
||||
var messageType = RecordMessageTypeSerializer.get(serializer, (Class<? extends Message>) type.getComponentType());
|
||||
return this.readArray(messageType);
|
||||
return readArray(messageType);
|
||||
} else if (Message.class.isAssignableFrom(type)) {
|
||||
var messageType = RecordMessageTypeSerializer.get(serializer, (Class<? extends Message>) type);
|
||||
return messageType.reader().read(this);
|
||||
|
|
|
@ -143,9 +143,17 @@ public class ExtendedDataOutputStream extends DataOutputStream {
|
|||
*/
|
||||
public void writeObject(Object o, Class<?> type) throws IOException {
|
||||
if (type.equals(Integer.class) || type.equals(int.class)) {
|
||||
writeInt((Integer) o);
|
||||
writeInt((int) o);
|
||||
} else if (type.equals(Short.class) || type.equals(short.class)) {
|
||||
writeShort((short) o);
|
||||
} else if (type.equals(Byte.class) || type.equals(byte.class)) {
|
||||
writeByte((byte) o);
|
||||
} else if (type.equals(Long.class) || type.equals(long.class)) {
|
||||
writeLong((Long) o);
|
||||
writeLong((long) o);
|
||||
} else if (type.equals(Float.class) || type.equals(float.class)) {
|
||||
writeFloat((float) o);
|
||||
} else if (type.equals(Double.class) || type.equals(double.class)) {
|
||||
writeDouble((double) o);
|
||||
} else if (type.equals(String.class)) {
|
||||
writeString((String) o);
|
||||
} else if (type.equals(UUID.class)) {
|
||||
|
|
Loading…
Reference in New Issue