From 09483fff2c3c6aa667decdc96589876c9e871a7f Mon Sep 17 00:00:00 2001 From: cbt24 Date: Thu, 4 May 2017 11:49:16 +1200 Subject: [PATCH] Added blocking loop to wait - waits for the InputStream to be populated for at least 15 bytes of size before reading in case of header size error potentially crashing the program. #story[782] --- .../main/java/seng302/Model/RaceClock.java | 22 +++++++++++++++++++ .../main/java/seng302/VisualiserInput.java | 3 +++ 2 files changed, 25 insertions(+) diff --git a/visualiser/src/main/java/seng302/Model/RaceClock.java b/visualiser/src/main/java/seng302/Model/RaceClock.java index d797d399..93b85b7c 100644 --- a/visualiser/src/main/java/seng302/Model/RaceClock.java +++ b/visualiser/src/main/java/seng302/Model/RaceClock.java @@ -55,6 +55,24 @@ public class RaceClock { this.lastTime = System.currentTimeMillis(); } + /** + * Sets time to given UTC time in seconds from Unix epoch, preserving timezone. + * @param time UTC time + */ + public void setUTCTime(long time) { + Date utcTime = new Date(time); + setTime(utcTime.toInstant().atZone(this.zoneId)); + } + + /** + * Get ZonedDateTime corresponding to local time zone and given UTC time. + * @param time + */ + public ZonedDateTime getLocalTime(long time) { + Date utcTime = new Date(time); + return utcTime.toInstant().atZone(this.zoneId); + } + /** * Updates time by duration elapsed since last update. */ @@ -68,6 +86,10 @@ public class RaceClock { return zoneId.toString(); } + public ZonedDateTime getTime() { + return time; + } + public StringProperty timeStringProperty() { return timeString; } diff --git a/visualiser/src/main/java/seng302/VisualiserInput.java b/visualiser/src/main/java/seng302/VisualiserInput.java index b1ab21de..012e954a 100644 --- a/visualiser/src/main/java/seng302/VisualiserInput.java +++ b/visualiser/src/main/java/seng302/VisualiserInput.java @@ -148,6 +148,9 @@ public class VisualiserInput implements Runnable * @throws IOException */ private static byte[] getBytes(InputStream inStream) throws IOException { + while (inStream.available() < 15){ + //WE NEED THIS!!!! + } byte[] headerBytes = new byte[15]; inStream.read(headerBytes); byte[] messageLenBytes = Arrays.copyOfRange(headerBytes, 13, 15);