From c1f371725a15145fbd3a21d9819d346a104a5732 Mon Sep 17 00:00:00 2001 From: ipbeegle Date: Mon, 25 May 2020 01:48:32 -0400 Subject: [PATCH 1/6] Update PacketProtocol Add reverse mapping for outgoing packet id<->class --- .../packetlib/packet/PacketProtocol.java | 25 ++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/github/steveice10/packetlib/packet/PacketProtocol.java b/src/main/java/com/github/steveice10/packetlib/packet/PacketProtocol.java index 15b6586d..77613f00 100644 --- a/src/main/java/com/github/steveice10/packetlib/packet/PacketProtocol.java +++ b/src/main/java/com/github/steveice10/packetlib/packet/PacketProtocol.java @@ -17,6 +17,8 @@ public abstract class PacketProtocol { private final Map> incoming = new HashMap>(); private final Map, Integer> outgoing = new HashMap, Integer>(); + private final Map> outgoingClasses = new HashMap>(); + /** * Gets the prefix used when locating SRV records for this protocol. * @@ -60,6 +62,7 @@ public abstract class PacketProtocol { public final void clearPackets() { this.incoming.clear(); this.outgoing.clear(); + this.outgoingClasses.clear(); } /** @@ -99,6 +102,7 @@ public abstract class PacketProtocol { */ public final void registerOutgoing(int id, Class packet) { this.outgoing.put(packet, id); + this.outgoingClasses.put(id, packet); } /** @@ -106,15 +110,15 @@ public abstract class PacketProtocol { * * @param id Id of the packet to create. * @return The created packet. - * @throws IllegalArgumentException If the packet ID is invalid. + * @throws IllegalArgumentException If the packet ID less than zero or it is not registered. * @throws IllegalStateException If the packet does not have a no-params constructor or cannot be instantiated. */ public final Packet createIncomingPacket(int id) { - if(id < 0 || !this.incoming.containsKey(id) || this.incoming.get(id) == null) { + Class packet; + if(id < 0 || (packet = this.incoming.get(id)) == null) { throw new IllegalArgumentException("Invalid packet id: " + id); } - Class packet = this.incoming.get(id); try { Constructor constructor = packet.getDeclaredConstructor(); if(!constructor.isAccessible()) { @@ -159,4 +163,19 @@ public abstract class PacketProtocol { return getOutgoingId(packet.getClass()); } + + /** + * Gets the packet class for a packet id. + * @param id The packet id. + * @return The registered packet's class + * @throws IllegalArgumentException If the packet ID less than zero or it is not registered. + */ + public final Class getOutgoingClass(int id) { + Class packet; + if(id < 0 || (packet = this.outgoingClasses.get(id)) == null) { + throw new IllegalArgumentException("Invalid packet id: " + id); + } + + return packet; + } } From e3208ef4e8d9ff4f68ac8b4aea63fed7cc068a39 Mon Sep 17 00:00:00 2001 From: ipbeegle Date: Mon, 25 May 2020 01:55:28 -0400 Subject: [PATCH 2/6] Update pom to target Java 1.8 Fixes Maven warning about dnsjava using 1.8 --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index ba13d261..ab2beefe 100644 --- a/pom.xml +++ b/pom.xml @@ -20,7 +20,7 @@ UTF-8 - 1.7 + 1.8 From 3072587bc7f2efffd8b366d61e9a37f1c75c793d Mon Sep 17 00:00:00 2001 From: ipbeegle Date: Mon, 25 May 2020 02:03:44 -0400 Subject: [PATCH 3/6] Update StreamNetInput Add mark, markSupported, skip --- .../packetlib/io/stream/StreamNetInput.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/main/java/com/github/steveice10/packetlib/io/stream/StreamNetInput.java b/src/main/java/com/github/steveice10/packetlib/io/stream/StreamNetInput.java index 197f913a..957f9e1f 100644 --- a/src/main/java/com/github/steveice10/packetlib/io/stream/StreamNetInput.java +++ b/src/main/java/com/github/steveice10/packetlib/io/stream/StreamNetInput.java @@ -261,4 +261,16 @@ public class StreamNetInput implements NetInput { public int available() throws IOException { return this.in.available(); } + + public void mark(int readLimit) { + this.in.mark(readLimit); + } + + public boolean markSupported() { + return this.in.markSupported(); + } + + public long skip(long n) throws IOException { + return this.in.skip(n); + } } From f2adf41dd21d4ba183f1c0fc001c349fd44daa34 Mon Sep 17 00:00:00 2001 From: ipbeegle Date: Mon, 25 May 2020 02:42:11 -0400 Subject: [PATCH 4/6] Add reset method to StreamNetInput --- .../github/steveice10/packetlib/io/stream/StreamNetInput.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/com/github/steveice10/packetlib/io/stream/StreamNetInput.java b/src/main/java/com/github/steveice10/packetlib/io/stream/StreamNetInput.java index 957f9e1f..97c95e2b 100644 --- a/src/main/java/com/github/steveice10/packetlib/io/stream/StreamNetInput.java +++ b/src/main/java/com/github/steveice10/packetlib/io/stream/StreamNetInput.java @@ -270,6 +270,10 @@ public class StreamNetInput implements NetInput { return this.in.markSupported(); } + public void reset() throws IOException { + this.in.reset(); + } + public long skip(long n) throws IOException { return this.in.skip(n); } From 68542b8e58de1c269d89dd102c4c1f1638a084f8 Mon Sep 17 00:00:00 2001 From: ipbeegle Date: Mon, 25 May 2020 05:13:13 -0400 Subject: [PATCH 5/6] Update PacketProtocol remove negative packet id checks from createIncomingPacket and getOutgoingClass update syntax --- .../packetlib/packet/PacketProtocol.java | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/github/steveice10/packetlib/packet/PacketProtocol.java b/src/main/java/com/github/steveice10/packetlib/packet/PacketProtocol.java index 77613f00..11556482 100644 --- a/src/main/java/com/github/steveice10/packetlib/packet/PacketProtocol.java +++ b/src/main/java/com/github/steveice10/packetlib/packet/PacketProtocol.java @@ -14,10 +14,10 @@ import java.util.Map; * All implementations must have a no-params constructor for server protocol creation. */ public abstract class PacketProtocol { - private final Map> incoming = new HashMap>(); - private final Map, Integer> outgoing = new HashMap, Integer>(); + private final Map> incoming = new HashMap<>(); + private final Map, Integer> outgoing = new HashMap<>(); - private final Map> outgoingClasses = new HashMap>(); + private final Map> outgoingClasses = new HashMap<>(); /** * Gets the prefix used when locating SRV records for this protocol. @@ -110,12 +110,12 @@ public abstract class PacketProtocol { * * @param id Id of the packet to create. * @return The created packet. - * @throws IllegalArgumentException If the packet ID less than zero or it is not registered. + * @throws IllegalArgumentException If the packet ID is not registered. * @throws IllegalStateException If the packet does not have a no-params constructor or cannot be instantiated. */ public final Packet createIncomingPacket(int id) { - Class packet; - if(id < 0 || (packet = this.incoming.get(id)) == null) { + Class packet = this.incoming.get(id); + if (packet == null) { throw new IllegalArgumentException("Invalid packet id: " + id); } @@ -168,11 +168,11 @@ public abstract class PacketProtocol { * Gets the packet class for a packet id. * @param id The packet id. * @return The registered packet's class - * @throws IllegalArgumentException If the packet ID less than zero or it is not registered. + * @throws IllegalArgumentException If the packet ID is not registered. */ public final Class getOutgoingClass(int id) { - Class packet; - if(id < 0 || (packet = this.outgoingClasses.get(id)) == null) { + Class packet = this.outgoingClasses.get(id); + if(packet == null) { throw new IllegalArgumentException("Invalid packet id: " + id); } From ff20c8f2a59269dbcd3dfa1bc31d96a7d5b32f37 Mon Sep 17 00:00:00 2001 From: ipbeegle Date: Mon, 25 May 2020 05:21:10 -0400 Subject: [PATCH 6/6] Change StreamNetInput, StreamNetOutput convert to using Java filter streams update syntax --- .../packetlib/io/stream/StreamNetInput.java | 39 +++++-------------- .../packetlib/io/stream/StreamNetOutput.java | 19 ++++----- 2 files changed, 16 insertions(+), 42 deletions(-) diff --git a/src/main/java/com/github/steveice10/packetlib/io/stream/StreamNetInput.java b/src/main/java/com/github/steveice10/packetlib/io/stream/StreamNetInput.java index 97c95e2b..d96bf9f7 100644 --- a/src/main/java/com/github/steveice10/packetlib/io/stream/StreamNetInput.java +++ b/src/main/java/com/github/steveice10/packetlib/io/stream/StreamNetInput.java @@ -3,23 +3,23 @@ package com.github.steveice10.packetlib.io.stream; import com.github.steveice10.packetlib.io.NetInput; import java.io.EOFException; +import java.io.FilterInputStream; import java.io.IOException; import java.io.InputStream; +import java.nio.charset.StandardCharsets; import java.util.UUID; /** * A NetInput implementation using an InputStream as a backend. */ -public class StreamNetInput implements NetInput { - private InputStream in; - +public class StreamNetInput extends FilterInputStream implements NetInput { /** * Creates a new StreamNetInput instance. * * @param in InputStream to read from. */ public StreamNetInput(InputStream in) { - this.in = in; + super(in); } @Override @@ -34,7 +34,7 @@ public class StreamNetInput implements NetInput { @Override public int readUnsignedByte() throws IOException { - int b = this.in.read(); + int b = this.read(); if(b < 0) { throw new EOFException(); } @@ -125,7 +125,7 @@ public class StreamNetInput implements NetInput { byte b[] = new byte[length]; int n = 0; while(n < length) { - int count = this.in.read(b, n, length - n); + int count = this.read(b, n, length - n); if(count < 0) { throw new EOFException(); } @@ -138,12 +138,12 @@ public class StreamNetInput implements NetInput { @Override public int readBytes(byte[] b) throws IOException { - return this.in.read(b); + return this.read(b); } @Override public int readBytes(byte[] b, int offset, int length) throws IOException { - return this.in.read(b, offset, length); + return this.read(b, offset, length); } @Override @@ -249,32 +249,11 @@ public class StreamNetInput implements NetInput { public String readString() throws IOException { int length = this.readVarInt(); byte bytes[] = this.readBytes(length); - return new String(bytes, "UTF-8"); + return new String(bytes, StandardCharsets.UTF_8); } @Override public UUID readUUID() throws IOException { return new UUID(this.readLong(), this.readLong()); } - - @Override - public int available() throws IOException { - return this.in.available(); - } - - public void mark(int readLimit) { - this.in.mark(readLimit); - } - - public boolean markSupported() { - return this.in.markSupported(); - } - - public void reset() throws IOException { - this.in.reset(); - } - - public long skip(long n) throws IOException { - return this.in.skip(n); - } } diff --git a/src/main/java/com/github/steveice10/packetlib/io/stream/StreamNetOutput.java b/src/main/java/com/github/steveice10/packetlib/io/stream/StreamNetOutput.java index bd6ae79a..78562929 100644 --- a/src/main/java/com/github/steveice10/packetlib/io/stream/StreamNetOutput.java +++ b/src/main/java/com/github/steveice10/packetlib/io/stream/StreamNetOutput.java @@ -2,23 +2,23 @@ package com.github.steveice10.packetlib.io.stream; import com.github.steveice10.packetlib.io.NetOutput; +import java.io.FilterOutputStream; import java.io.IOException; import java.io.OutputStream; +import java.nio.charset.StandardCharsets; import java.util.UUID; /** * A NetOutput implementation using an OutputStream as a backend. */ -public class StreamNetOutput implements NetOutput { - private OutputStream out; - +public class StreamNetOutput extends FilterOutputStream implements NetOutput { /** * Creates a new StreamNetOutput instance. * * @param out OutputStream to write to. */ public StreamNetOutput(OutputStream out) { - this.out = out; + super(out); } @Override @@ -28,7 +28,7 @@ public class StreamNetOutput implements NetOutput { @Override public void writeByte(int b) throws IOException { - this.out.write(b); + this.write(b); } @Override @@ -100,7 +100,7 @@ public class StreamNetOutput implements NetOutput { @Override public void writeBytes(byte b[], int length) throws IOException { - this.out.write(b, 0, length); + this.write(b, 0, length); } @Override @@ -145,7 +145,7 @@ public class StreamNetOutput implements NetOutput { throw new IllegalArgumentException("String cannot be null!"); } - byte[] bytes = s.getBytes("UTF-8"); + byte[] bytes = s.getBytes(StandardCharsets.UTF_8); if(bytes.length > 32767) { throw new IOException("String too big (was " + s.length() + " bytes encoded, max " + 32767 + ")"); } else { @@ -159,9 +159,4 @@ public class StreamNetOutput implements NetOutput { this.writeLong(uuid.getMostSignificantBits()); this.writeLong(uuid.getLeastSignificantBits()); } - - @Override - public void flush() throws IOException { - this.out.flush(); - } }