org.geotools.data.shapefile.dbf
Class DbaseFileReader

java.lang.Object
  extended byorg.geotools.data.shapefile.dbf.DbaseFileReader
Direct Known Subclasses:
IndexedDbaseFileReader

public class DbaseFileReader
extends java.lang.Object

A DbaseFileReader is used to read a dbase III format file.
The general use of this class is:

 
 FileChannel in = new FileInputStream("thefile.dbf").getChannel();
 DbaseFileReader r = new DbaseFileReader( in ) Object[] fields = new
 Object[r.getHeader().getNumFields()]; while (r.hasNext()) {
 r.readEntry(fields); // do stuff } r.close();
 
 
For consumers who wish to be a bit more selective with their reading of rows, the Row object has been added. The semantics are the same as using the readEntry method, but remember that the Row object is always the same. The values are parsed as they are read, so it pays to copy them out (as each call to Row.read() will result in an expensive String parse).
EACH CALL TO readEntry OR readRow ADVANCES THE FILE!
An example of using the Row method of reading:
 
 FileChannel in = new FileInputStream("thefile.dbf").getChannel();
 DbaseFileReader r = new DbaseFileReader( in ) int fields =
 r.getHeader().getNumFields(); while (r.hasNext()) { DbaseFileReader.Row row =
 r.readRow(); for (int i = 0; i < fields; i++) { // do stuff Foo.bar(
 row.read(i) ); } } r.close();
 
 

Author:
Ian Schneider

Nested Class Summary
 class DbaseFileReader.Row
           
 
Field Summary
protected  int currentOffset
           
protected  boolean randomAccessEnabled
           
protected  boolean useMemoryMappedBuffer
           
 
Constructor Summary
DbaseFileReader(java.nio.channels.ReadableByteChannel channel)
          Creates a new instance of DBaseFileReader
DbaseFileReader(java.nio.channels.ReadableByteChannel channel, boolean useMemoryMappedBuffer)
          Creates a new instance of DBaseFileReader
 
Method Summary
 void close()
          Clean up all resources associated with this reader.
protected  int fill(java.nio.ByteBuffer buffer, java.nio.channels.ReadableByteChannel channel)
           
 DbaseFileHeader getHeader()
          Get the header from this file.
 boolean hasNext()
          Query the reader as to whether there is another record.
static void main(java.lang.String[] args)
           
 java.lang.Object[] readEntry()
          Get the next record (entry).
 java.lang.Object[] readEntry(java.lang.Object[] entry)
          Copy the next entry into the array.
 java.lang.Object[] readEntry(java.lang.Object[] entry, int offset)
          Copy the next record into the array starting at offset.
 DbaseFileReader.Row readRow()
           
 void skip()
          Skip the next record.
 void transferTo(DbaseFileWriter writer)
          Transfer, by bytes, the next record to the writer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

useMemoryMappedBuffer

protected boolean useMemoryMappedBuffer

randomAccessEnabled

protected final boolean randomAccessEnabled

currentOffset

protected int currentOffset
Constructor Detail

DbaseFileReader

public DbaseFileReader(java.nio.channels.ReadableByteChannel channel)
                throws java.io.IOException
Creates a new instance of DBaseFileReader

Parameters:
channel - The readable channel to use.
Throws:
java.io.IOException - If an error occurs while initializing.

DbaseFileReader

public DbaseFileReader(java.nio.channels.ReadableByteChannel channel,
                       boolean useMemoryMappedBuffer)
                throws java.io.IOException
Creates a new instance of DBaseFileReader

Parameters:
channel - The readable channel to use.
Throws:
java.io.IOException - If an error occurs while initializing.
Method Detail

fill

protected int fill(java.nio.ByteBuffer buffer,
                   java.nio.channels.ReadableByteChannel channel)
            throws java.io.IOException
Throws:
java.io.IOException

getHeader

public DbaseFileHeader getHeader()
Get the header from this file. The header is read upon instantiation.

Returns:
The header associated with this file or null if an error occurred.

close

public void close()
           throws java.io.IOException
Clean up all resources associated with this reader.Highly recomended.

Throws:
java.io.IOException - If an error occurs.

hasNext

public boolean hasNext()
Query the reader as to whether there is another record.

Returns:
True if more records exist, false otherwise.

readEntry

public java.lang.Object[] readEntry()
                             throws java.io.IOException
Get the next record (entry). Will return a new array of values.

Returns:
A new array of values.
Throws:
java.io.IOException - If an error occurs.

readRow

public DbaseFileReader.Row readRow()
                            throws java.io.IOException
Throws:
java.io.IOException

skip

public void skip()
          throws java.io.IOException
Skip the next record.

Throws:
java.io.IOException - If an error occurs.

readEntry

public java.lang.Object[] readEntry(java.lang.Object[] entry,
                                    int offset)
                             throws java.io.IOException
Copy the next record into the array starting at offset.

Parameters:
entry - Th array to copy into.
offset - The offset to start at
Returns:
The same array passed in.
Throws:
java.io.IOException - If an error occurs.

transferTo

public void transferTo(DbaseFileWriter writer)
                throws java.io.IOException
Transfer, by bytes, the next record to the writer.

Throws:
java.io.IOException

readEntry

public java.lang.Object[] readEntry(java.lang.Object[] entry)
                             throws java.io.IOException
Copy the next entry into the array.

Parameters:
entry - The array to copy into.
Returns:
The same array passed in.
Throws:
java.io.IOException - If an error occurs.

main

public static void main(java.lang.String[] args)
                 throws java.lang.Exception
Throws:
java.lang.Exception


Copyright © GeoTools. All Rights Reserved.