Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added support for Integer data type fields. #14

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ date (D) | `java.util.Date`
float (F) | `java.lang.Float`
logical (L) | `java.lang.Boolean`
numeric (N) | `java.lang.Number`
integer (I) | `java.lang.Integer`

### How to use

Expand Down
27 changes: 23 additions & 4 deletions dbf-reader/src/main/java/org/jamel/dbf/DbfReader.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
package org.jamel.dbf;

import java.io.BufferedInputStream;
import java.io.Closeable;
import java.io.DataInput;
import java.io.DataInputStream;
import java.io.EOFException;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.util.Date;
import java.util.GregorianCalendar;

import org.jamel.dbf.exception.DbfException;
import org.jamel.dbf.structure.DbfField;
import org.jamel.dbf.structure.DbfHeader;
import org.jamel.dbf.utils.DbfUtils;

import java.io.*;
import java.util.Date;
import java.util.GregorianCalendar;

/**
* Dbf reader.
* This class is not thread safe.
Expand Down Expand Up @@ -121,6 +129,7 @@ private Object readFieldValue(DbfField field) throws IOException {
case FLOAT: return readFloatValue(field, buf);
case LOGICAL: return readLogicalValue(field, buf);
case NUMERIC: return readNumericValue(field, buf);
case INTEGER: return readIntegerValue(field, buf);
default: return null;
}
}
Expand Down Expand Up @@ -161,6 +170,16 @@ protected Number readNumericValue(DbfField field, byte[] buf) throws IOException
}
}

protected Integer readIntegerValue(DbfField field, byte[] buf) throws IOException {
try {
if (buf.length != 4) throw new DbfException("Failed to parse Integer from " + field.getName());
int x = java.nio.ByteBuffer.wrap(buf).order(java.nio.ByteOrder.LITTLE_ENDIAN).getInt();
return x;
} catch (NumberFormatException e) {
throw new DbfException("Failed to parse Integer from " + field.getName(), e);
}
}

/**
* @return the number of records in the Dbf.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ public enum DbfDataType {
DATE('D'),
FLOAT('F'),
LOGICAL('L'),
NUMERIC('N');
NUMERIC('N'),
INTEGER('I');

public final byte byteValue;

Expand Down