Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / drivers / dbf / DbaseFile.java @ 2462

History | View | Annotate | Download (9.37 KB)

1
/*
2
 * Created on 16-feb-2004
3
 *
4
 * To change the template for this generated file go to
5
 * Window>Preferences>Java>Code Generation>Code and Comments
6
 */
7
package com.iver.cit.gvsig.fmap.drivers.dbf;
8

    
9

    
10
/**
11
 */
12
import java.io.File;
13
import java.io.FileInputStream;
14
import java.io.IOException;
15

    
16
import java.nio.ByteBuffer;
17
import java.nio.channels.FileChannel;
18

    
19
import com.iver.utiles.bigfile.BigByteBuffer;
20

    
21

    
22
/**
23
 * Class to read and write data to a dbase III format file. Creation date:
24
 * (5/15/2001 5:15:13 PM)
25
 */
26
public class DbaseFile {
27
    // Header information for the DBase File
28
    private DbaseFileHeader myHeader;
29
    private FileInputStream fin;
30
    private FileChannel channel;
31
    private BigByteBuffer buffer;
32

    
33
    // Retrieve number of records in the DbaseFile
34
    public int getRecordCount() {
35
        return myHeader.getNumRecords();
36
    }
37

    
38
    /**
39
     * DOCUMENT ME!
40
     *
41
     * @return DOCUMENT ME!
42
     */
43
    public int getFieldCount() {
44
        return myHeader.getNumFields();
45
    }
46

    
47
    /**
48
     * DOCUMENT ME!
49
     *
50
     * @param rowIndex DOCUMENT ME!
51
     * @param fieldId DOCUMENT ME!
52
     *
53
     * @return DOCUMENT ME!
54
     */
55
    public boolean getBooleanFieldValue(int rowIndex, int fieldId) {
56
        int recordOffset = (myHeader.getRecordLength() * rowIndex) +
57
            myHeader.getHeaderLength() + 1;
58

    
59
        //Se calcula el offset del campo
60
        int fieldOffset = 0;
61

    
62
        for (int i = 0; i < (fieldId - 1); i++) {
63
            fieldOffset += myHeader.getFieldLength(i);
64
        }
65

    
66
        buffer.position(recordOffset + fieldOffset);
67

    
68
        char bool = (char) buffer.get();
69

    
70
        return ((bool == 't') || (bool == 'T') || (bool == 'Y') ||
71
        (bool == 'y'));
72
    }
73

    
74
    /**
75
     * DOCUMENT ME!
76
     *
77
     * @param rowIndex DOCUMENT ME!
78
     * @param fieldId DOCUMENT ME!
79
     *
80
     * @return DOCUMENT ME!
81
     */
82
    public String getStringFieldValue(int rowIndex, int fieldId) {
83
        int recordOffset = (myHeader.getRecordLength() * rowIndex) +
84
            myHeader.getHeaderLength() + 1;
85

    
86
        //Se calcula el offset del campo
87
        int fieldOffset = 0;
88

    
89
        for (int i = 0; i < fieldId; i++) {
90
            fieldOffset += myHeader.getFieldLength(i);
91
        }
92

    
93
        buffer.position(recordOffset + fieldOffset);
94

    
95
        byte[] data = new byte[myHeader.getFieldLength(fieldId)];
96
        buffer.get(data);
97

    
98
        return new String(data);
99
    }
100

    
101
    // Retrieve the record at the given index
102

    
103
    /*    public Object[] getRecord(long inIndex) throws IOException {
104
       long nRecordOffset = (myHeader.getRecordLength() * inIndex) +
105
           myHeader.getHeaderLength();
106
       // retrieve the record length
107
       int tempNumFields = myHeader.getNumFields();
108
       // storage for the actual values
109
       Object[] tempRow = new Object[tempNumFields];
110
           buffer.position((int) nRecordOffset);
111
           // read the deleted flag
112
           char tempDeleted = (char) buffer.get();
113
           // read the record length
114
           int tempRecordLength = 1; // for the deleted character just read.
115
           // read the Fields
116
           for (int j = 0; j < tempNumFields; j++) {
117
               // find the length of the field.
118
               int tempFieldLength = myHeader.getFieldLength(j);
119
               tempRecordLength = tempRecordLength + tempFieldLength;
120
               // find the field type
121
               char tempFieldType = myHeader.getFieldType(j);
122
               //System.out.print("Reading Name="+myHeader.getFieldName(j)+" Type="+tempFieldType +" Length="+tempFieldLength);
123
               // read the data.
124
               Object tempObject = null;
125
               switch (tempFieldType) {
126
               case 'L': // logical data type, one character (T,t,F,f,Y,y,N,n)
127
                   char tempChar = (char) buffer.get();
128
                   if ((tempChar == 'T') || (tempChar == 't') ||
129
                           (tempChar == 'Y') || (tempChar == 'y')) {
130
                       tempObject = new Boolean(true);
131
                   } else {
132
                       tempObject = new Boolean(false);
133
                   }
134
                   break;
135
               case 'C': // character record.
136
                   byte[] sbuffer = new byte[tempFieldLength];
137
                                       buffer.get(sbuffer);
138
                   tempObject = new String(sbuffer, "ISO-8859-1").trim();
139
                   break;
140
               case 'D': // date data type.
141
                   byte[] dbuffer = new byte[8];
142
                                       buffer.get(dbuffer);
143
                   String tempString = new String(dbuffer, 0, 4);
144
                   try {
145
                       int tempYear = Integer.parseInt(tempString);
146
                       tempString = new String(dbuffer, 4, 2);
147
                       int tempMonth = Integer.parseInt(tempString) - 1;
148
                       tempString = new String(dbuffer, 6, 2);
149
                       int tempDay = Integer.parseInt(tempString);
150
                       Calendar c = Calendar.getInstance();
151
                       c.set(Calendar.YEAR, tempYear);
152
                       c.set(Calendar.MONTH, tempMonth);
153
                       c.set(Calendar.DAY_OF_MONTH, tempDay);
154
                       tempObject = c.getTime();
155
                   } catch (NumberFormatException e) {
156
                   }
157
                   break;
158
               case 'M': // memo field.
159
                   byte[] mbuffer = new byte[10];
160
                                       buffer.get(mbuffer);
161
                   break;
162
               case 'N': // number
163
               case 'F': // floating point number
164
                   byte[] fbuffer = new byte[tempFieldLength];
165
                                       buffer.get(fbuffer);
166
                   try {
167
                       tempString = new String(fbuffer);
168
                       tempObject = Double.valueOf(tempString.trim());
169
                   } catch (NumberFormatException e) {
170
                   }
171
                   break;
172
               default:
173
                   byte[] defbuffer = new byte[tempFieldLength];
174
                                       buffer.get(defbuffer);
175
                   System.out.println("Do not know how to parse Field type " +
176
                       tempFieldType);
177
               }
178
               tempRow[j] = tempObject;
179
               //                                System.out.println(" Data="+tempObject);
180
           }
181
           // ensure that the full record has been read.
182
           if (tempRecordLength < myHeader.getRecordLength()) {
183
               byte[] tempbuff = new byte[myHeader.getRecordLength() -
184
                   tempRecordLength];
185
               buffer.get(tempbuff);
186
               /* if (tempTelling){
187
                       System.out.println("DBF File has "+(myHeader.getRecordLength()-tempRecordLength)+" extra bytes per record");
188
                       tempTelling = false;
189
               } */
190
    /*           }
191
       return tempRow;
192
       }
193
     */
194

    
195
    /**
196
     * Retrieve the name of the given column.
197
     *
198
     * @param inIndex DOCUMENT ME!
199
     *
200
     * @return DOCUMENT ME!
201
     */
202
    public String getFieldName(int inIndex) {
203
        return myHeader.getFieldName(inIndex).trim();
204
    }
205

    
206
    /**
207
     * Retrieve the type of the given column.
208
     *
209
     * @param inIndex DOCUMENT ME!
210
     *
211
     * @return DOCUMENT ME!
212
     */
213
    public char getFieldType(int inIndex) {
214
        return myHeader.getFieldType(inIndex);
215
    }
216

    
217
    /**
218
     * Retrieve the length of the given column.
219
     *
220
     * @param inIndex DOCUMENT ME!
221
     *
222
     * @return DOCUMENT ME!
223
     */
224
    public int getFieldLength(int inIndex) {
225
        return myHeader.getFieldLength(inIndex);
226
    }
227

    
228
    /*
229
     * Retrieve the value of the given column as string.
230
     *
231
     * @param idField DOCUMENT ME!
232
     * @param idRecord DOCUMENT ME!
233
     *
234
     * @return DOCUMENT ME!
235
     *
236
             public Object getFieldValue(int idField, long idRecord) throws IOException {
237
                 Object[] tmpReg = getRecord(idRecord);
238
                 return tmpReg[idField];
239
             }
240
     */
241
    /*
242
     * DOCUMENT ME!
243
     *
244
     * @param idField DOCUMENT ME!
245
     * @param idRecord DOCUMENT ME!
246
     *
247
     * @return DOCUMENT ME!
248
     *
249
             public double getFieldValueAsDouble(int idField, int idRecord) throws IOException {
250
                 Object[] tmpReg = getRecord(idRecord);
251
                 return (double) Double.parseDouble(tmpReg[idField].toString());
252
             }
253
     */
254

    
255
    /**
256
     * Retrieve the location of the decimal point.
257
     *
258
     * @param inIndex DOCUMENT ME!
259
     *
260
     * @return DOCUMENT ME!
261
     */
262
    public int getFieldDecimalLength(int inIndex) {
263
        return myHeader.getFieldDecimalCount(inIndex);
264
    }
265

    
266
    /**
267
     * read the DBF file into memory.
268
     *
269
     * @param file DOCUMENT ME!
270
     *
271
     * @throws IOException DOCUMENT ME!
272
     */
273
    public void open(File file) throws IOException {
274
        fin = new FileInputStream(file);
275
        channel = fin.getChannel();
276

    
277
        // buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
278
        buffer = new BigByteBuffer(channel, FileChannel.MapMode.READ_ONLY);
279

    
280
        // create the header to contain the header information.
281
        myHeader = new DbaseFileHeader();
282
        myHeader.readHeader(buffer);
283
    }
284

    
285
    /**
286
     * Removes all data from the dataset
287
     *
288
     * @throws IOException DOCUMENT ME!
289
     */
290
    public void close() throws IOException {
291
        fin.close();
292
        channel.close();
293
        buffer = null;
294
    }
295
}