Fixed Network While loops

- Removed while blocking loops and replaced them with return statements
#story[782]
main
Fan-Wu Yang 9 years ago
parent 565b7adaa2
commit b7f010a2fa

@ -148,16 +148,15 @@ public class VisualiserInput implements Runnable
* @throws IOException made by the inputstream reading * @throws IOException made by the inputstream reading
*/ */
private static byte[] getBytes(InputStream inStream) throws IOException { private static byte[] getBytes(InputStream inStream) throws IOException {
while (inStream.available() < 15){ if (inStream.available() < 15) return null;//if there is not enough bytes foer the headerr
//WE NEED THIS!!!!
}
byte[] headerBytes = new byte[15]; byte[] headerBytes = new byte[15];
inStream.read(headerBytes); inStream.read(headerBytes);
byte[] messageLenBytes = Arrays.copyOfRange(headerBytes, 13, 15); byte[] messageLenBytes = Arrays.copyOfRange(headerBytes, 13, 15);
short messageLen = bytesToShort(messageLenBytes); short messageLen = bytesToShort(messageLenBytes);
byte[] messageBytesWithCRC = new byte[messageLen+4]; byte[] messageBytesWithCRC = new byte[messageLen+4];
while (inStream.available() < messageLen + 4){ if (inStream.available() < messageLen + 4){//if the message is not long enough
//WE NEED THIS!!!! inStream.reset();//reset pointer.
return null;
} }
inStream.read(messageBytesWithCRC, 0, messageLen + 4); inStream.read(messageBytesWithCRC, 0, messageLen + 4);
ByteBuffer binaryMessageBytes = ByteBuffer.allocate(headerBytes.length+messageBytesWithCRC.length); ByteBuffer binaryMessageBytes = ByteBuffer.allocate(headerBytes.length+messageBytesWithCRC.length);
@ -176,6 +175,10 @@ public class VisualiserInput implements Runnable
//converts the input into a byte array that can be read by the decoder //converts the input into a byte array that can be read by the decoder
byte[] binaryMessage = getBytes(inFromClient); byte[] binaryMessage = getBytes(inFromClient);
//if there is no bytes read.
if (binaryMessage == null){
continue;
}
//decode the binary message into readable date //decode the binary message into readable date
BinaryMessageDecoder testDecoder = new BinaryMessageDecoder(binaryMessage); BinaryMessageDecoder testDecoder = new BinaryMessageDecoder(binaryMessage);

Loading…
Cancel
Save