Skip to content

Commit 8935876

Browse files
committed
updated InputStreamThread.java
1 parent 1cd3a29 commit 8935876

File tree

1 file changed

+25
-23
lines changed

1 file changed

+25
-23
lines changed

src/de/javasocketapi/core/InputStreamThread.java

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -47,31 +47,33 @@ public void run() {
4747
}
4848
//check if finalInputStream is null
4949
assert finalInputStream != null;
50-
int b = finalInputStream.read();
51-
if (b != -1) {
52-
bytes[0] = new byte[b];
53-
//receive bytes
54-
finalInputStream.read(bytes[0], 0, b);
55-
ReadingByteBuffer readingByteBuffer = new ReadingByteBuffer(bytes[0]);
56-
//read packetId
57-
int packetId = readingByteBuffer.readInt();
58-
//check if packet is UpdateUUIDPacket
59-
if (packetId == -2) {
60-
//read connectionUUID
61-
UUID connectionUUID = readingByteBuffer.readUUID();
62-
//set updated connectionUUID
63-
client.getConnectionUUID().set(connectionUUID);
50+
if (finalInputStream.available() > 0) {
51+
int b = finalInputStream.read();
52+
if (b != -1) {
53+
bytes[0] = new byte[b];
54+
//receive bytes
55+
finalInputStream.read(bytes[0], 0, b);
56+
ReadingByteBuffer readingByteBuffer = new ReadingByteBuffer(bytes[0]);
57+
//read packetId
58+
int packetId = readingByteBuffer.readInt();
59+
//check if packet is UpdateUUIDPacket
60+
if (packetId == -2) {
61+
//read connectionUUID
62+
UUID connectionUUID = readingByteBuffer.readUUID();
63+
//set updated connectionUUID
64+
client.getConnectionUUID().set(connectionUUID);
65+
} else {
66+
//get packet
67+
Class<? extends Packet> packet = PacketRegistry.get(packetId);
68+
//read connectionUUID
69+
UUID connectionUUID = readingByteBuffer.readUUID();
70+
//initialise packet
71+
packet.getConstructor(UUID.class).newInstance(connectionUUID).recieve(readingByteBuffer);
72+
}
6473
} else {
65-
//get packet
66-
Class<? extends Packet> packet = PacketRegistry.get(packetId);
67-
//read connectionUUID
68-
UUID connectionUUID = readingByteBuffer.readUUID();
69-
//initialise packet
70-
packet.getConstructor(UUID.class).newInstance(connectionUUID).recieve(readingByteBuffer);
74+
//close socket
75+
socket.close();
7176
}
72-
} else {
73-
//close socket
74-
socket.close();
7577
}
7678
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
7779
e.printStackTrace();

0 commit comments

Comments
 (0)