Statistics
| Revision:

svn-gvsig-desktop / branches / Mobile_Compatible_Hito_1 / libFMap_mobile_shp_driver / src-file / org / gvsig / data / datastores / vectorial / file / shp_mem / DbaseFileNIO.java @ 21865

History | View | Annotate | Download (12.1 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
/************************************************
42
 *                                                                                                *
43
 *   Modfied By:                                                                *
44
 *   Prodevelop Integraci?n de Tecnolog?as SL        *
45
 *   Conde Salvatierra de ?lava , 34-10                        *
46
 *   46004 Valencia                                                                *
47
 *   Spain                                                                                *
48
 *                                                                                                *
49
 *   +34 963 510 612                                                        *
50
 *   +34 963 510 968                                                        *
51
 *   gis@prodevelop.es                                                        *
52
 *   http://www.prodevelop.es                                        *
53
 *                                                                                                *
54
 *   gvSIG Mobile Team 2006                                         *
55
 *                                                                                          *         
56
 ************************************************/
57

    
58
package org.gvsig.data.datastores.vectorial.file.shp_mem;
59

    
60
import java.io.File;
61
import java.io.FileInputStream;
62
import java.io.IOException;
63
import java.io.UnsupportedEncodingException;
64

    
65
import org.apache.log4j.Logger;
66
import org.gvsig.data.datastores.vectorial.file.FalseByteBuffer;
67
import org.gvsig.data.datastores.vectorial.file.Utils;
68

    
69

    
70
/**
71
 * Class to read and write data to a dbase III format file. Creation date:
72
 * (5/15/2001 5:15:13 PM)
73
 */
74
public class DbaseFileNIO {
75
        
76
        private static Logger logger = Logger.getLogger(DbaseFileNIO.class);
77
        // Header information for the DBase File
78
        private DbaseFileHeaderNIO myHeader;
79
        private FileInputStream fin;
80

    
81
        private FalseByteBuffer buffer;
82
        
83
        /*
84
        CharBuffer charBuffer;
85
          CharsetDecoder decoder;
86
          Charset chars;
87
        */
88

    
89
        /**
90
         * Retrieve number of records in the DbaseFile
91
         *
92
         * @return N?mero de registros.
93
         */
94
        public int getRecordCount() {
95
                return myHeader.getNumRecords();
96
        }
97

    
98
        /**
99
         * Devuelve el n?mero de fields.
100
         *
101
         * @return N?mero de fields.
102
         */
103
        public int getFieldCount() {
104
                return myHeader.getNumFields();
105
        }
106
        
107
        public int getFieldIntType(int inIndex) {
108
                return myHeader.getFieldIntType(inIndex);
109
        }
110
        
111

    
112
        /**
113
         * Devuelve el valor de un boolean a partir de su n?mero de fila y de
114
         * field.
115
         *
116
         * @param rowIndex N?mero de fila.
117
         * @param fieldId N?mero columna.
118
         *
119
         * @return boolean.
120
         */
121
        public boolean getBooleanFieldValue(int rowIndex, int fieldId) {
122
                int recordOffset = (myHeader.getRecordLength() * rowIndex) +
123
                        myHeader.getHeaderLength() + 1;
124

    
125
                //Se calcula el offset del campo
126
                int fieldOffset = 0;
127

    
128
                for (int i = 0; i < (fieldId - 1); i++) {
129
                        fieldOffset += myHeader.getFieldLength(i);
130
                }
131

    
132
                buffer.position(recordOffset + fieldOffset);
133

    
134
                char bool = (char) buffer.getByte();
135

    
136
                return ((bool == 't') || (bool == 'T') || (bool == 'Y') ||
137
                (bool == 'y'));
138
        }
139

    
140
        /**
141
         * Devuelve el String a partir del n?mero de fila y columna.
142
         *
143
         * @param rowIndex N?mero de fila.
144
         * @param fieldId N?mero de columna.
145
         *
146
         * @return String.
147
         * @throws UnsupportedEncodingException 
148
         */
149
        public String getStringFieldValue(int rowIndex, int fieldId) { // throws UnsupportedEncodingException {
150
                int recordOffset = (myHeader.getRecordLength() * rowIndex) +
151
                        myHeader.getHeaderLength() + 1;
152

    
153
                //Se calcula el offset del campo
154
                int fieldOffset = 0;
155

    
156
                for (int i = 0; i < fieldId; i++) {
157
                        fieldOffset += myHeader.getFieldLength(i);
158
                }
159

    
160
                buffer.position(recordOffset + fieldOffset);
161

    
162
                byte[] data = new byte[myHeader.getFieldLength(fieldId)];
163

    
164
//                ByteBuffer byteBuffer = ByteBuffer.wrap(data);
165
                
166
                
167
                buffer.getBytes(data);
168
                return new String(data); //, chars.name());
169
        }
170

    
171
        /**
172
         * Devuelve el Number a partir de una fila y columna.
173
         *
174
         * @param rowIndex N?mero fila.
175
         * @param fieldId N?mero columna.
176
         *
177
         * @return Number.
178
         */
179
        public Number getNumberFieldValue(int rowIndex, int fieldId) {
180
                //System.out.println("rowIndex = "+rowIndex+ " , "+"fieldId = "+fieldId);
181
                int recordOffset = (myHeader.getRecordLength() * rowIndex) +
182
                        myHeader.getHeaderLength() + 1;
183

    
184
                //Se calcula el offset del campo
185
                int fieldOffset = 0;
186

    
187
                for (int i = 0; i < fieldId; i++) {
188
                        fieldOffset += myHeader.getFieldLength(i);
189
                }
190

    
191
                buffer.position(recordOffset + fieldOffset);
192

    
193
                byte[] data = new byte[myHeader.getFieldLength(fieldId)];
194
                buffer.getBytes(data);
195

    
196
                String s = new String(data);
197
                s = s.trim();
198

    
199
                if (getFieldType(fieldId) == 'N') {
200
                        Object tempObject = Double.valueOf(s);
201

    
202
                        return new Double(tempObject.toString());
203
                } else {
204
                        Object tempObject = Integer.valueOf(s);
205

    
206
                        return new Integer(tempObject.toString());
207
                }
208

    
209
                //return 0;
210
        }
211

    
212
        // Retrieve the record at the given index
213

    
214
        /*    public Object[] getRecord(long inIndex) throws IOException {
215
           long nRecordOffset = (myHeader.getRecordLength() * inIndex) +
216
               myHeader.getHeaderLength();
217
           // retrieve the record length
218
           int tempNumFields = myHeader.getNumFields();
219
           // storage for the actual values
220
           Object[] tempRow = new Object[tempNumFields];
221
               buffer.position((int) nRecordOffset);
222
               // read the deleted flag
223
               char tempDeleted = (char) buffer.get();
224
               // read the record length
225
               int tempRecordLength = 1; // for the deleted character just read.
226
               // read the Fields
227
               for (int j = 0; j < tempNumFields; j++) {
228
                   // find the length of the field.
229
                   int tempFieldLength = myHeader.getFieldLength(j);
230
                   tempRecordLength = tempRecordLength + tempFieldLength;
231
                   // find the field type
232
                   char tempFieldType = myHeader.getFieldType(j);
233
                   //System.out.print("Reading Name="+myHeader.getFieldName(j)+" Type="+tempFieldType +" Length="+tempFieldLength);
234
                   // read the data.
235
                   Object tempObject = null;
236
                   switch (tempFieldType) {
237
                   case 'L': // logical data type, one character (T,t,F,f,Y,y,N,n)
238
                       char tempChar = (char) buffer.get();
239
                       if ((tempChar == 'T') || (tempChar == 't') ||
240
                               (tempChar == 'Y') || (tempChar == 'y')) {
241
                           tempObject = new Boolean(true);
242
                       } else {
243
                           tempObject = new Boolean(false);
244
                       }
245
                       break;
246
                   case 'C': // character record.
247
                       byte[] sbuffer = new byte[tempFieldLength];
248
                                           buffer.get(sbuffer);
249
                       tempObject = new String(sbuffer, "ISO-8859-1").trim();
250
                       break;
251
                   case 'D': // date data type.
252
                       byte[] dbuffer = new byte[8];
253
                                           buffer.get(dbuffer);
254
                       String tempString = new String(dbuffer, 0, 4);
255
                       try {
256
                           int tempYear = Integer.parseInt(tempString);
257
                           tempString = new String(dbuffer, 4, 2);
258
                           int tempMonth = Integer.parseInt(tempString) - 1;
259
                           tempString = new String(dbuffer, 6, 2);
260
                           int tempDay = Integer.parseInt(tempString);
261
                           Calendar c = Calendar.getInstance();
262
                           c.set(Calendar.YEAR, tempYear);
263
                           c.set(Calendar.MONTH, tempMonth);
264
                           c.set(Calendar.DAY_OF_MONTH, tempDay);
265
                           tempObject = c.getTime();
266
                       } catch (NumberFormatException e) {
267
                       }
268
                       break;
269
                   case 'M': // memo field.
270
                       byte[] mbuffer = new byte[10];
271
                                           buffer.get(mbuffer);
272
                       break;
273
                   case 'N': // number
274
                   case 'F': // floating point number
275
                       byte[] fbuffer = new byte[tempFieldLength];
276
                                           buffer.get(fbuffer);
277
                       try {
278
                           tempString = new String(fbuffer);
279
                           tempObject = Double.valueOf(tempString.trim());
280
                       } catch (NumberFormatException e) {
281
                       }
282
                       break;
283
                   default:
284
                       byte[] defbuffer = new byte[tempFieldLength];
285
                                           buffer.get(defbuffer);
286
                       System.out.println("Do not know how to parse Field type " +
287
                           tempFieldType);
288
                   }
289
                   tempRow[j] = tempObject;
290
                   //                                System.out.println(" Data="+tempObject);
291
               }
292
               // ensure that the full record has been read.
293
               if (tempRecordLength < myHeader.getRecordLength()) {
294
                   byte[] tempbuff = new byte[myHeader.getRecordLength() -
295
                       tempRecordLength];
296
                   buffer.get(tempbuff);
297
                   /* if (tempTelling){
298
                           System.out.println("DBF File has "+(myHeader.getRecordLength()-tempRecordLength)+" extra bytes per record");
299
                           tempTelling = false;
300
                   } */
301
        /*           }
302
           return tempRow;
303
           }
304
         */
305

    
306
        /**
307
         * Retrieve the name of the given column.
308
         *
309
         * @param inIndex ?ndice.
310
         *
311
         * @return nombre del campo.
312
         */
313
        public String getFieldName(int inIndex) {
314
                return myHeader.getFieldName(inIndex).trim();
315
        }
316

    
317
        /**
318
         * Retrieve the type of the given column.
319
         *
320
         * @param inIndex ?ndice.
321
         *
322
         * @return tipo de campo.
323
         */
324
        public char getFieldType(int inIndex) {
325
                return myHeader.getFieldType(inIndex);
326
        }
327

    
328
        /**
329
         * Retrieve the length of the given column.
330
         *
331
         * @param inIndex indice.
332
         *
333
         * @return longitud del field.
334
         */
335
        public int getFieldLength(int inIndex) {
336
                return myHeader.getFieldLength(inIndex);
337
        }
338

    
339
        /*
340
         * Retrieve the value of the given column as string.
341
         *
342
         * @param idField 
343
         * @param idRecord 
344
         *
345
         * @return 
346
         *
347
                 public Object getFieldValue(int idField, long idRecord) throws IOException {
348
                     Object[] tmpReg = getRecord(idRecord);
349
                     return tmpReg[idField];
350
                 }
351
         */
352
        /*
353
         * 
354
         *
355
         * @param idField 
356
         * @param idRecord 
357
         *
358
         * @return 
359
         *
360
                 public double getFieldValueAsDouble(int idField, int idRecord) throws IOException {
361
                     Object[] tmpReg = getRecord(idRecord);
362
                     return (double) Double.parseDouble(tmpReg[idField].toString());
363
                 }
364
         */
365

    
366
        /**
367
         * Retrieve the location of the decimal point.
368
         *
369
         * @param inIndex ?ndice.
370
         *
371
         * @return localizaci?n.
372
         */
373
        public int getFieldDecimalLength(int inIndex) {
374
                return myHeader.getFieldDecimalCount(inIndex);
375
        }
376

    
377
        /**
378
         * read the DBF file into memory.
379
         *
380
         * @param file Fichero.
381
         *
382
         * @throws IOException
383
         */
384
        public void open(File file) throws IOException {
385
                
386
                // logger.debug("open(file) de DBaseFileNIO");
387
                fin = new FileInputStream(file);
388
                // channel = fin.getChannel();
389
                // channel = FileChannelImpl.open(fin.getFD(), true, false, fin);
390

    
391
        // buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
392
        // buffer = ByteBuffer.allocate((int) file.length()); //new BigByteBuffer2(channel, FileChannel.MapMode.READ_ONLY);
393
        
394
        buffer = Utils.loadFileInputStream(fin, (int) file.length());
395
        
396
        // logger.debug("buffer = " + buffer);
397
        
398
                // create the header to contain the header information.
399
                myHeader = new DbaseFileHeaderNIO();
400
                myHeader.readHeader(buffer);
401
                
402
                /*
403
                charBuffer = CharBuffer.allocate(myHeader.getRecordLength() - 1);
404
                chars = Charset.forName("ISO-8859-1");
405
                decoder = chars.newDecoder();
406
                */
407

    
408
        }
409
        
410
        
411

    
412
        /**
413
         * Removes all data from the dataset
414
         *
415
         * @throws IOException .
416
         */
417
        public void close() throws IOException {
418
                fin.close();
419
        
420
        }
421

    
422
        /**
423
         * @return Returns the DbaseFileHeaderNIO.
424
         */
425
        public DbaseFileHeaderNIO getDBaseHeader() {
426
                return myHeader;
427
        }
428
}