From 982b47943af0cdc8c19fb7efe25a30f9baf0e867 Mon Sep 17 00:00:00 2001 From: Fan-Wu Yang Date: Sat, 6 May 2017 15:27:21 +1200 Subject: [PATCH] Fixed blocking issue - Fixed byte stream error where it would reinitilise every run #story[782] --- .../src/main/java/seng302/VisualiserInput.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/visualiser/src/main/java/seng302/VisualiserInput.java b/visualiser/src/main/java/seng302/VisualiserInput.java index 60292bf2..8f2551ed 100644 --- a/visualiser/src/main/java/seng302/VisualiserInput.java +++ b/visualiser/src/main/java/seng302/VisualiserInput.java @@ -9,6 +9,7 @@ import seng302.Networking.Utils.*; import javax.xml.parsers.ParserConfigurationException; import java.io.*; import java.net.*; +import java.nio.Buffer; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.text.ParseException; @@ -50,11 +51,13 @@ public class VisualiserInput implements Runnable private RaceStartStatus raceStartStatus; + private BufferedInputStream inStream; + public VisualiserInput(Socket socket, StreamedCourse course) throws IOException{ this.connectionSocket = socket; // this.connectionSocket = new Socket("livedata.americascup.com",4941); - + this.inStream = new BufferedInputStream(connectionSocket.getInputStream()); this.course = course; this.boatLocation = new HashMap<>(); this.boatStatus = new HashMap<>(); @@ -143,11 +146,11 @@ public class VisualiserInput implements Runnable /** * Takes an inputStream and reads the first 15 bytes (the header) and gets the message length * for the whole message then reads that and returns the byte array - * @param inStream inputStream from socket * @return encoded binary messsage bytes * @throws IOException made by the inputstream reading */ - private static byte[] getBytes(InputStream inStream) throws IOException { + private byte[] getBytes() throws IOException { + inStream.mark(0); if (inStream.available() < 15) return null;//if there is not enough bytes foer the headerr byte[] headerBytes = new byte[15]; inStream.read(headerBytes); @@ -170,11 +173,8 @@ public class VisualiserInput implements Runnable //receiver loop that gets the input boolean receiverLoop = true; while(receiverLoop) { - //gets the input from the socket - InputStream inFromClient = connectionSocket.getInputStream(); - //converts the input into a byte array that can be read by the decoder - byte[] binaryMessage = getBytes(inFromClient); + byte[] binaryMessage = getBytes(); //if there is no bytes read. if (binaryMessage == null){ continue;