Added support for booleans and more arrays.

This commit is contained in:
Andrew Lalis 2022-07-07 09:12:43 +02:00
parent 8e174c0a61
commit d5507073f8
3 changed files with 43 additions and 3 deletions

View File

@ -6,7 +6,7 @@
<groupId>nl.andrewl</groupId> <groupId>nl.andrewl</groupId>
<artifactId>record-net</artifactId> <artifactId>record-net</artifactId>
<version>1.2.1</version> <version>1.3.0</version>
<properties> <properties>
<maven.compiler.source>17</maven.compiler.source> <maven.compiler.source>17</maven.compiler.source>

View File

@ -53,6 +53,35 @@ public class ExtendedDataInputStream extends DataInputStream {
return new UUID(a, b); 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") @SuppressWarnings("unchecked")
public <T extends Message> T[] readArray(MessageTypeSerializer<T> type) throws IOException { public <T extends Message> T[] readArray(MessageTypeSerializer<T> type) throws IOException {
int length = super.readInt(); int length = super.readInt();
@ -83,6 +112,8 @@ public class ExtendedDataInputStream extends DataInputStream {
return readFloat(); return readFloat();
} else if (type.equals(Double.class) || type.equals(double.class)) { } else if (type.equals(Double.class) || type.equals(double.class)) {
return readDouble(); return readDouble();
} else if (type.equals(Boolean.class) || type.equals(boolean.class)) {
return readBoolean();
} else if (type.equals(String.class)) { } else if (type.equals(String.class)) {
return readString(); return readString();
} else if (type.equals(UUID.class)) { } else if (type.equals(UUID.class)) {
@ -90,8 +121,11 @@ public class ExtendedDataInputStream extends DataInputStream {
} else if (type.isEnum()) { } else if (type.isEnum()) {
return readEnum((Class<? extends Enum<?>>) type); return readEnum((Class<? extends Enum<?>>) type);
} else if (type.isAssignableFrom(byte[].class)) { } else if (type.isAssignableFrom(byte[].class)) {
int length = this.readInt(); return readByteArray();
return readNBytes(length); } 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())) { } else if (type.isArray() && Message.class.isAssignableFrom(type.getComponentType())) {
var messageType = RecordMessageTypeSerializer.get(serializer, (Class<? extends Message>) type.getComponentType()); var messageType = RecordMessageTypeSerializer.get(serializer, (Class<? extends Message>) type.getComponentType());
return readArray(messageType); return readArray(messageType);

View File

@ -154,6 +154,8 @@ public class ExtendedDataOutputStream extends DataOutputStream {
writeFloat((float) o); writeFloat((float) o);
} else if (type.equals(Double.class) || type.equals(double.class)) { } else if (type.equals(Double.class) || type.equals(double.class)) {
writeDouble((double) o); writeDouble((double) o);
} else if (type.equals(Boolean.class) || type.equals(boolean.class)) {
writeBoolean((boolean) o);
} else if (type.equals(String.class)) { } else if (type.equals(String.class)) {
writeString((String) o); writeString((String) o);
} else if (type.equals(UUID.class)) { } else if (type.equals(UUID.class)) {
@ -162,6 +164,10 @@ public class ExtendedDataOutputStream extends DataOutputStream {
writeEnum((Enum<?>) o); writeEnum((Enum<?>) o);
} else if (type.equals(byte[].class)) { } else if (type.equals(byte[].class)) {
writeArray((byte[]) o); 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())) { } else if (type.isArray() && Message.class.isAssignableFrom(type.getComponentType())) {
writeArray((Message[]) o); writeArray((Message[]) o);
} else if (Message.class.isAssignableFrom(type)) { } else if (Message.class.isAssignableFrom(type)) {