Statistics
| Revision:

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

History | View | Annotate | Download (9.71 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
import java.io.RandomAccessFile;
16

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

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

    
22

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

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

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

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

    
61
        //Se calcula el offset del campo
62
        int fieldOffset = 0;
63

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

    
68
        buffer.position(recordOffset + fieldOffset);
69

    
70
        char bool = (char) buffer.get();
71

    
72
        return ((bool == 't') || (bool == 'T') || (bool == 'Y') ||
73
        (bool == 'y'));
74
    }
75

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

    
88
        //Se calcula el offset del campo
89
        int fieldOffset = 0;
90

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

    
95
        buffer.position(recordOffset + fieldOffset);
96

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

    
100
        return new String(data);
101
    }
102

    
103
    // Retrieve the record at the given index
104

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

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

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

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

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

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

    
268
    /**
269
     * read the DBF file into memory.
270
     *
271
     * @param file DOCUMENT ME!
272
     *
273
     * @throws IOException DOCUMENT ME!
274
     */
275
    public void open(File file) throws IOException {
276
        if (file.canWrite()){
277
            raf = new RandomAccessFile(file, "rw");
278
            mode = FileChannel.MapMode.READ_WRITE;
279
        } else {
280
            raf = new RandomAccessFile(file, "r");
281
            mode = FileChannel.MapMode.READ_ONLY;
282
        }
283
        channel = raf.getChannel();
284

    
285
        // buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
286
        buffer = new BigByteBuffer(channel, mode);
287

    
288
        // create the header to contain the header information.
289
        myHeader = new DbaseFileHeader();
290
        myHeader.readHeader(buffer);
291
    }
292

    
293
    /**
294
     * Removes all data from the dataset
295
     *
296
     * @throws IOException DOCUMENT ME!
297
     */
298
    public void close() throws IOException {
299
        raf.close();
300
        channel.close();
301
        buffer = null;
302
    }
303

    
304
    public FileChannel getWriteChannel() {
305
        return channel;
306
    }
307
}