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>
|
<groupId>nl.andrewl</groupId>
|
||||||
<artifactId>record-net</artifactId>
|
<artifactId>record-net</artifactId>
|
||||||
<version>1.1.0</version>
|
<version>1.2.0</version>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<maven.compiler.source>17</maven.compiler.source>
|
<maven.compiler.source>17</maven.compiler.source>
|
||||||
|
|
|
@ -88,14 +88,17 @@ public class Serializer {
|
||||||
* constructed for the incoming data.
|
* constructed for the incoming data.
|
||||||
*/
|
*/
|
||||||
public Message readMessage(InputStream i) throws IOException {
|
public Message readMessage(InputStream i) throws IOException {
|
||||||
ExtendedDataInputStream d = new ExtendedDataInputStream(this, i);
|
return readMessage(new ExtendedDataInputStream(this, i));
|
||||||
byte typeId = d.readByte();
|
}
|
||||||
|
|
||||||
|
public Message readMessage(ExtendedDataInputStream in) throws IOException {
|
||||||
|
byte typeId = in.readByte();
|
||||||
var type = messageTypes.get(typeId);
|
var type = messageTypes.get(typeId);
|
||||||
if (type == null) {
|
if (type == null) {
|
||||||
throw new IOException("Unsupported message type: " + typeId);
|
throw new IOException("Unsupported message type: " + typeId);
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
return type.reader().read(d);
|
return type.reader().read(in);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new IOException("Could not instantiate new message object of type " + type.getClass().getSimpleName(), 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.
|
* to write is not supported by this serializer.
|
||||||
*/
|
*/
|
||||||
public <T extends Message> void writeMessage(T msg, OutputStream o) throws IOException {
|
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));
|
Byte typeId = inverseMessageTypes.get(msg.getTypeSerializer(this));
|
||||||
if (typeId == null) {
|
if (typeId == null) {
|
||||||
throw new IOException("Unsupported message type: " + msg.getClass().getSimpleName());
|
throw new IOException("Unsupported message type: " + msg.getClass().getSimpleName());
|
||||||
}
|
}
|
||||||
d.writeByte(typeId);
|
out.writeByte(typeId);
|
||||||
msg.getTypeSerializer(this).writer().write(msg, new ExtendedDataOutputStream(this, d));
|
msg.getTypeSerializer(this).writer().write(msg, out);
|
||||||
d.flush();
|
out.flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -72,21 +72,29 @@ public class ExtendedDataInputStream extends DataInputStream {
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public Object readObject(Class<?> type) throws IOException {
|
public Object readObject(Class<?> type) throws IOException {
|
||||||
if (type.equals(Integer.class) || type.equals(int.class)) {
|
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)) {
|
} 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)) {
|
} else if (type.equals(String.class)) {
|
||||||
return this.readString();
|
return readString();
|
||||||
} else if (type.equals(UUID.class)) {
|
} else if (type.equals(UUID.class)) {
|
||||||
return this.readUUID();
|
return readUUID();
|
||||||
} else if (type.isEnum()) {
|
} else if (type.isEnum()) {
|
||||||
return this.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();
|
int length = this.readInt();
|
||||||
return this.readNBytes(length);
|
return readNBytes(length);
|
||||||
} 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 this.readArray(messageType);
|
return readArray(messageType);
|
||||||
} else if (Message.class.isAssignableFrom(type)) {
|
} else if (Message.class.isAssignableFrom(type)) {
|
||||||
var messageType = RecordMessageTypeSerializer.get(serializer, (Class<? extends Message>) type);
|
var messageType = RecordMessageTypeSerializer.get(serializer, (Class<? extends Message>) type);
|
||||||
return messageType.reader().read(this);
|
return messageType.reader().read(this);
|
||||||
|
|
|
@ -143,9 +143,17 @@ public class ExtendedDataOutputStream extends DataOutputStream {
|
||||||
*/
|
*/
|
||||||
public void writeObject(Object o, Class<?> type) throws IOException {
|
public void writeObject(Object o, Class<?> type) throws IOException {
|
||||||
if (type.equals(Integer.class) || type.equals(int.class)) {
|
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)) {
|
} 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)) {
|
} else if (type.equals(String.class)) {
|
||||||
writeString((String) o);
|
writeString((String) o);
|
||||||
} else if (type.equals(UUID.class)) {
|
} else if (type.equals(UUID.class)) {
|
||||||
|
|
Loading…
Reference in New Issue