Skip to content

Commit e59465f

Browse files
committed
Fixed BufferUnderflowException.
1 parent 6021f23 commit e59465f

File tree

10 files changed

+72
-76
lines changed

10 files changed

+72
-76
lines changed

src/de/javasocketapi/core/Client.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ public class Client extends Connection {
1212
private OutputStreamThread outputStreamThread;
1313

1414
public String getHostname() {
15-
return hostname;
15+
return this.hostname;
1616
}
1717

1818
public int getPort() {
19-
return port;
19+
return this.port;
2020
}
2121

2222
public Socket getSocket() {
23-
return socket;
23+
return this.socket;
2424
}
2525

2626
public Client(final String hostname, final int port) {
@@ -36,12 +36,12 @@ public Client(final String hostname, final int port) {
3636
@Override
3737
public void connect() throws IOException {
3838
//check if socket is initialised
39-
if (socket == null) {
39+
if (this.socket == null) {
4040
//initialise socket
4141
this.socket = new Socket(this.hostname, this.port);
4242
this.socket.setTcpNoDelay(true);
4343
this.socket.setKeepAlive(true);
44-
this.socket.setPerformancePreferences(0 ,1 ,2);
44+
this.socket.setPerformancePreferences(0, 1, 2);
4545
}
4646
//start reading and writing
4747
this.inputStreamThread = new InputStreamThread(this);

src/de/javasocketapi/core/Connection.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66

77
abstract class Connection {
88

9-
private volatile AtomicReference<UUID> connectionUUID;
9+
private final AtomicReference<UUID> connectionUUID;
1010

1111
public AtomicReference<UUID> getConnectionUUID() {
12-
return connectionUUID;
12+
return this.connectionUUID;
1313
}
1414

1515
{

src/de/javasocketapi/core/InputStreamThread.java

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010

1111
class InputStreamThread {
1212

13-
private Client client;
14-
private Socket socket;
15-
private Timer timer;
13+
private final Client client;
14+
private final Socket socket;
15+
private final Timer timer;
1616

1717
{
1818
this.timer = new Timer();
@@ -28,55 +28,55 @@ public void run() {
2828
InputStream inputStream = null;
2929
try {
3030
inputStream = this.socket.getInputStream();
31-
} catch (IOException e) {
31+
} catch (final IOException e) {
3232
e.printStackTrace();
3333
}
3434
final byte[][] bytes = new byte[1][1];
3535
//start reading byte arrays
36-
InputStream finalInputStream = inputStream;
36+
final InputStream finalInputStream = inputStream;
3737
this.timer.scheduleAtFixedRate(new TimerTask() {
3838
@Override
3939
public void run() {
4040
try {
41-
if (socket.isClosed()) {
41+
if (InputStreamThread.this.socket.isClosed()) {
4242
//interrupt thread
43-
interrupt();
43+
InputStreamThread.this.interrupt();
4444
return;
4545
}
4646
//check if finalInputStream is null
4747
assert finalInputStream != null;
4848
if (finalInputStream.available() > 0) {
49-
int b = finalInputStream.read();
49+
final int b = finalInputStream.read();
5050
if (b != -1) {
5151
bytes[0] = new byte[b];
5252
//receive bytes
5353
finalInputStream.read(bytes[0], 0, b);
54-
ReadingByteBuffer readingByteBuffer = new ReadingByteBuffer(bytes[0]);
54+
final ReadingByteBuffer readingByteBuffer = new ReadingByteBuffer(bytes[0]);
5555
//read packetId
56-
int packetId = readingByteBuffer.readInt();
56+
final int packetId = readingByteBuffer.readInt();
5757
//check if packet is UpdateUUIDPacket
5858
if (packetId == -2) {
5959
//read connectionUUID
60-
UUID connectionUUID = readingByteBuffer.readUUID();
60+
final UUID connectionUUID = readingByteBuffer.readUUID();
6161
//set updated connectionUUID
62-
client.getConnectionUUID().set(connectionUUID);
62+
InputStreamThread.this.client.getConnectionUUID().set(connectionUUID);
6363
} else {
6464
//get packet
65-
Class<? extends Packet> packet = PacketRegistry.get(packetId);
65+
final Class<? extends Packet> packet = PacketRegistry.get(packetId);
6666
//read connectionUUID
67-
UUID connectionUUID = readingByteBuffer.readUUID();
67+
final UUID connectionUUID = readingByteBuffer.readUUID();
6868
//initialise packet
6969
packet.getConstructor(UUID.class).newInstance(connectionUUID).recieve(readingByteBuffer);
7070
}
7171
} else {
7272
//close socket
73-
socket.close();
73+
InputStreamThread.this.socket.close();
7474
}
7575
}
76-
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
76+
} catch (final InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
7777
e.printStackTrace();
78-
} catch (IOException e) {
79-
interrupt();
78+
} catch (final IOException e) {
79+
InputStreamThread.this.interrupt();
8080
}
8181
}
8282
}, 0, 1);

src/de/javasocketapi/core/OutputStreamThread.java

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111

1212
class OutputStreamThread {
1313

14-
private Client client;
15-
private Socket socket;
16-
private List<Packet> packets;
17-
private Timer timer;
14+
private final Client client;
15+
private final Socket socket;
16+
private final List<Packet> packets;
17+
private final Timer timer;
1818

1919
{
2020
this.packets = new LinkedList<>();
@@ -31,46 +31,46 @@ public void run() {
3131
OutputStream outputStream = null;
3232
try {
3333
outputStream = this.socket.getOutputStream();
34-
} catch (IOException e) {
34+
} catch (final IOException e) {
3535
e.printStackTrace();
3636
}
37-
OutputStream finalOutputStream = outputStream;
37+
final OutputStream finalOutputStream = outputStream;
3838
//start sending send byte arrays
3939
this.timer.scheduleAtFixedRate(new TimerTask() {
4040
@Override
4141
public void run() {
4242
try {
43-
if (socket.isClosed()) {
43+
if (OutputStreamThread.this.socket.isClosed()) {
4444
//interrupt thread
45-
interrupt();
45+
OutputStreamThread.this.interrupt();
4646
return;
4747
}
4848
//skip when no packets available to send
49-
if (!packets.isEmpty()) {
49+
if (!OutputStreamThread.this.packets.isEmpty()) {
5050
//get next packet available to send
51-
Packet packet = packets.get(0);
51+
final Packet packet = OutputStreamThread.this.packets.get(0);
5252
//check if packet is valid
5353
if (packet != null) {
5454
//remove packet
55-
packets.remove(0);
56-
WritingByteBuffer writingByteBuffer = new WritingByteBuffer();
55+
OutputStreamThread.this.packets.remove(0);
56+
final WritingByteBuffer writingByteBuffer = new WritingByteBuffer();
5757
//check if packet is UpdateUUIDPacket
5858
if (packet.getClass().equals(UpdateUUIDPacket.class)) {
5959
writingByteBuffer.writeInt(-2);
6060
writingByteBuffer.writeUUID(packet.getConnectionUUID());
6161
} else {
6262
//get packetId
63-
int packetId = PacketRegistry.indexOf(packet.getClass());
63+
final int packetId = PacketRegistry.indexOf(packet.getClass());
6464
//write packetId
6565
writingByteBuffer.writeInt(packetId);
6666
//write connectionUuid
67-
writingByteBuffer.writeUUID(client.getConnectionUUID().get());
67+
writingByteBuffer.writeUUID(OutputStreamThread.this.client.getConnectionUUID().get());
6868
//initialise packet
6969
packet.send(writingByteBuffer);
7070
}
7171
try {
7272
//receive bytes
73-
byte[] bytes = writingByteBuffer.toBytes();
73+
final byte[] bytes = writingByteBuffer.toBytes();
7474
//check if outputstream is null
7575
assert finalOutputStream != null;
7676
//write bytes length
@@ -79,14 +79,14 @@ public void run() {
7979
finalOutputStream.write(bytes);
8080
//flush outputStream
8181
finalOutputStream.flush();
82-
} catch (SocketException ignored) {
82+
} catch (final SocketException ignored) {
8383

8484
}
8585
}
8686
}
87-
} catch (IOException e) {
87+
} catch (final IOException e) {
8888
e.printStackTrace();
89-
} catch (NullPointerException ignored) {
89+
} catch (final NullPointerException ignored) {
9090

9191
}
9292
}
@@ -97,7 +97,7 @@ public void interrupt() {
9797
this.timer.cancel();
9898
}
9999

100-
public void send(Packet packet) {
100+
public void send(final Packet packet) {
101101
this.packets.add(packet);
102102
}
103103
}

src/de/javasocketapi/core/Packet.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@
44

55
public abstract class Packet {
66

7-
private UUID connectionUUID;
7+
private final UUID connectionUUID;
88

99
public UUID getConnectionUUID() {
10-
return connectionUUID;
10+
return this.connectionUUID;
1111
}
1212

1313
public Packet(final UUID connectionUUID) {
1414
this.connectionUUID = connectionUUID;
1515
}
1616

17-
public abstract void send(final WritingByteBuffer writingByteBuffer);
17+
public abstract void send(WritingByteBuffer writingByteBuffer);
1818

19-
public abstract void recieve(final ReadingByteBuffer readingByteBuffer);
19+
public abstract void recieve(ReadingByteBuffer readingByteBuffer);
2020

2121
}

src/de/javasocketapi/core/PacketRegistry.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
public class PacketRegistry {
77

8-
private static List<Class<? extends Packet>> registerdPackets = new ArrayList<>();
8+
private static final List<Class<? extends Packet>> registerdPackets = new ArrayList<>();
99

1010
static int indexOf(final Class<? extends Packet> packetClass) {
1111
return PacketRegistry.registerdPackets.indexOf(packetClass);

src/de/javasocketapi/core/ReadingByteBuffer.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
package de.javasocketapi.core;
22

3-
import org.boon.primitive.ByteBuf;
4-
53
import java.nio.ByteBuffer;
64
import java.util.UUID;
75

86
public class ReadingByteBuffer {
9-
private ByteBuffer byteBuffer;
7+
private final ByteBuffer byteBuffer;
108

119
ReadingByteBuffer(final byte... bytes) {
1210
this.byteBuffer = ByteBuffer.wrap(bytes);
13-
this.byteBuffer = this.byteBuffer.flip();
1411
}
1512

1613
public boolean readBoolean() {
@@ -55,8 +52,8 @@ public char readChar() {
5552

5653
public String readString() {
5754
//read string
58-
int length = this.readInt();
59-
byte[] bytes = new byte[length];
55+
final int length = this.readInt();
56+
final byte[] bytes = new byte[length];
6057
for (int i = 0; i < length; i++) {
6158
bytes[i] = this.readByte();
6259
}

src/de/javasocketapi/core/Server.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66

77
public class Server extends Connection {
88

9-
private int port;
9+
private final int port;
1010
private ServerSocket serverSocket;
1111
private ServerSocketAcceptingThread serverSocketAcceptingThread;
1212

1313
public int getPort() {
14-
return port;
14+
return this.port;
1515
}
1616

1717
public Server(final int port) {
@@ -22,10 +22,10 @@ public Server(final int port) {
2222
@Override
2323
public void connect() throws IOException {
2424
//check if serverSocket is initialised
25-
if (serverSocket == null) {
25+
if (this.serverSocket == null) {
2626
//initialise serverSocket
2727
this.serverSocket = new ServerSocket(this.port);
28-
this.serverSocket.setPerformancePreferences(0 ,1, 2);
28+
this.serverSocket.setPerformancePreferences(0, 1, 2);
2929
}
3030
//start accepting clients
3131
this.serverSocketAcceptingThread = new ServerSocketAcceptingThread(this.serverSocket);

src/de/javasocketapi/core/ServerSocketAcceptingThread.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010
class ServerSocketAcceptingThread extends Thread {
1111

12-
private ServerSocket serverSocket;
13-
private List<Client> clients;
12+
private final ServerSocket serverSocket;
13+
private final List<Client> clients;
1414

1515
{
1616
this.clients = new LinkedList<>();
@@ -27,25 +27,25 @@ public void run() {
2727
try {
2828
while (true) {
2929
if (this.serverSocket.isClosed()) {
30-
interrupt();
30+
this.interrupt();
3131
break;
3232
}
3333
//initialise new client socket
34-
Socket socket = this.serverSocket.accept();
35-
Client client = new Client(socket);
34+
final Socket socket = this.serverSocket.accept();
35+
final Client client = new Client(socket);
3636
client.connect();
3737
this.clients.add(client);
3838
//update connectionUUID on clioent side
3939
client.send(new UpdateUUIDPacket(client.getConnectionUUID().get()));
4040
}
41-
} catch (IOException e) {
41+
} catch (final IOException e) {
4242
e.printStackTrace();
4343
}
4444
}
4545

4646
public void sendToClient(final Packet packet, final UUID uuid) {
4747
//send to client
48-
for (Client client : this.clients) {
48+
for (final Client client : this.clients) {
4949
if (!client.getConnectionUUID().get().equals(uuid)) {
5050
continue;
5151
}
@@ -55,14 +55,14 @@ public void sendToClient(final Packet packet, final UUID uuid) {
5555

5656
public void sendToAllClients(final Packet packet) {
5757
//send to all clients
58-
for (Client client : this.clients) {
58+
for (final Client client : this.clients) {
5959
client.send(packet);
6060
}
6161
}
6262

6363
public void disconnectClient(final UUID uuid) throws IOException {
6464
//disconnect client
65-
for (Client client : this.clients) {
65+
for (final Client client : this.clients) {
6666
if (!client.getConnectionUUID().get().equals(uuid)) {
6767
continue;
6868
}
@@ -72,7 +72,7 @@ public void disconnectClient(final UUID uuid) throws IOException {
7272

7373
public void disconnectAllClients() throws IOException {
7474
//disconnect all clients
75-
for (Client client : this.clients) {
75+
for (final Client client : this.clients) {
7676
client.disconnect();
7777
}
7878
this.clients.clear();

0 commit comments

Comments
 (0)