Merge pull request #21 from ipbeegle/master

Update PacketProtocol, StreamNetInput, pom to 1.8
This commit is contained in:
Steven Smith 2020-05-25 02:34:25 -07:00 committed by GitHub
commit 5f73b5a7ae
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 41 additions and 32 deletions

View file

@ -20,7 +20,7 @@
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jdk.version>1.7</jdk.version> <jdk.version>1.8</jdk.version>
<argLine></argLine> <argLine></argLine>
</properties> </properties>

View file

@ -3,23 +3,23 @@ package com.github.steveice10.packetlib.io.stream;
import com.github.steveice10.packetlib.io.NetInput; import com.github.steveice10.packetlib.io.NetInput;
import java.io.EOFException; import java.io.EOFException;
import java.io.FilterInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.UUID; import java.util.UUID;
/** /**
* A NetInput implementation using an InputStream as a backend. * A NetInput implementation using an InputStream as a backend.
*/ */
public class StreamNetInput implements NetInput { public class StreamNetInput extends FilterInputStream implements NetInput {
private InputStream in;
/** /**
* Creates a new StreamNetInput instance. * Creates a new StreamNetInput instance.
* *
* @param in InputStream to read from. * @param in InputStream to read from.
*/ */
public StreamNetInput(InputStream in) { public StreamNetInput(InputStream in) {
this.in = in; super(in);
} }
@Override @Override
@ -34,7 +34,7 @@ public class StreamNetInput implements NetInput {
@Override @Override
public int readUnsignedByte() throws IOException { public int readUnsignedByte() throws IOException {
int b = this.in.read(); int b = this.read();
if(b < 0) { if(b < 0) {
throw new EOFException(); throw new EOFException();
} }
@ -125,7 +125,7 @@ public class StreamNetInput implements NetInput {
byte b[] = new byte[length]; byte b[] = new byte[length];
int n = 0; int n = 0;
while(n < length) { while(n < length) {
int count = this.in.read(b, n, length - n); int count = this.read(b, n, length - n);
if(count < 0) { if(count < 0) {
throw new EOFException(); throw new EOFException();
} }
@ -138,12 +138,12 @@ public class StreamNetInput implements NetInput {
@Override @Override
public int readBytes(byte[] b) throws IOException { public int readBytes(byte[] b) throws IOException {
return this.in.read(b); return this.read(b);
} }
@Override @Override
public int readBytes(byte[] b, int offset, int length) throws IOException { 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 @Override
@ -249,16 +249,11 @@ public class StreamNetInput implements NetInput {
public String readString() throws IOException { public String readString() throws IOException {
int length = this.readVarInt(); int length = this.readVarInt();
byte bytes[] = this.readBytes(length); byte bytes[] = this.readBytes(length);
return new String(bytes, "UTF-8"); return new String(bytes, StandardCharsets.UTF_8);
} }
@Override @Override
public UUID readUUID() throws IOException { public UUID readUUID() throws IOException {
return new UUID(this.readLong(), this.readLong()); return new UUID(this.readLong(), this.readLong());
} }
@Override
public int available() throws IOException {
return this.in.available();
}
} }

View file

@ -2,23 +2,23 @@ package com.github.steveice10.packetlib.io.stream;
import com.github.steveice10.packetlib.io.NetOutput; import com.github.steveice10.packetlib.io.NetOutput;
import java.io.FilterOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.UUID; import java.util.UUID;
/** /**
* A NetOutput implementation using an OutputStream as a backend. * A NetOutput implementation using an OutputStream as a backend.
*/ */
public class StreamNetOutput implements NetOutput { public class StreamNetOutput extends FilterOutputStream implements NetOutput {
private OutputStream out;
/** /**
* Creates a new StreamNetOutput instance. * Creates a new StreamNetOutput instance.
* *
* @param out OutputStream to write to. * @param out OutputStream to write to.
*/ */
public StreamNetOutput(OutputStream out) { public StreamNetOutput(OutputStream out) {
this.out = out; super(out);
} }
@Override @Override
@ -28,7 +28,7 @@ public class StreamNetOutput implements NetOutput {
@Override @Override
public void writeByte(int b) throws IOException { public void writeByte(int b) throws IOException {
this.out.write(b); this.write(b);
} }
@Override @Override
@ -100,7 +100,7 @@ public class StreamNetOutput implements NetOutput {
@Override @Override
public void writeBytes(byte b[], int length) throws IOException { public void writeBytes(byte b[], int length) throws IOException {
this.out.write(b, 0, length); this.write(b, 0, length);
} }
@Override @Override
@ -145,7 +145,7 @@ public class StreamNetOutput implements NetOutput {
throw new IllegalArgumentException("String cannot be null!"); throw new IllegalArgumentException("String cannot be null!");
} }
byte[] bytes = s.getBytes("UTF-8"); byte[] bytes = s.getBytes(StandardCharsets.UTF_8);
if(bytes.length > 32767) { if(bytes.length > 32767) {
throw new IOException("String too big (was " + s.length() + " bytes encoded, max " + 32767 + ")"); throw new IOException("String too big (was " + s.length() + " bytes encoded, max " + 32767 + ")");
} else { } else {
@ -159,9 +159,4 @@ public class StreamNetOutput implements NetOutput {
this.writeLong(uuid.getMostSignificantBits()); this.writeLong(uuid.getMostSignificantBits());
this.writeLong(uuid.getLeastSignificantBits()); this.writeLong(uuid.getLeastSignificantBits());
} }
@Override
public void flush() throws IOException {
this.out.flush();
}
} }

View file

@ -14,8 +14,10 @@ import java.util.Map;
* All implementations must have a no-params constructor for server protocol creation. * All implementations must have a no-params constructor for server protocol creation.
*/ */
public abstract class PacketProtocol { public abstract class PacketProtocol {
private final Map<Integer, Class<? extends Packet>> incoming = new HashMap<Integer, Class<? extends Packet>>(); private final Map<Integer, Class<? extends Packet>> incoming = new HashMap<>();
private final Map<Class<? extends Packet>, Integer> outgoing = new HashMap<Class<? extends Packet>, Integer>(); private final Map<Class<? extends Packet>, Integer> outgoing = new HashMap<>();
private final Map<Integer, Class<? extends Packet>> outgoingClasses = new HashMap<>();
/** /**
* Gets the prefix used when locating SRV records for this protocol. * Gets the prefix used when locating SRV records for this protocol.
@ -60,6 +62,7 @@ public abstract class PacketProtocol {
public final void clearPackets() { public final void clearPackets() {
this.incoming.clear(); this.incoming.clear();
this.outgoing.clear(); this.outgoing.clear();
this.outgoingClasses.clear();
} }
/** /**
@ -99,6 +102,7 @@ public abstract class PacketProtocol {
*/ */
public final void registerOutgoing(int id, Class<? extends Packet> packet) { public final void registerOutgoing(int id, Class<? extends Packet> packet) {
this.outgoing.put(packet, id); 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. * @param id Id of the packet to create.
* @return The created packet. * @return The created packet.
* @throws IllegalArgumentException If the packet ID is invalid. * @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. * @throws IllegalStateException If the packet does not have a no-params constructor or cannot be instantiated.
*/ */
public final Packet createIncomingPacket(int id) { public final Packet createIncomingPacket(int id) {
if(id < 0 || !this.incoming.containsKey(id) || this.incoming.get(id) == null) { Class<? extends Packet> packet = this.incoming.get(id);
if (packet == null) {
throw new IllegalArgumentException("Invalid packet id: " + id); throw new IllegalArgumentException("Invalid packet id: " + id);
} }
Class<? extends Packet> packet = this.incoming.get(id);
try { try {
Constructor<? extends Packet> constructor = packet.getDeclaredConstructor(); Constructor<? extends Packet> constructor = packet.getDeclaredConstructor();
if(!constructor.isAccessible()) { if(!constructor.isAccessible()) {
@ -159,4 +163,19 @@ public abstract class PacketProtocol {
return getOutgoingId(packet.getClass()); 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 is not registered.
*/
public final Class<? extends Packet> getOutgoingClass(int id) {
Class<? extends Packet> packet = this.outgoingClasses.get(id);
if(packet == null) {
throw new IllegalArgumentException("Invalid packet id: " + id);
}
return packet;
}
} }