From c3c0ce9aef90a47e6afd2ccd0306b720350448f6 Mon Sep 17 00:00:00 2001 From: Alberto Fernandez Date: Fri, 19 Jan 2024 16:33:48 +0100 Subject: [PATCH] Better method names --- .../java/com/linuxense/javadbf/DBFReader.java | 4 ++-- .../java/com/linuxense/javadbf/DBFUtils.java | 17 ++++------------- .../java/com/linuxense/javadbf/UtilsTest.java | 6 +++--- 3 files changed, 9 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/linuxense/javadbf/DBFReader.java b/src/main/java/com/linuxense/javadbf/DBFReader.java index 57feccb..76cb91e 100644 --- a/src/main/java/com/linuxense/javadbf/DBFReader.java +++ b/src/main/java/com/linuxense/javadbf/DBFReader.java @@ -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 { diff --git a/src/main/java/com/linuxense/javadbf/DBFUtils.java b/src/main/java/com/linuxense/javadbf/DBFUtils.java index ff4c50f..a3f5828 100644 --- a/src/main/java/com/linuxense/javadbf/DBFUtils.java +++ b/src/main/java/com/linuxense/javadbf/DBFUtils.java @@ -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; @@ -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; - } diff --git a/src/test/java/com/linuxense/javadbf/UtilsTest.java b/src/test/java/com/linuxense/javadbf/UtilsTest.java index af17ec0..b95b5d7 100644 --- a/src/test/java/com/linuxense/javadbf/UtilsTest.java +++ b/src/test/java/com/linuxense/javadbf/UtilsTest.java @@ -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); } @@ -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); } @@ -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); } }