Skip to content

Commit

Permalink
Better method names
Browse files Browse the repository at this point in the history
  • Loading branch information
albfernandez committed Jan 19, 2024
1 parent 62a0e9c commit c3c0ce9
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/linuxense/javadbf/DBFReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -507,14 +507,14 @@ private Object readDoubleField_B(DBFField field) throws IOException {
byte[] data = new byte[field.getLength()];
// return this.dataInputStream.readDouble();
this.dataInputStream.readFully(data);
return DBFUtils.toDoubleBinary(data);
return DBFUtils.toDoubleLittleEndian(data);
}

private Object readDoubleField_O(DBFField field) throws IOException {
byte[] data = new byte[field.getLength()];
// return this.dataInputStream.readDouble();
this.dataInputStream.readFully(data);
return DBFUtils.toDoubleDouble(data);
return DBFUtils.toDoubleBigEndian(data);
}

private Object readMemoField(DBFField field) throws IOException {
Expand Down
17 changes: 4 additions & 13 deletions src/main/java/com/linuxense/javadbf/DBFUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -415,16 +415,7 @@ protected static BitSet getBitSet(byte[] bytes) {
return BitSet.valueOf(bytes);
}

/*
protected static double toDouble(byte[] data) {
double d = ByteBuffer.wrap(data).order(ByteOrder.BIG_ENDIAN).getDouble();
if (d!= 0.0) {
d = -d;
}
return d;
}*/

protected static double toDoubleBinary(byte[] data) {
protected static double toDoubleLittleEndian(byte[] data) {
double d = ByteBuffer.wrap(data).order(ByteOrder.LITTLE_ENDIAN).getDouble();
if (d == -0.0) {
d = 0.0;
Expand All @@ -433,17 +424,17 @@ protected static double toDoubleBinary(byte[] data) {
}


protected static double toDoubleDouble(byte[] data) {
protected static double toDoubleBigEndian(byte[] data) {
double d = ByteBuffer.wrap(data).order(ByteOrder.BIG_ENDIAN).getDouble();

if (d != 0.0) {
// For some reason sign is swapped
d = -d;
}
}
if (d == -0.0) {
d = 0.0;
}
return d;

}


Expand Down
6 changes: 3 additions & 3 deletions src/test/java/com/linuxense/javadbf/UtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public void testConvertDoubleBinary_valueOne() {
byte[] data = {0,0,0,0,0,0, (byte) 0xF0, 0x3F};
System.out.println(toHexString(data));
System.out.println(toBinaryString(data));
double calculated = DBFUtils.toDoubleBinary(data);
double calculated = DBFUtils.toDoubleLittleEndian(data);
Assert.assertEquals(expected, calculated, 0.01);
}

Expand All @@ -188,7 +188,7 @@ public void testConvertDoubleDouble_valueOne() {
byte[] data = {(byte) 0xBF,(byte) 0xF0,0,0,0,0, 0, 0};
System.out.println(toHexString(data));
System.out.println(toBinaryString(data));
double calculated = DBFUtils.toDoubleDouble(data);
double calculated = DBFUtils.toDoubleBigEndian(data);
Assert.assertEquals(expected, calculated, 0.01);
}

Expand Down Expand Up @@ -227,7 +227,7 @@ public void testDoublesDouble() throws Exception {
list.add(new Pair(new byte[] {-64,42,-90,-117,42,-118,-51,29}, 13.325280503695973));

for (Pair p : list) {
Assert.assertEquals(p.expected, DBFUtils.toDoubleDouble(p.data), 0.01);
Assert.assertEquals(p.expected, DBFUtils.toDoubleBigEndian(p.data), 0.01);
}
}

Expand Down

0 comments on commit c3c0ce9

Please sign in to comment.