From 0d2b022cb606c36065a6e81d2a6057d62e7be036 Mon Sep 17 00:00:00 2001 From: Alberto Fernandez Date: Thu, 10 Jun 2021 22:44:36 +0200 Subject: [PATCH] Fix reading fields with null flags Fix #93 --- .../java/com/linuxense/javadbf/DBFUtils.java | 10 +++-- .../bug93nullfield/Bug99NullFieldTest.java | 36 ++++++++++++++++++ .../resources/bug93_null_field/testfile.FPT | Bin 0 -> 3008 bytes .../resources/bug93_null_field/testfile.dbf | Bin 0 -> 4515 bytes 4 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 src/test/java/com/linuxense/javadbf/bug93nullfield/Bug99NullFieldTest.java create mode 100644 src/test/resources/bug93_null_field/testfile.FPT create mode 100644 src/test/resources/bug93_null_field/testfile.dbf diff --git a/src/main/java/com/linuxense/javadbf/DBFUtils.java b/src/main/java/com/linuxense/javadbf/DBFUtils.java index cc3ed35..83eefc3 100644 --- a/src/main/java/com/linuxense/javadbf/DBFUtils.java +++ b/src/main/java/com/linuxense/javadbf/DBFUtils.java @@ -32,6 +32,8 @@ import java.io.IOException; import java.io.InputStream; import java.math.BigDecimal; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; import java.nio.charset.Charset; import java.nio.charset.CharsetEncoder; import java.text.DecimalFormat; @@ -410,14 +412,16 @@ protected static byte[] readAllBytes(File file) throws IOException { } protected static BitSet getBitSet(byte[] bytes) { -// return BitSet.valueOf(bytes); +// return BitSet.valueOf(bytes); + BitSet bits = new BitSet(); for (int i = 0; i < bytes.length * 8; i++) { - if ((bytes[bytes.length - i / 8 - 1] & (1 << (i % 8))) > 0) { + byte b = bytes[i/8]; + int bit = (b & (1 << (i % 8))); + if (bit > 0) { bits.set(i); } } return bits; } - } diff --git a/src/test/java/com/linuxense/javadbf/bug93nullfield/Bug99NullFieldTest.java b/src/test/java/com/linuxense/javadbf/bug93nullfield/Bug99NullFieldTest.java new file mode 100644 index 0000000..cd48218 --- /dev/null +++ b/src/test/java/com/linuxense/javadbf/bug93nullfield/Bug99NullFieldTest.java @@ -0,0 +1,36 @@ +package com.linuxense.javadbf.bug93nullfield; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; + +import org.junit.Assert; +import org.junit.Test; + +import com.linuxense.javadbf.DBFReader; +import com.linuxense.javadbf.DBFRow; +import com.linuxense.javadbf.DBFUtils; + +public class Bug99NullFieldTest { + + public Bug99NullFieldTest() { + super(); + } + + @Test + public void testBug() throws FileNotFoundException { + File testFile = new File("src/test/resources/bug93_null_field/testfile.dbf"); + + DBFReader reader = null; + try { + reader = new DBFReader(new FileInputStream(testFile)); + reader.setTrimRightSpaces(false); + DBFRow row = reader.nextRow(); + String value = row.getString("REQNAME"); + Assert.assertNotNull(value); + } + finally { + DBFUtils.close(reader); + } + } +} diff --git a/src/test/resources/bug93_null_field/testfile.FPT b/src/test/resources/bug93_null_field/testfile.FPT new file mode 100644 index 0000000000000000000000000000000000000000..b86af71950d55e2cce02f94b3b6f1455019b9819 GIT binary patch literal 3008 zcmeH}(N2Rf6o%^q@D8Mx+}HwRYC__&$Xwj?V$sA)2F=6}A(S-n1-$n)eF{$pBW{3` z2~na168e|)^gsRD148H+A#@JU|MfRBa0IqrW*QGtW@9p(T#m6a)8ZsbZLAYQjJ)|6 z_|Q-20EDY`KW8f&k7UaiJkGnZGLDjjd8xY$nR@3Yfqda-iUVKyJ`*ZjFc6I5Xr5qU zu*#HstQ@oe?ap8Sk@#bSCrDP*HsHml`9 zwQTLkB@E>HH}S8V?f56Bh0%Wbw_5Ul@_YQh7W{wj`~e1Q&mTtgN~g2b_DY#eWAW**?Jl literal 0 HcmV?d00001 diff --git a/src/test/resources/bug93_null_field/testfile.dbf b/src/test/resources/bug93_null_field/testfile.dbf new file mode 100644 index 0000000000000000000000000000000000000000..0aeac0ca51e7affe6428cddc0086d44c3c02d19e GIT binary patch literal 4515 zcmeH~zfaph6vr=4Kmi0o2&Ag&5P>2FKzipN`Qgo+9YDkhI3@xXD+7Wd8$#kAX#au! z6CJw4*s-1XJ1TYPP$^@1&vp{oBoK=w$ea}Wbl>O8-N!HAdx5ptxkbj^gD3kvwjq)1Gy>DaAcMEt`0Mof3Cvm;+5{U2VkFqCh!By z6~ed=Sn%RR{^O1OA7DRL??Z6w8{q!QC+GE7`k41mzr#`xg0 z4x4S$U)r`697rSHH-=aZ$0(46hkeiAZXPjReEm{ne}Dfc3+H&ARk@;T6ZQ z7hsa7mBh2_dJU$92{4r``y?y~IEN;Gqlevj+j$lviv1AEChQLjf+fUsMcLH*E@cPV zr0T$RwTusvg-0;)RAO3Aim8-JOj{FRid5m%RASmmh$*Gsv@!vvh&Q2yYcDzBbn0z0 m{7=_zjjL!d