From 90a27a97a4484b2b7732ba2080696df3c28383c9 Mon Sep 17 00:00:00 2001 From: Andrew Lalis Date: Tue, 5 Jul 2022 20:34:18 +0200 Subject: [PATCH] Added more expansive support for primitives and more efficient reading/writing. --- pom.xml | 2 +- .../nl/andrewl/record_net/Serializer.java | 20 +++++++++++------ .../util/ExtendedDataInputStream.java | 22 +++++++++++++------ .../util/ExtendedDataOutputStream.java | 12 ++++++++-- 4 files changed, 39 insertions(+), 17 deletions(-) diff --git a/pom.xml b/pom.xml index d16dc78..4239f41 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ nl.andrewl record-net - 1.1.0 + 1.2.0 17 diff --git a/src/main/java/nl/andrewl/record_net/Serializer.java b/src/main/java/nl/andrewl/record_net/Serializer.java index 0a576f8..b3842f3 100644 --- a/src/main/java/nl/andrewl/record_net/Serializer.java +++ b/src/main/java/nl/andrewl/record_net/Serializer.java @@ -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 void writeMessage(T msg, OutputStream o) throws IOException { - DataOutputStream d = new DataOutputStream(o); + writeMessage(msg, new ExtendedDataOutputStream(this, o)); + } + + public 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(); } /** diff --git a/src/main/java/nl/andrewl/record_net/util/ExtendedDataInputStream.java b/src/main/java/nl/andrewl/record_net/util/ExtendedDataInputStream.java index d51fe06..82c6d62 100644 --- a/src/main/java/nl/andrewl/record_net/util/ExtendedDataInputStream.java +++ b/src/main/java/nl/andrewl/record_net/util/ExtendedDataInputStream.java @@ -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>) type); + return readEnum((Class>) 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) type.getComponentType()); - return this.readArray(messageType); + return readArray(messageType); } else if (Message.class.isAssignableFrom(type)) { var messageType = RecordMessageTypeSerializer.get(serializer, (Class) type); return messageType.reader().read(this); diff --git a/src/main/java/nl/andrewl/record_net/util/ExtendedDataOutputStream.java b/src/main/java/nl/andrewl/record_net/util/ExtendedDataOutputStream.java index 955d7f7..f307935 100644 --- a/src/main/java/nl/andrewl/record_net/util/ExtendedDataOutputStream.java +++ b/src/main/java/nl/andrewl/record_net/util/ExtendedDataOutputStream.java @@ -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)) {