Commit 988d9fc8 authored by legoc's avatar legoc

Corrected PortableByteArray so that all 64bits values are properly parsed....

Corrected PortableByteArray so that all 64bits values are properly parsed. Added a test program and some test files.
parent a089931f
......@@ -6,6 +6,7 @@
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="src/test/resources"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7">
<attributes>
<attribute name="maven.pomderived" value="true"/>
......
......@@ -18,7 +18,6 @@
package fr.ill.ics.nscclient.dataprovider;
public class PortableByteArray {
private enum Endianess {
......@@ -49,11 +48,11 @@ public class PortableByteArray {
short result;
if (endianness == Endianess.BIG) {
result = (short) ((encodedData[pos+1] & 0xff) |
(encodedData[pos+0] & 0xff) << 8);
result = (short)((encodedData[pos+1] & 0xff) << 0 |
(encodedData[pos+0] & 0xff) << 8);
} else {
result = (short) ((encodedData[pos+0] & 0xff) |
(encodedData[pos+1] & 0xff) << 8);
result = (short)((encodedData[pos+0] & 0xff) << 0 |
(encodedData[pos+1] & 0xff) << 8);
}
pos = pos + 2;
......@@ -64,15 +63,15 @@ public class PortableByteArray {
int result;
if (endianness == Endianess.BIG) {
result = (int) ((encodedData[pos+3] & 0xff) |
(encodedData[pos+2] & 0xff) << 8 |
(encodedData[pos+1] & 0xff) << 16 |
(encodedData[pos+0] & 0xff) << 24);
result = (int)(encodedData[pos+3] & 0xff) << 0 |
(int)(encodedData[pos+2] & 0xff) << 8 |
(int)(encodedData[pos+1] & 0xff) << 16 |
(int)(encodedData[pos+0] & 0xff) << 24;
} else {
result = (int) ((encodedData[pos+0] & 0xff) |
(encodedData[pos+1] & 0xff) << 8 |
(encodedData[pos+2] & 0xff) << 16 |
(encodedData[pos+3] & 0xff) << 24);
result = (int)(encodedData[pos+0] & 0xff) << 0 |
(int)(encodedData[pos+1] & 0xff) << 8 |
(int)(encodedData[pos+2] & 0xff) << 16 |
(int)(encodedData[pos+3] & 0xff) << 24;
}
pos = pos + 4;
......@@ -83,25 +82,27 @@ public class PortableByteArray {
long result;
if (endianness == Endianess.BIG) {
result = (long) ((encodedData[pos+7] & 0xff) |
(encodedData[pos+6] & 0xff) << 8 |
(encodedData[pos+5] & 0xff) << 16 |
(encodedData[pos+4] & 0xff) << 24 |
(long)((encodedData[pos+3] & 0xff) << 0 |
(encodedData[pos+2] & 0xff) << 8 |
(encodedData[pos+1] & 0xff) << 16 |
(encodedData[pos+0] & 0xff) << 24) << 32);
result = (long)(encodedData[pos+7] & 0xff) << 0 |
(long)(encodedData[pos+6] & 0xff) << 8 |
(long)(encodedData[pos+5] & 0xff) << 16 |
(long)(encodedData[pos+4] & 0xff) << 24 |
(long)(encodedData[pos+3] & 0xff) << 32 |
(long)(encodedData[pos+2] & 0xff) << 40 |
(long)(encodedData[pos+1] & 0xff) << 48 |
(long)(encodedData[pos+0] & 0xff) << 56;
} else {
result = (long) ((encodedData[pos] & 0xff) |
(encodedData[pos+1] & 0xff) << 8 |
(encodedData[pos+2] & 0xff) << 16 |
(encodedData[pos+3] & 0xff) << 24 |
(long)((encodedData[pos+4] & 0xff) << 0 |
(encodedData[pos+5] & 0xff) << 8 |
(encodedData[pos+6] & 0xff) << 16 |
(encodedData[pos+7] & 0xff) << 24) << 32);
result = (long)(encodedData[pos+0] & 0xff) << 0 |
(long)(encodedData[pos+1] & 0xff) << 8 |
(long)(encodedData[pos+2] & 0xff) << 16 |
(long)(encodedData[pos+3] & 0xff) << 24 |
(long)(encodedData[pos+4] & 0xff) << 32 |
(long)(encodedData[pos+5] & 0xff) << 40 |
(long)(encodedData[pos+6] & 0xff) << 48 |
(long)(encodedData[pos+7] & 0xff) << 56;
}
pos = pos + 8;
//System.out.println("result " + Long.toHexString(result));
return result;
}
......@@ -138,10 +139,38 @@ public class PortableByteArray {
size = readInt();
if (size*bytesLength != encodedData.length-8) {
if (size * bytesLength != encodedData.length - 8) {
throw new SizeException();
}
}
// The following implementation can be used. It is simple because it is calling ByteBuffer class but it runs slower than ours.
//
// public double[] parseToDouble(byte[] inputArray) throws TypeException, SizeException, NullException {
//
// ByteBuffer buffer = ByteBuffer.wrap(inputArray);
//
// short s = buffer.getShort();
//
// if (s == 1) {
// buffer.order(ByteOrder.BIG_ENDIAN);
// }
// else {
// buffer.order(ByteOrder.LITTLE_ENDIAN);
// }
//
// // Type of data.
// s = buffer.getShort();
//
// int arraySize = buffer.getInt();
// double[] result = new double[arraySize];
//
// for (int i = 0; i < arraySize; ++i) {
// result[i] = buffer.getDouble();
// }
//
// return result;
// }
public double[] parseToDouble(byte[] inputArray) throws TypeException, SizeException, NullException {
// Parse byte array to retrieve double array
......@@ -156,6 +185,8 @@ public class PortableByteArray {
}
return result;
}
public int[] parseToInt(byte[] inputArray) throws TypeException, SizeException, NullException {
// Parse byte array to retrieve int array
......
package fr.ill.ics.nscclient.dataprovider;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class PortableByteArrayTest {
public static void main(String[] args) {
try {
ClassLoader classLoader = PortableByteArrayTest.class.getClassLoader();
{
String testFile = "test-float64-little.bin";
System.out.println("Reading " + testFile);
Path fileLocation = Paths.get(classLoader.getResource(testFile).getPath());
byte[] data = Files.readAllBytes(fileLocation);
PortableByteArray portableByteArray = new PortableByteArray();
double[] result = portableByteArray.parseToDouble(data);
for (double r : result) {
System.out.println(r);
}
testFile = "test-float64-big.bin";
System.out.println("");
System.out.println("Reading " + testFile);
fileLocation = Paths.get(classLoader.getResource(testFile).getPath());
data = Files.readAllBytes(fileLocation);
portableByteArray = new PortableByteArray();
result = portableByteArray.parseToDouble(data);
for (double r : result) {
System.out.println(r);
}
}
{
String testFile = "test-int32-little.bin";
System.out.println("");
System.out.println("Reading " + testFile);
Path fileLocation = Paths.get(classLoader.getResource(testFile).getPath());
byte[] data = Files.readAllBytes(fileLocation);
PortableByteArray portableByteArray = new PortableByteArray();
int[] result = portableByteArray.parseToInt(data);
for (int r : result) {
System.out.println(r);
}
testFile = "test-int32-big.bin";
System.out.println("");
System.out.println("Reading " + testFile);
fileLocation = Paths.get(classLoader.getResource(testFile).getPath());
data = Files.readAllBytes(fileLocation);
portableByteArray = new PortableByteArray();
result = portableByteArray.parseToInt(data);
for (int r : result) {
System.out.println(r);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment