Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / drivers / shp / DbaseFileNIO.java @ 4430

History | View | Annotate | Download (10.8 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package com.iver.cit.gvsig.fmap.drivers.shp;
42

    
43
import java.io.File;
44
import java.io.FileInputStream;
45
import java.io.IOException;
46
import java.nio.ByteBuffer;
47
import java.nio.channels.FileChannel;
48

    
49
import com.iver.utiles.bigfile.BigByteBuffer;
50
import com.iver.utiles.bigfile.BigByteBuffer2;
51

    
52

    
53
/**
54
 * Class to read and write data to a dbase III format file. Creation date:
55
 * (5/15/2001 5:15:13 PM)
56
 */
57
public class DbaseFileNIO {
58
        // Header information for the DBase File
59
        private DbaseFileHeaderNIO myHeader;
60
        private FileInputStream fin;
61
        private FileChannel channel;
62
        private BigByteBuffer2 buffer;
63

    
64
        /**
65
         * Retrieve number of records in the DbaseFile
66
         *
67
         * @return N?mero de registros.
68
         */
69
        public int getRecordCount() {
70
                return myHeader.getNumRecords();
71
        }
72

    
73
        /**
74
         * Devuelve el n?mero de fields.
75
         *
76
         * @return N?mero de fields.
77
         */
78
        public int getFieldCount() {
79
                return myHeader.getNumFields();
80
        }
81

    
82
        /**
83
         * Devuelve el valor de un boolean a partir de su n?mero de fila y de
84
         * field.
85
         *
86
         * @param rowIndex N?mero de fila.
87
         * @param fieldId N?mero columna.
88
         *
89
         * @return boolean.
90
         */
91
        public boolean getBooleanFieldValue(int rowIndex, int fieldId) {
92
                int recordOffset = (myHeader.getRecordLength() * rowIndex) +
93
                        myHeader.getHeaderLength() + 1;
94

    
95
                //Se calcula el offset del campo
96
                int fieldOffset = 0;
97

    
98
                for (int i = 0; i < (fieldId - 1); i++) {
99
                        fieldOffset += myHeader.getFieldLength(i);
100
                }
101

    
102
                buffer.position(recordOffset + fieldOffset);
103

    
104
                char bool = (char) buffer.get();
105

    
106
                return ((bool == 't') || (bool == 'T') || (bool == 'Y') ||
107
                (bool == 'y'));
108
        }
109

    
110
        /**
111
         * Devuelve el String a partir del n?mero de fila y columna.
112
         *
113
         * @param rowIndex N?mero de fila.
114
         * @param fieldId N?mero de columna.
115
         *
116
         * @return String.
117
         */
118
        public String getStringFieldValue(int rowIndex, int fieldId) {
119
                int recordOffset = (myHeader.getRecordLength() * rowIndex) +
120
                        myHeader.getHeaderLength() + 1;
121

    
122
                //Se calcula el offset del campo
123
                int fieldOffset = 0;
124

    
125
                for (int i = 0; i < fieldId; i++) {
126
                        fieldOffset += myHeader.getFieldLength(i);
127
                }
128

    
129
                buffer.position(recordOffset + fieldOffset);
130

    
131
                byte[] data = new byte[myHeader.getFieldLength(fieldId)];
132
                buffer.get(data);
133

    
134
                return new String(data);
135
        }
136

    
137
        /**
138
         * Devuelve el Number a partir de una fila y columna.
139
         *
140
         * @param rowIndex N?mero fila.
141
         * @param fieldId N?mero columna.
142
         *
143
         * @return Number.
144
         */
145
        public Number getNumberFieldValue(int rowIndex, int fieldId) {
146
                //System.out.println("rowIndex = "+rowIndex+ " , "+"fieldId = "+fieldId);
147
                int recordOffset = (myHeader.getRecordLength() * rowIndex) +
148
                        myHeader.getHeaderLength() + 1;
149

    
150
                //Se calcula el offset del campo
151
                int fieldOffset = 0;
152

    
153
                for (int i = 0; i < fieldId; i++) {
154
                        fieldOffset += myHeader.getFieldLength(i);
155
                }
156

    
157
                buffer.position(recordOffset + fieldOffset);
158

    
159
                byte[] data = new byte[myHeader.getFieldLength(fieldId)];
160
                buffer.get(data);
161

    
162
                String s = new String(data);
163
                s = s.trim();
164

    
165
                if (getFieldType(fieldId) == 'N') {
166
                        Object tempObject = Double.valueOf(s);
167

    
168
                        return new Double(tempObject.toString());
169
                } else {
170
                        Object tempObject = Integer.valueOf(s);
171

    
172
                        return new Integer(tempObject.toString());
173
                }
174

    
175
                //return 0;
176
        }
177

    
178
        // Retrieve the record at the given index
179

    
180
        /*    public Object[] getRecord(long inIndex) throws IOException {
181
           long nRecordOffset = (myHeader.getRecordLength() * inIndex) +
182
               myHeader.getHeaderLength();
183
           // retrieve the record length
184
           int tempNumFields = myHeader.getNumFields();
185
           // storage for the actual values
186
           Object[] tempRow = new Object[tempNumFields];
187
               buffer.position((int) nRecordOffset);
188
               // read the deleted flag
189
               char tempDeleted = (char) buffer.get();
190
               // read the record length
191
               int tempRecordLength = 1; // for the deleted character just read.
192
               // read the Fields
193
               for (int j = 0; j < tempNumFields; j++) {
194
                   // find the length of the field.
195
                   int tempFieldLength = myHeader.getFieldLength(j);
196
                   tempRecordLength = tempRecordLength + tempFieldLength;
197
                   // find the field type
198
                   char tempFieldType = myHeader.getFieldType(j);
199
                   //System.out.print("Reading Name="+myHeader.getFieldName(j)+" Type="+tempFieldType +" Length="+tempFieldLength);
200
                   // read the data.
201
                   Object tempObject = null;
202
                   switch (tempFieldType) {
203
                   case 'L': // logical data type, one character (T,t,F,f,Y,y,N,n)
204
                       char tempChar = (char) buffer.get();
205
                       if ((tempChar == 'T') || (tempChar == 't') ||
206
                               (tempChar == 'Y') || (tempChar == 'y')) {
207
                           tempObject = new Boolean(true);
208
                       } else {
209
                           tempObject = new Boolean(false);
210
                       }
211
                       break;
212
                   case 'C': // character record.
213
                       byte[] sbuffer = new byte[tempFieldLength];
214
                                           buffer.get(sbuffer);
215
                       tempObject = new String(sbuffer, "ISO-8859-1").trim();
216
                       break;
217
                   case 'D': // date data type.
218
                       byte[] dbuffer = new byte[8];
219
                                           buffer.get(dbuffer);
220
                       String tempString = new String(dbuffer, 0, 4);
221
                       try {
222
                           int tempYear = Integer.parseInt(tempString);
223
                           tempString = new String(dbuffer, 4, 2);
224
                           int tempMonth = Integer.parseInt(tempString) - 1;
225
                           tempString = new String(dbuffer, 6, 2);
226
                           int tempDay = Integer.parseInt(tempString);
227
                           Calendar c = Calendar.getInstance();
228
                           c.set(Calendar.YEAR, tempYear);
229
                           c.set(Calendar.MONTH, tempMonth);
230
                           c.set(Calendar.DAY_OF_MONTH, tempDay);
231
                           tempObject = c.getTime();
232
                       } catch (NumberFormatException e) {
233
                       }
234
                       break;
235
                   case 'M': // memo field.
236
                       byte[] mbuffer = new byte[10];
237
                                           buffer.get(mbuffer);
238
                       break;
239
                   case 'N': // number
240
                   case 'F': // floating point number
241
                       byte[] fbuffer = new byte[tempFieldLength];
242
                                           buffer.get(fbuffer);
243
                       try {
244
                           tempString = new String(fbuffer);
245
                           tempObject = Double.valueOf(tempString.trim());
246
                       } catch (NumberFormatException e) {
247
                       }
248
                       break;
249
                   default:
250
                       byte[] defbuffer = new byte[tempFieldLength];
251
                                           buffer.get(defbuffer);
252
                       System.out.println("Do not know how to parse Field type " +
253
                           tempFieldType);
254
                   }
255
                   tempRow[j] = tempObject;
256
                   //                                System.out.println(" Data="+tempObject);
257
               }
258
               // ensure that the full record has been read.
259
               if (tempRecordLength < myHeader.getRecordLength()) {
260
                   byte[] tempbuff = new byte[myHeader.getRecordLength() -
261
                       tempRecordLength];
262
                   buffer.get(tempbuff);
263
                   /* if (tempTelling){
264
                           System.out.println("DBF File has "+(myHeader.getRecordLength()-tempRecordLength)+" extra bytes per record");
265
                           tempTelling = false;
266
                   } */
267
        /*           }
268
           return tempRow;
269
           }
270
         */
271

    
272
        /**
273
         * Retrieve the name of the given column.
274
         *
275
         * @param inIndex ?ndice.
276
         *
277
         * @return nombre del campo.
278
         */
279
        public String getFieldName(int inIndex) {
280
                return myHeader.getFieldName(inIndex).trim();
281
        }
282

    
283
        /**
284
         * Retrieve the type of the given column.
285
         *
286
         * @param inIndex ?ndice.
287
         *
288
         * @return tipo de campo.
289
         */
290
        public char getFieldType(int inIndex) {
291
                return myHeader.getFieldType(inIndex);
292
        }
293

    
294
        /**
295
         * Retrieve the length of the given column.
296
         *
297
         * @param inIndex indice.
298
         *
299
         * @return longitud del field.
300
         */
301
        public int getFieldLength(int inIndex) {
302
                return myHeader.getFieldLength(inIndex);
303
        }
304

    
305
        /*
306
         * Retrieve the value of the given column as string.
307
         *
308
         * @param idField DOCUMENT ME!
309
         * @param idRecord DOCUMENT ME!
310
         *
311
         * @return DOCUMENT ME!
312
         *
313
                 public Object getFieldValue(int idField, long idRecord) throws IOException {
314
                     Object[] tmpReg = getRecord(idRecord);
315
                     return tmpReg[idField];
316
                 }
317
         */
318
        /*
319
         * DOCUMENT ME!
320
         *
321
         * @param idField DOCUMENT ME!
322
         * @param idRecord DOCUMENT ME!
323
         *
324
         * @return DOCUMENT ME!
325
         *
326
                 public double getFieldValueAsDouble(int idField, int idRecord) throws IOException {
327
                     Object[] tmpReg = getRecord(idRecord);
328
                     return (double) Double.parseDouble(tmpReg[idField].toString());
329
                 }
330
         */
331

    
332
        /**
333
         * Retrieve the location of the decimal point.
334
         *
335
         * @param inIndex ?ndice.
336
         *
337
         * @return localizaci?n.
338
         */
339
        public int getFieldDecimalLength(int inIndex) {
340
                return myHeader.getFieldDecimalCount(inIndex);
341
        }
342

    
343
        /**
344
         * read the DBF file into memory.
345
         *
346
         * @param file Fichero.
347
         *
348
         * @throws IOException
349
         */
350
        public void open(File file) throws IOException {
351
                fin = new FileInputStream(file);
352
                channel = fin.getChannel();
353

    
354
        // buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
355
        buffer = new BigByteBuffer2(channel, FileChannel.MapMode.READ_ONLY);
356

    
357
                // create the header to contain the header information.
358
                myHeader = new DbaseFileHeaderNIO();
359
                myHeader.readHeader(buffer);
360
        }
361

    
362
        /**
363
         * Removes all data from the dataset
364
         *
365
         * @throws IOException .
366
         */
367
        public void close() throws IOException {
368
                fin.close();
369
                channel.close();
370
        }
371

    
372
        /**
373
         * @return Returns the DbaseFileHeaderNIO.
374
         */
375
        public DbaseFileHeaderNIO getDBaseHeader() {
376
                return myHeader;
377
        }
378
}