package seng302.Networking.MessageDecoders; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.util.Arrays; /** * Created by hba56 on 21/04/17. */ public class RaceStartStatusDecoder { private byte messageVersion; private byte[] timestamp; private byte[] ackNumber; private byte[] raceStartTime; private byte[] raceIdentifier; private byte notificationType; private long time; private short ack; private long startTime; private int raceID; private char notification; public RaceStartStatusDecoder(byte[] encodedRaceStartStatus) { messageVersion = encodedRaceStartStatus[0]; timestamp = Arrays.copyOfRange(encodedRaceStartStatus, 1, 7); ackNumber = Arrays.copyOfRange(encodedRaceStartStatus, 7, 9); raceStartTime = Arrays.copyOfRange(encodedRaceStartStatus, 9, 15); raceIdentifier = Arrays.copyOfRange(encodedRaceStartStatus, 15, 19); notificationType = encodedRaceStartStatus[19]; time = bytesToLong(timestamp); ack = bytesToShort(ackNumber); startTime = bytesToLong(raceStartTime); raceID = bytesToInt(raceIdentifier); notification = byteToChar(notificationType); } private char byteToChar(byte bytesInt) { ByteBuffer byteBuffer = ByteBuffer.allocate(4); byteBuffer.order(ByteOrder.BIG_ENDIAN); byteBuffer.put((byte) 0); byteBuffer.put(bytesInt); char num = byteBuffer.getChar(0); return num; } private short bytesToShort(byte[] bytesShort) { ByteBuffer wrapped = ByteBuffer.wrap(bytesShort); short num = wrapped.getShort(); return num; } private int bytesToInt(byte[] bytesInt) { ByteBuffer wrapped = ByteBuffer.wrap(bytesInt); int num = wrapped.getInt(); return num; } private long bytesToLong(byte[] bytesLong) { ByteBuffer byteBuffer = ByteBuffer.allocate(8); byteBuffer.order(ByteOrder.BIG_ENDIAN); byteBuffer.put((byte) 0); byteBuffer.put((byte) 0); byteBuffer.put(bytesLong[0]); byteBuffer.put(bytesLong[1]); byteBuffer.put(bytesLong[2]); byteBuffer.put(bytesLong[3]); byteBuffer.put(bytesLong[4]); byteBuffer.put(bytesLong[5]); long longVal = byteBuffer.getLong(0); return longVal; } public byte getMessageVersion() { return messageVersion; } public long getTime() { return time; } public short getAck() { return ack; } public long getStartTime() { return startTime; } public int getRaceID() { return raceID; } public char getNotification() { return notification; } }