Skip to content

Commit

Permalink
Read fully
Browse files Browse the repository at this point in the history
  • Loading branch information
albfernandez committed Jun 7, 2021
1 parent f2ac745 commit c378f58
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 34 deletions.
38 changes: 8 additions & 30 deletions src/main/java/com/linuxense/javadbf/DBFReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -409,14 +409,10 @@ public DBFRow nextRow() {
}

protected Object getFieldValue(DBFField field) throws IOException {
int bytesReaded = 0;
switch (field.getType()) {
case CHARACTER:
byte[] b_array = new byte[field.getLength()];
bytesReaded = this.dataInputStream.read(b_array);
if (bytesReaded < field.getLength()) {
throw new EOFException("Unexpected end of file");
}
this.dataInputStream.readFully(b_array);
if (this.trimRightSpaces) {
return new String(DBFUtils.trimRightSpaces(b_array), getCharset());
}
Expand All @@ -427,30 +423,18 @@ protected Object getFieldValue(DBFField field) throws IOException {
case VARCHAR:
case VARBINARY:
byte[] b_array_var = new byte[field.getLength()];
bytesReaded = this.dataInputStream.read(b_array_var);
if (bytesReaded < field.getLength()) {
throw new EOFException("Unexpected end of file");
}
this.dataInputStream.readFully(b_array_var);
return b_array_var;
case DATE:

byte[] t_byte_year = new byte[4];
bytesReaded = this.dataInputStream.read(t_byte_year);
if (bytesReaded < 4) {
throw new EOFException("Unexpected end of file");
}

this.dataInputStream.readFully(t_byte_year);

byte[] t_byte_month = new byte[2];
bytesReaded = this.dataInputStream.read(t_byte_month);
if (bytesReaded < 2) {
throw new EOFException("Unexpected end of file");
}
this.dataInputStream.readFully(t_byte_month);

byte[] t_byte_day = new byte[2];
bytesReaded = this.dataInputStream.read(t_byte_day);
if (bytesReaded < 2) {
throw new EOFException("Unexpected end of file");
}
this.dataInputStream.readFully(t_byte_day);

try {
GregorianCalendar calendar = new GregorianCalendar(Integer.parseInt(new String(t_byte_year, DBFStandardCharsets.US_ASCII)),
Expand Down Expand Up @@ -512,10 +496,7 @@ protected Object getFieldValue(DBFField field) throws IOException {
return readDoubleField(field);
case NULL_FLAGS:
byte [] data1 = new byte[field.getLength()];
int readed = dataInputStream.read(data1);
if (readed != field.getLength()){
throw new EOFException("Unexpected end of file");
}
this.dataInputStream.readFully(data1);
return DBFUtils.getBitSet(data1);
default:
skip(field.getLength());
Expand All @@ -525,10 +506,7 @@ protected Object getFieldValue(DBFField field) throws IOException {

private Object readDoubleField(DBFField field) throws IOException {
byte[] data = new byte[field.getLength()];
int bytesReaded = this.dataInputStream.read(data);
if (bytesReaded < field.getLength()) {
throw new EOFException("Unexpected end of file");
}
this.dataInputStream.readFully(data);
return ByteBuffer.wrap(
new byte[]{
data[7], data[6], data[5], data[4],
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/com/linuxense/javadbf/DBFUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,8 @@ private DBFUtils() {
*/
public static Number readNumericStoredAsText(DataInputStream dataInput, int length) throws IOException {
byte[] t_float = new byte[length];
int readed = dataInput.read(t_float);
if (readed != length) {
throw new EOFException("failed to read:" + length + " bytes");
}
dataInput.readFully(t_float);

t_float = DBFUtils.removeSpaces(t_float);
t_float = DBFUtils.removeNullBytes(t_float);
if (t_float.length > 0 && DBFUtils.isPureAscii(t_float) && !DBFUtils.contains(t_float, (byte) '?') && !DBFUtils.contains(t_float, (byte) '*')) {
Expand Down

0 comments on commit c378f58

Please sign in to comment.