001: /*
002: * Geotools - OpenSource mapping toolkit
003: * (C) 2003-2006, GeoTools Project Managment Committee (PMC)
004: * (C) 2002, Centre for Computational Geography
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * This file is based on an origional contained in the GISToolkit project:
017: * http://gistoolkit.sourceforge.net/
018: */
019: package org.geotools.data.shapefile.dbf;
020:
021: import java.io.FileInputStream;
022: import java.io.IOException;
023: import java.nio.channels.FileChannel;
024: import java.nio.channels.ReadableByteChannel;
025: import java.nio.charset.Charset;
026:
027: /** A DbaseFileReader is used to read a dbase III format file.
028: * <br>
029: * The general use of this class is:
030: * <CODE><PRE>
031: * FileChannel in = new FileInputStream("thefile.dbf").getChannel();
032: * DbaseFileReader r = new DbaseFileReader( in )
033: * Object[] fields = new Object[r.getHeader().getNumFields()];
034: * while (r.hasNext()) {
035: * r.readEntry(fields);
036: * // do stuff
037: * }
038: * r.close();
039: * </PRE></CODE>
040: * For consumers who wish to be a bit more selective with their reading of rows,
041: * the Row object has been added. The semantics are the same as using the
042: * readEntry method, but remember that the Row object is always the same. The
043: * values are parsed as they are read, so it pays to copy them out (as each call
044: * to Row.read() will result in an expensive String parse).
045: * <br><b>EACH CALL TO readEntry OR readRow ADVANCES THE FILE!</b><br>
046: * An example of using the Row method of reading:
047: * <CODE><PRE>
048: * FileChannel in = new FileInputStream("thefile.dbf").getChannel();
049: * DbaseFileReader r = new DbaseFileReader( in )
050: * int fields = r.getHeader().getNumFields();
051: * while (r.hasNext()) {
052: * DbaseFileReader.Row row = r.readRow();
053: * for (int i = 0; i < fields; i++) {
054: * // do stuff
055: * Foo.bar( row.read(i) );
056: * }
057: * }
058: * r.close();
059: * </PRE></CODE>
060: *
061: * @author Ian Schneider
062: * @author Tommaso Nolli
063: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/plugin/shapefile/src/main/java/org/geotools/data/shapefile/dbf/IndexedDbaseFileReader.java $
064: */
065: public class IndexedDbaseFileReader extends DbaseFileReader {
066:
067: /**
068: *
069: * @param recno
070: * @throws IOException
071: * @throws UnsupportedOperationException
072: */
073: public void goTo(int recno) throws IOException,
074: UnsupportedOperationException {
075:
076: if (this .randomAccessEnabled) {
077: int newPosition = this .header.getHeaderLength()
078: + this .header.getRecordLength() * (recno - 1);
079:
080: if (this .useMemoryMappedBuffer) {
081: buffer.position(newPosition);
082: } else {
083: FileChannel fc = (FileChannel) this .channel;
084: fc.position(newPosition);
085: buffer.limit(buffer.capacity());
086: buffer.position(0);
087: fill(buffer, channel);
088: buffer.position(0);
089:
090: this .currentOffset = newPosition;
091: }
092: } else {
093: throw new UnsupportedOperationException(
094: "Random access not enabled!");
095: }
096:
097: }
098:
099: /**
100: * Like calling DbaseFileReader(ReadableByteChannel, true);
101: * @param channel
102: * @throws IOException
103: */
104: public IndexedDbaseFileReader(ReadableByteChannel channel)
105: throws IOException {
106: this (channel, true);
107: }
108:
109: /** Creates a new instance of DBaseFileReader
110: * @param channel The readable channel to use.
111: * @param useMemoryMappedBuffer Wether or not map the file in memory
112: * @throws IOException If an error occurs while initializing.
113: */
114: public IndexedDbaseFileReader(ReadableByteChannel channel,
115: boolean useMemoryMappedBuffer) throws IOException {
116: super (channel, useMemoryMappedBuffer);
117: }
118:
119: public IndexedDbaseFileReader(ReadableByteChannel channel,
120: boolean useMemoryMappedBuffer, Charset stringCharset)
121: throws IOException {
122: super (channel, useMemoryMappedBuffer, stringCharset);
123: }
124:
125: public boolean IsRandomAccessEnabled() {
126: return this .randomAccessEnabled;
127: }
128:
129: public static void main(String[] args) throws Exception {
130: FileChannel channel = new FileInputStream(args[0]).getChannel();
131: IndexedDbaseFileReader reader = new IndexedDbaseFileReader(
132: channel, true);
133: System.out.println(reader.getHeader());
134: int r = 0;
135: while (reader.hasNext()) {
136: System.out.println(++r + ","
137: + java.util.Arrays.asList(reader.readEntry()));
138: }
139: reader.close();
140: }
141:
142: }
|