jwo.utils.dbase
Class DbaseFileHeader

java.lang.Object
  extended by jwo.utils.dbase.DbaseFileHeader

public class DbaseFileHeader
extends Object

Represents the header of a Dbase III file. This code is based on the class provided as part of the Geotools OpenSource mapping toolkit - www.geotools.org/ under the GNU Lesser General Public License. That in turn is based on the original code in the GISToolkit project - gistoolkit.sourceforge.net.

Version:
2.3, 4th October, 2007.
Author:
Geotools/GISToolkit modified by Jo Wood.

Constructor Summary
DbaseFileHeader()
          Creates a class for representing a DBase III header.
DbaseFileHeader(Logger logger)
          Creates a class for representing a DBase III header with logged warning and error messages.
 
Method Summary
 void addColumn(String inFieldName, char inFieldType, int inFieldLength, int inDecimalCount)
          Adds a column to this DbaseFileHeader.
 Class getFieldClass(int i)
          Determines the most appropriate Java Class for representing the data in the field.
 int getFieldDecimalCount(int inIndex)
          Reports the location of the decimal point within the field.
 int getFieldLength(int inIndex)
          Reports the field length in bytes.
 String getFieldName(int inIndex)
          Reports the name of the field at the given index.
 char getFieldType(int inIndex)
          Reports the type of field at the given index.
 int getHeaderLength()
          Reports the length of the header.
 int getLargestFieldSize()
          Reports the largest field size of this table.
 Date getLastUpdateDate()
          Reports the date this file was last updated.
 int getNumFields()
          Reports the number of fields in the records.
 int getNumRecords()
          Reports the number of records in the file.
 int getRecordLength()
          Reports the length of the records in bytes.
 void readHeader(ReadableByteChannel channel)
          Reads the header data from the DBF file.
 int removeColumn(String inFieldName)
          Removes a column from this DbaseFileHeader.
 void setNumRecords(int inNumRecords)
          Sets the number of records in the file.
 String toString()
          Provides a simple representation of this header.
 void writeHeader(WritableByteChannel out)
          Writes the header data to the DBF file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

DbaseFileHeader

public DbaseFileHeader()
Creates a class for representing a DBase III header.


DbaseFileHeader

public DbaseFileHeader(Logger logger)
Creates a class for representing a DBase III header with logged warning and error messages.

Parameters:
logger - Logger to monitor warning and error messages.
Method Detail

getFieldClass

public Class getFieldClass(int i)
Determines the most appropriate Java Class for representing the data in the field.
 All packages are java.lang unless otherwise specified.
 C (Character) -> String
 N (Numeric)   -> Integer or Double (depends on field's decimal count)
 F (Floating)  -> Double
 L (Logical)   -> Boolean
 D (Date)      -> java.util.Date
 Unknown       -> String
 

Parameters:
i - The index of the field, from 0 to getNumFields() - 1 .
Returns:
A Class which closely represents the dbase field type.

addColumn

public void addColumn(String inFieldName,
                      char inFieldType,
                      int inFieldLength,
                      int inDecimalCount)
Adds a column to this DbaseFileHeader. The type is one of (C N L or D) character, number, logical(true/false), or date. The Field length is the total length in bytes reserved for this column. Note that this length applies to the representation in text form, not binary. Therefore numbers with large numbers of digits require proportionately more bytes than their floating point or double precision equivalent. The decimal count only applies to numbers(N), and floating point values (F), and refers to the number of characters to reserve after the decimal point.
 Field Type MaxLength
 ---------- ---------
 C          254
 D          8
 F          20
 N          18
 

Parameters:
inFieldName - The name of the new field, must be less than 10 characters or it gets truncated.
inFieldType - A character representing the dBase field, (see above). Case insensitive.
inFieldLength - The length of the field, in bytes at 1 byte per digit (see above).
inDecimalCount - For numeric fields, the number of decimal places to track.

removeColumn

public int removeColumn(String inFieldName)
Removes a column from this DbaseFileHeader.

Parameters:
inFieldName - The name of the field, will ignore case and trim.
Returns:
index of the removed column, -1 if not found.

getFieldLength

public int getFieldLength(int inIndex)
Reports the field length in bytes.

Parameters:
inIndex - The field index.
Returns:
The length in bytes.

getFieldDecimalCount

public int getFieldDecimalCount(int inIndex)
Reports the location of the decimal point within the field.

Parameters:
inIndex - The field index.
Returns:
The decimal count.

getFieldName

public String getFieldName(int inIndex)
Reports the name of the field at the given index.

Parameters:
inIndex - The field index.
Returns:
The name of the field.

getFieldType

public char getFieldType(int inIndex)
Reports the type of field at the given index.

Parameters:
inIndex - The field index.
Returns:
The dbase character representing this field.

getLastUpdateDate

public Date getLastUpdateDate()
Reports the date this file was last updated.

Returns:
The Date last modified.

getNumFields

public int getNumFields()
Reports the number of fields in the records.

Returns:
The number of fields in this table.

getNumRecords

public int getNumRecords()
Reports the number of records in the file.

Returns:
The number of records in this table.

getRecordLength

public int getRecordLength()
Reports the length of the records in bytes.

Returns:
The number of bytes per record.

getHeaderLength

public int getHeaderLength()
Reports the length of the header.

Returns:
The length of the header in bytes.

readHeader

public void readHeader(ReadableByteChannel channel)
                throws IOException
Reads the header data from the DBF file.

Parameters:
channel - A readable byte channel. If you have an InputStream you need to use, you can call java.nio.Channels.getChannel(InputStream in).
Throws:
IOException - If errors occur while reading.

getLargestFieldSize

public int getLargestFieldSize()
Reports the largest field size of this table.

Returns:
The largest field size in bytes.

setNumRecords

public void setNumRecords(int inNumRecords)
Sets the number of records in the file.

Parameters:
inNumRecords - The number of records.

writeHeader

public void writeHeader(WritableByteChannel out)
                 throws IOException
Writes the header data to the DBF file.

Parameters:
out - A channel to write to. If you have an OutputStream you can obtain the correct channel by using java.nio.Channels.newChannel(OutputStream out).
Throws:
IOException - If errors occur.

toString

public String toString()
Provides a simple representation of this header.

Overrides:
toString in class Object
Returns:
A String representing the state of the header.


Copyright Jo Wood, 1996-2009, last modified, 17th April, 2009