org.geotools.data.shapefile.dbf
Class DbaseFileReader

Object
  extended by DbaseFileReader
All Implemented Interfaces:
FileReader
Direct Known Subclasses:
IndexedDbaseFileReader

public class DbaseFileReader
extends Object
implements FileReader

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, Andrea Aaime
Module:
modules/plugin/shapefile (gt-shapefile.jar)

Nested Class Summary
 class DbaseFileReader.Row
           
 
Field Summary
protected  int currentOffset
           
protected  boolean randomAccessEnabled
           
protected  boolean useMemoryMappedBuffer
           
 
Constructor Summary
DbaseFileReader(ReadableByteChannel readChannel, boolean useMemoryMappedBuffer, Charset charset)
           
DbaseFileReader(ShpFiles shapefileFiles, boolean useMemoryMappedBuffer, Charset charset)
          Creates a new instance of DBaseFileReader
 
Method Summary
 void close()
          Clean up all resources associated with this reader.
protected  int fill(ByteBuffer buffer, ReadableByteChannel channel)
           
 DbaseFileHeader getHeader()
          Get the header from this file.
 boolean hasNext()
          Query the reader as to whether there is another record.
 String id()
          An id for the reader.
static void main(String[] args)
           
 void read()
          Reads the next record into memory.
 Object[] readEntry()
          Get the next record (entry).
 Object[] readEntry(Object[] entry)
          Copy the next entry into the array.
 Object[] readEntry(Object[] entry, int offset)
          Copy the next record into the array starting at offset.
 Object readField(int fieldNum)
          Reads a single field from the current record and returns it.
 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 Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

useMemoryMappedBuffer

protected boolean useMemoryMappedBuffer

randomAccessEnabled

protected boolean randomAccessEnabled

currentOffset

protected int currentOffset
Constructor Detail

DbaseFileReader

public DbaseFileReader(ShpFiles shapefileFiles,
                       boolean useMemoryMappedBuffer,
                       Charset charset)
                throws IOException
Creates a new instance of DBaseFileReader

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

DbaseFileReader

public DbaseFileReader(ReadableByteChannel readChannel,
                       boolean useMemoryMappedBuffer,
                       Charset charset)
                throws IOException
Throws:
IOException
Method Detail

fill

protected int fill(ByteBuffer buffer,
                   ReadableByteChannel channel)
            throws IOException
Throws:
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 IOException
Clean up all resources associated with this reader.Highly recomended.

Throws:
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 Object[] readEntry()
                   throws IOException
Get the next record (entry). Will return a new array of values.

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

readRow

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

skip

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

Throws:
IOException - If an error occurs.

readEntry

public Object[] readEntry(Object[] entry,
                          int offset)
                   throws 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:
IOException - If an error occurs.

readField

public Object readField(int fieldNum)
                 throws IOException
Reads a single field from the current record and returns it. Remember to call read() before starting to read fields from the dbf, and call it every time you need to move to the next record.

Parameters:
fieldNum - The field number to be read (zero based)
Returns:
The value of the field
Throws:
IOException - If an error occurs.

transferTo

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

Throws:
IOException

read

public void read()
          throws IOException
Reads the next record into memory. You need to use this directly when reading only a subset of the fields using readField(int).

Throws:
IOException

readEntry

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

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

main

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

id

public String id()
Description copied from interface: FileReader
An id for the reader. This is only used for debugging.

Specified by:
id in interface FileReader
Returns:
id for the reader.


Copyright © 1996-2009 Geotools. All Rights Reserved.