update byte to int methods in decoders
#story[778]
This commit is contained in:
@@ -6,6 +6,8 @@ import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.util.Arrays;
|
||||
|
||||
import static seng302.Networking.Utils.ByteConverter.*;
|
||||
|
||||
/**
|
||||
* Created by hba56 on 21/04/17.
|
||||
*/
|
||||
@@ -63,55 +65,16 @@ public class BoatLocationDecoder {
|
||||
bytesToInt(sourceID), bytesToInt(seqNum),
|
||||
deviceType, bytesToInt(latitude),
|
||||
bytesToInt(longitude), bytesToInt(altitude),
|
||||
twoByteToInt(heading), bytesToShort(pitch),
|
||||
bytesToShort(roll), twoByteToInt(boatSpeed),
|
||||
twoByteToInt(cog), twoByteToInt(sog),
|
||||
twoByteToInt(apparentWindSpeed), bytesToShort(apparentWindAngle),
|
||||
twoByteToInt(trueWindSpeed), bytesToShort(trueWindDirection),
|
||||
bytesToShort(trueWindAngle), twoByteToInt(currentDrift),
|
||||
twoByteToInt(currentSet), bytesToShort(rudderAngle)
|
||||
bytesToInt(heading), bytesToShort(pitch),
|
||||
bytesToShort(roll), bytesToInt(boatSpeed),
|
||||
bytesToInt(cog), bytesToInt(sog),
|
||||
bytesToInt(apparentWindSpeed), bytesToShort(apparentWindAngle),
|
||||
bytesToInt(trueWindSpeed), bytesToShort(trueWindDirection),
|
||||
bytesToShort(trueWindAngle), bytesToInt(currentDrift),
|
||||
bytesToInt(currentSet), bytesToShort(rudderAngle)
|
||||
);
|
||||
}
|
||||
|
||||
private int twoByteToInt(byte[] bytesInt){
|
||||
ByteBuffer byteBuffer = ByteBuffer.allocate(4);
|
||||
byteBuffer.order(ByteOrder.BIG_ENDIAN);
|
||||
byteBuffer.put((byte)0);
|
||||
byteBuffer.put((byte)0);
|
||||
byteBuffer.put(bytesInt);
|
||||
int num = byteBuffer.getInt(0);
|
||||
|
||||
return num;
|
||||
}
|
||||
|
||||
private int bytesToInt(byte[] bytesInt){
|
||||
ByteBuffer wrapped = ByteBuffer.wrap(bytesInt);
|
||||
int num = wrapped.getInt();
|
||||
return num;
|
||||
}
|
||||
|
||||
private short bytesToShort(byte[] bytesShort){
|
||||
ByteBuffer wrapped = ByteBuffer.wrap(bytesShort);
|
||||
short num = wrapped.getShort();
|
||||
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);
|
||||
// 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 BoatLocationMessage getMessage() {
|
||||
return message;
|
||||
|
||||
@@ -7,6 +7,8 @@ import java.nio.ByteOrder;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
||||
import static seng302.Networking.Utils.ByteConverter.*;
|
||||
|
||||
/**
|
||||
* Created by hba56 on 23/04/17.
|
||||
*/
|
||||
@@ -38,48 +40,15 @@ public class CourseWindDecoder {
|
||||
byte[] flags = Arrays.copyOfRange(messageBytes, 19, 20);
|
||||
|
||||
CourseWind message = new CourseWind(windId[0], bytesToLong(time),
|
||||
bytesToInt(raceID), twoByteToInt(windDirection),
|
||||
twoByteToInt(windSpeed), twoByteToInt(bestUpwindAngle),
|
||||
twoByteToInt(bestDownwindAngle), flags[0]);
|
||||
bytesToInt(raceID), bytesToInt(windDirection),
|
||||
bytesToInt(windSpeed), bytesToInt(bestUpwindAngle),
|
||||
bytesToInt(bestDownwindAngle), flags[0]);
|
||||
|
||||
loopMessages.add(message);
|
||||
messageLoopIndex += 20;
|
||||
}
|
||||
}
|
||||
|
||||
private int twoByteToInt(byte[] bytesInt){
|
||||
ByteBuffer byteBuffer = ByteBuffer.allocate(4);
|
||||
byteBuffer.order(ByteOrder.BIG_ENDIAN);
|
||||
byteBuffer.put((byte)0);
|
||||
byteBuffer.put((byte)0);
|
||||
byteBuffer.put(bytesInt);
|
||||
int num = byteBuffer.getInt(0);
|
||||
|
||||
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 ArrayList<CourseWind> getLoopMessages() {
|
||||
return loopMessages;
|
||||
}
|
||||
|
||||
@@ -7,6 +7,8 @@ import java.nio.ByteOrder;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
||||
import static seng302.Networking.Utils.ByteConverter.*;
|
||||
|
||||
/**
|
||||
* Created by hba56 on 21/04/17.
|
||||
*/
|
||||
@@ -37,44 +39,9 @@ public class RaceStartStatusDecoder {
|
||||
ack = bytesToShort(ackNumber);
|
||||
startTime = bytesToLong(raceStartTime);
|
||||
raceID = bytesToInt(raceIdentifier);
|
||||
notification = byteToChar(notificationType);
|
||||
notification = bytesToChar(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;
|
||||
|
||||
@@ -7,6 +7,8 @@ import java.nio.ByteOrder;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
||||
import static seng302.Networking.Utils.ByteConverter.*;
|
||||
|
||||
/**
|
||||
* Created by hba56 on 21/04/17.
|
||||
*/
|
||||
@@ -47,11 +49,11 @@ public class RaceStatusDecoder {
|
||||
|
||||
time = bytesToLong(timeBytes);
|
||||
race = bytesToInt(raceID);
|
||||
raceState = byteToInt(raceStatus);
|
||||
raceState = bytesToInt(raceStatus);
|
||||
startTime = bytesToLong(expectedStart);
|
||||
raceWindDir = bytesToShort(raceWind);
|
||||
raceWindSpeed = bytesToShort(windSpeed);
|
||||
numberOfBoats = byteToInt(numBoats);
|
||||
numberOfBoats = bytesToInt(numBoats);
|
||||
|
||||
int boatLoopIndex = 0;
|
||||
|
||||
@@ -75,39 +77,6 @@ public class RaceStatusDecoder {
|
||||
}
|
||||
}
|
||||
|
||||
private int byteToInt(byte bytesInt){
|
||||
ByteBuffer byteBuffer = ByteBuffer.allocate(4);
|
||||
byteBuffer.order(ByteOrder.BIG_ENDIAN);
|
||||
byteBuffer.put((byte)0);
|
||||
byteBuffer.put((byte)0);
|
||||
byteBuffer.put((byte)0);
|
||||
byteBuffer.put(bytesInt);
|
||||
int intVal = byteBuffer.getInt(0);
|
||||
return intVal;
|
||||
}
|
||||
|
||||
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);
|
||||
long longVal = byteBuffer.getLong(0);
|
||||
return longVal;
|
||||
}
|
||||
|
||||
public byte getVersionNum() {
|
||||
return versionNum;
|
||||
}
|
||||
|
||||
@@ -7,6 +7,8 @@ import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.util.Arrays;
|
||||
|
||||
import static seng302.Networking.Utils.ByteConverter.bytesToLong;
|
||||
|
||||
/**
|
||||
* Created by hba56 on 20/04/17.
|
||||
*/
|
||||
@@ -52,29 +54,6 @@ public class XMLMessageDecoder {
|
||||
return shortVal;
|
||||
}
|
||||
|
||||
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]);
|
||||
|
||||
// System.out.println("====decode====");
|
||||
// for (byte i:byteBuffer.array()
|
||||
// ) {
|
||||
// System.out.println(i);
|
||||
// }
|
||||
// System.out.println("====decode====");
|
||||
|
||||
long longVal = byteBuffer.getLong(0);
|
||||
return longVal;
|
||||
}
|
||||
|
||||
public byte getMessageVersionNumber() {
|
||||
return messageVersionNumber;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user