Skip to content

Commit

Permalink
Fix reading fields with null flags
Browse files Browse the repository at this point in the history
Fix #93
  • Loading branch information
albfernandez committed Jun 10, 2021
1 parent d196a39 commit 0d2b022
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/main/java/com/linuxense/javadbf/DBFUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Binary file added src/test/resources/bug93_null_field/testfile.FPT
Binary file not shown.
Binary file added src/test/resources/bug93_null_field/testfile.dbf
Binary file not shown.

0 comments on commit 0d2b022

Please sign in to comment.