1
0

Fixed NullPointerException

This commit is contained in:
Marcin Kostrzewski 2020-10-29 12:30:55 +01:00
parent 2e4bd8289e
commit 4f5800cba9

View File

@ -106,10 +106,10 @@ public class ProtocolMessage {
public void readFromStream(Reader reader) throws IOException { public void readFromStream(Reader reader) throws IOException {
int totalBytesRead = 0; int totalBytesRead = 0;
char[] buffer = new char[2048];
// Read first 4 bytes containing the length of the incoming message // Read first 4 bytes containing the length of the incoming message
while(totalBytesRead != 4){ while(totalBytesRead != 4){
int bytesRead = reader.read(this.raw, totalBytesRead, 4 - totalBytesRead); int bytesRead = reader.read(buffer, totalBytesRead, 4 - totalBytesRead);
if (bytesRead == -1) { if (bytesRead == -1) {
logger.log(Level.ERROR, "Invalid packet."); logger.log(Level.ERROR, "Invalid packet.");
return; return;
@ -117,16 +117,17 @@ public class ProtocolMessage {
totalBytesRead += bytesRead; totalBytesRead += bytesRead;
} }
int messageLength = charArrayToInt(this.raw); int messageLength = charArrayToInt(buffer);
while(totalBytesRead != 4 + messageLength){ while(totalBytesRead != 4 + messageLength){
int bytesRead = reader.read(this.raw, totalBytesRead, messageLength - totalBytesRead); int bytesRead = reader.read(buffer, totalBytesRead, messageLength - totalBytesRead + 4);
if (bytesRead == -1) { if (bytesRead == -1) {
logger.log(Level.ERROR, "Invalid packet."); logger.log(Level.ERROR, "Invalid packet.");
return; return;
} else } else
totalBytesRead += bytesRead; totalBytesRead += bytesRead;
} }
this.raw = new char[totalBytesRead];
System.arraycopy(buffer, 0, this.raw, 0, totalBytesRead);
deconstructPacket(this.raw); deconstructPacket(this.raw);
} }