diff --git a/pom.xml b/pom.xml index a6e8208..43608d6 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ nl.andrewl record-net - 1.2.1 + 1.3.0 17 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 82c6d62..776ed0d 100644 --- a/src/main/java/nl/andrewl/record_net/util/ExtendedDataInputStream.java +++ b/src/main/java/nl/andrewl/record_net/util/ExtendedDataInputStream.java @@ -53,6 +53,35 @@ public class ExtendedDataInputStream extends DataInputStream { return new UUID(a, b); } + public byte[] readByteArray() throws IOException { + int length = readInt(); + if (length < 0) return null; + byte[] array = new byte[length]; + int readLength = read(array); + if (readLength != length) throw new IOException("Could not read complete byte array."); + return array; + } + + public int[] readIntArray() throws IOException { + int length = readInt(); + if (length < 0) return null; + int[] array = new int[length]; + for (int i = 0; i < length; i++) { + array[i] = readInt(); + } + return array; + } + + public float[] readFloatArray() throws IOException { + int length = readInt(); + if (length < 0) return null; + float[] array = new float[length]; + for (int i = 0; i < length; i++) { + array[i] = readFloat(); + } + return array; + } + @SuppressWarnings("unchecked") public T[] readArray(MessageTypeSerializer type) throws IOException { int length = super.readInt(); @@ -83,6 +112,8 @@ public class ExtendedDataInputStream extends DataInputStream { return readFloat(); } else if (type.equals(Double.class) || type.equals(double.class)) { return readDouble(); + } else if (type.equals(Boolean.class) || type.equals(boolean.class)) { + return readBoolean(); } else if (type.equals(String.class)) { return readString(); } else if (type.equals(UUID.class)) { @@ -90,8 +121,11 @@ public class ExtendedDataInputStream extends DataInputStream { } else if (type.isEnum()) { return readEnum((Class>) type); } else if (type.isAssignableFrom(byte[].class)) { - int length = this.readInt(); - return readNBytes(length); + return readByteArray(); + } else if (type.isAssignableFrom(int[].class)) { + return readIntArray(); + } else if (type.isAssignableFrom(float[].class)) { + return readFloatArray(); } else if (type.isArray() && Message.class.isAssignableFrom(type.getComponentType())) { var messageType = RecordMessageTypeSerializer.get(serializer, (Class) type.getComponentType()); return readArray(messageType); 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 f307935..acd3a00 100644 --- a/src/main/java/nl/andrewl/record_net/util/ExtendedDataOutputStream.java +++ b/src/main/java/nl/andrewl/record_net/util/ExtendedDataOutputStream.java @@ -154,6 +154,8 @@ public class ExtendedDataOutputStream extends DataOutputStream { writeFloat((float) o); } else if (type.equals(Double.class) || type.equals(double.class)) { writeDouble((double) o); + } else if (type.equals(Boolean.class) || type.equals(boolean.class)) { + writeBoolean((boolean) o); } else if (type.equals(String.class)) { writeString((String) o); } else if (type.equals(UUID.class)) { @@ -162,6 +164,10 @@ public class ExtendedDataOutputStream extends DataOutputStream { writeEnum((Enum) o); } else if (type.equals(byte[].class)) { writeArray((byte[]) o); + } else if (type.equals(int[].class)) { + writeArray((int[]) o); + } else if (type.equals(float[].class)) { + writeArray((float[]) o); } else if (type.isArray() && Message.class.isAssignableFrom(type.getComponentType())) { writeArray((Message[]) o); } else if (Message.class.isAssignableFrom(type)) {