/*
DBFReader
Class for reading the records assuming that the given
InputStream comtains DBF data.
This file is part of JavaDBF packege.
Author: anil@linuxense.com
License: LGPL (http://www.gnu.org/copyleft/lesser.html)
$Id: DBFReader.java,v 1.8 2004/03/31 10:54:03 anil Exp $
*/
package com.linuxense.javadbf;
import java.io.*;
import java.util.*;
/**
DBFReader class can creates objects to represent DBF data.
This Class is used to read data from a DBF file. Meta data and
records can be queried against this document.
DBFReader cannot write anythng to a DBF file. For creating DBF files
use DBFWriter.
Fetching rocord is possible only in the forward direction and
cannot re-wound. In such situation, a suggested approach is to reconstruct the object.
The nextRecord() method returns an array of Objects and the types of these
Object are as follows:
| xBase Type | Java Type |
| C | String |
| N | Integer |
| F | Double |
| L | Boolean |
| D | java.util.Date |
*/
public class DBFReader extends DBFBase {
DataInputStream dataInputStream;
DBFHeader header;
/* Class specific variables */
boolean isClosed = true;
/**
Initializes a DBFReader object.
When this constructor returns the object
will have completed reading the hader (meta date) and
header information can be quried there on. And it will
be ready to return the first row.
@param InputStream where the data is read from.
*/
public DBFReader( InputStream in) throws DBFException {
try {
this.dataInputStream = new DataInputStream( in);
this.isClosed = false;
this.header = new DBFHeader();
this.header.read( this.dataInputStream);
/* it might be required to leap to the start of records at times */
int t_dataStartIndex = this.header.headerLength - ( 32 + (32*this.header.fieldArray.length)) - 1;
if( t_dataStartIndex > 0) {
dataInputStream.skip( t_dataStartIndex);
}
}
catch( IOException e) {
throw new DBFException( e.getMessage());
}
}
public String toString() {
StringBuffer sb = new StringBuffer( this.header.year + "/" + this.header.month + "/" + this.header.day + "\n"
+ "Total records: " + this.header.numberOfRecords +
"\nHEader length: " + this.header.headerLength +
"");
for( int i=0; i 0 && !Utils.contains( t_float, (byte)'?')) {
recordObjects[i] = new Float( new String( t_float));
}
else {
recordObjects[i] = null;
}
}
catch( NumberFormatException e) {
throw new DBFException( "Failed to parse Float: " + e.getMessage());
}
break;
case 'N':
try {
byte t_numeric[] = new byte[ this.header.fieldArray[i].getFieldLength()];
dataInputStream.read( t_numeric);
t_numeric = Utils.trimLeftSpaces( t_numeric);
if( t_numeric.length > 0 && !Utils.contains( t_numeric, (byte)'?')) {
recordObjects[i] = new Double( new String( t_numeric));
}
else {
recordObjects[i] = null;
}
}
catch( NumberFormatException e) {
throw new DBFException( "Failed to parse Number: " + e.getMessage());
}
break;
case 'L':
byte t_logical = dataInputStream.readByte();
if( t_logical == 'Y' || t_logical == 't' || t_logical == 'T' || t_logical == 't') {
recordObjects[i] = Boolean.TRUE;
}
else {
recordObjects[i] = Boolean.FALSE;
}
break;
case 'M':
// TODO Later
recordObjects[i] = new String( "null");
break;
default:
recordObjects[i] = new String( "null");
}
}
}
catch( EOFException e) {
return null;
}
catch( IOException e) {
throw new DBFException( e.getMessage());
}
return recordObjects;
}
}