svn-gvsig-desktop / tags / v1_1_Build_1013 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / drivers / shp / DbaseFileHeaderNIO.java @ 13521
History | View | Annotate | Download (24.5 KB)
1 |
/*
|
---|---|
2 |
* Geotools - OpenSource mapping toolkit
|
3 |
* (C) 2002, Centre for Computational Geography
|
4 |
*
|
5 |
* This library is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU Lesser General Public
|
7 |
* License as published by the Free Software Foundation;
|
8 |
* version 2.1 of the License.
|
9 |
*
|
10 |
* This library 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 GNU
|
13 |
* Lesser General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU Lesser General Public
|
16 |
* License along with this library; if not, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
18 |
*
|
19 |
* This file is based on an origional contained in the GISToolkit project:
|
20 |
* http://gistoolkit.sourceforge.net/
|
21 |
*
|
22 |
*/
|
23 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
24 |
*
|
25 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
26 |
*
|
27 |
* This program is free software; you can redistribute it and/or
|
28 |
* modify it under the terms of the GNU General Public License
|
29 |
* as published by the Free Software Foundation; either version 2
|
30 |
* of the License, or (at your option) any later version.
|
31 |
*
|
32 |
* This program is distributed in the hope that it will be useful,
|
33 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
34 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
35 |
* GNU General Public License for more details.
|
36 |
*
|
37 |
* You should have received a copy of the GNU General Public License
|
38 |
* along with this program; if not, write to the Free Software
|
39 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
40 |
*
|
41 |
* For more information, contact:
|
42 |
*
|
43 |
* Generalitat Valenciana
|
44 |
* Conselleria d'Infraestructures i Transport
|
45 |
* Av. Blasco Ib??ez, 50
|
46 |
* 46010 VALENCIA
|
47 |
* SPAIN
|
48 |
*
|
49 |
* +34 963862235
|
50 |
* gvsig@gva.es
|
51 |
* www.gvsig.gva.es
|
52 |
*
|
53 |
* or
|
54 |
*
|
55 |
* IVER T.I. S.A
|
56 |
* Salamanca 50
|
57 |
* 46005 Valencia
|
58 |
* Spain
|
59 |
*
|
60 |
* +34 963163400
|
61 |
* dac@iver.es
|
62 |
*/
|
63 |
package com.iver.cit.gvsig.fmap.drivers.shp; |
64 |
|
65 |
import java.io.EOFException; |
66 |
import java.io.IOException; |
67 |
import java.nio.ByteBuffer; |
68 |
import java.nio.ByteOrder; |
69 |
import java.nio.channels.FileChannel; |
70 |
import java.nio.channels.ReadableByteChannel; |
71 |
import java.sql.Types; |
72 |
import java.util.Calendar; |
73 |
import java.util.Date; |
74 |
import java.util.logging.Level; |
75 |
import java.util.logging.Logger; |
76 |
|
77 |
import com.hardcode.gdbms.engine.data.DataSource; |
78 |
import com.hardcode.gdbms.engine.data.driver.DriverException; |
79 |
import com.iver.cit.gvsig.fmap.drivers.FieldDescription; |
80 |
import com.iver.utiles.bigfile.BigByteBuffer2; |
81 |
import com.vividsolutions.jts.geom.Geometry; |
82 |
|
83 |
/**
|
84 |
* Class to represent the header of a Dbase III file. Creation date: (5/15/2001
|
85 |
* 5:15:30 PM)
|
86 |
*/
|
87 |
public class DbaseFileHeaderNIO { |
88 |
// Constant for the size of a record
|
89 |
private static final int FILE_DESCRIPTOR_SIZE = 32; |
90 |
|
91 |
// type of the file, must be 03h
|
92 |
private static final byte MAGIC = 0x03; |
93 |
|
94 |
private static final int MINIMUM_HEADER = 33; |
95 |
|
96 |
// Date the file was last updated.
|
97 |
private Date date = new Date(); |
98 |
|
99 |
private int recordCnt = 0; |
100 |
|
101 |
private int fieldCnt = 0; |
102 |
|
103 |
private int myFileType = 0; |
104 |
|
105 |
// set this to a default length of 1, which is enough for one "space"
|
106 |
// character which signifies an empty record
|
107 |
private int recordLength = 1; |
108 |
|
109 |
// set this to a flagged value so if no fields are added before the write,
|
110 |
// we know to adjust the headerLength to MINIMUM_HEADER
|
111 |
private int headerLength = -1; |
112 |
|
113 |
private int largestFieldSize = 0; |
114 |
|
115 |
private Logger logger = Logger.getLogger("org.geotools.data.shapefile"); |
116 |
|
117 |
// collection of header records.
|
118 |
// lets start out with a zero-length array, just in case
|
119 |
private DbaseField[] fields = null; // new DbaseField[0]; |
120 |
|
121 |
/**
|
122 |
* Lee del buffer.
|
123 |
*
|
124 |
* @param buffer .
|
125 |
* @param channel .
|
126 |
*
|
127 |
* @throws IOException .
|
128 |
* @throws EOFException .
|
129 |
*/
|
130 |
private void read(ByteBuffer buffer, ReadableByteChannel channel) |
131 |
throws IOException { |
132 |
while (buffer.remaining() > 0) { |
133 |
if (channel.read(buffer) == -1) { |
134 |
throw new EOFException("Premature end of file"); |
135 |
} |
136 |
} |
137 |
} |
138 |
|
139 |
/**
|
140 |
* Determine the most appropriate Java Class for representing the data in
|
141 |
* the field.
|
142 |
*
|
143 |
* <PRE>
|
144 |
*
|
145 |
* All packages are java.lang unless otherwise specified. C (Character) ->
|
146 |
* String N (Numeric) -> Integer or Double (depends on field's decimal
|
147 |
* count) F (Floating) -> Double L (Logical) -> Boolean D (Date) ->
|
148 |
* java.util.Date Unknown -> String
|
149 |
*
|
150 |
* </PRE>
|
151 |
*
|
152 |
* @param i
|
153 |
* The index of the field, from 0 to <CODE>getNumFields() - 1</CODE> .
|
154 |
*
|
155 |
* @return A Class which closely represents the dbase field type.
|
156 |
*/
|
157 |
public Class getFieldClass(int i) { |
158 |
Class typeClass = null; |
159 |
|
160 |
switch (fields[i].fieldType) {
|
161 |
case 'C': |
162 |
typeClass = String.class;
|
163 |
|
164 |
break;
|
165 |
|
166 |
case 'N': |
167 |
|
168 |
if (fields[i].decimalCount == 0) { |
169 |
typeClass = Integer.class;
|
170 |
} else {
|
171 |
typeClass = Double.class;
|
172 |
} |
173 |
|
174 |
break;
|
175 |
|
176 |
case 'F': |
177 |
typeClass = Double.class;
|
178 |
|
179 |
break;
|
180 |
|
181 |
case 'L': |
182 |
typeClass = Boolean.class;
|
183 |
|
184 |
break;
|
185 |
|
186 |
case 'D': |
187 |
typeClass = Date.class;
|
188 |
|
189 |
break;
|
190 |
|
191 |
default:
|
192 |
typeClass = String.class;
|
193 |
|
194 |
break;
|
195 |
} |
196 |
|
197 |
return typeClass;
|
198 |
} |
199 |
|
200 |
/**
|
201 |
* Add a column to this DbaseFileHeader. The type is one of (C N L or D)
|
202 |
* character, number, logical(true/false), or date. The Field length is the
|
203 |
* total length in bytes reserved for this column. The decimal count only
|
204 |
* applies to numbers(N), and floating point values (F), and refers to the
|
205 |
* number of characters to reserve after the decimal point. <B>Don't expect
|
206 |
* miracles from this...</B>
|
207 |
*
|
208 |
* <PRE>
|
209 |
*
|
210 |
* Field Type MaxLength ---------- --------- C 254 D 8 F 20 N 18
|
211 |
*
|
212 |
* </PRE>
|
213 |
*
|
214 |
* @param inFieldName
|
215 |
* The name of the new field, must be less than 10 characters or
|
216 |
* it gets truncated.
|
217 |
* @param inFieldType
|
218 |
* A character representing the dBase field, ( see above ). Case
|
219 |
* insensitive.
|
220 |
* @param inFieldLength
|
221 |
* The length of the field, in bytes ( see above )
|
222 |
* @param inDecimalCount
|
223 |
* For numeric fields, the number of decimal places to track.
|
224 |
*/
|
225 |
public void addColumn(String inFieldName, char inFieldType, |
226 |
int inFieldLength, int inDecimalCount) { |
227 |
/*
|
228 |
* if (inFieldLength <=0) { throw new DbaseFileException("field length <=
|
229 |
* 0"); }
|
230 |
*/
|
231 |
if (fields == null) { |
232 |
fields = new DbaseField[0]; |
233 |
} |
234 |
|
235 |
int tempLength = 1; // the length is used for the offset, and there is a |
236 |
// * for deleted as the first byte
|
237 |
DbaseField[] tempFieldDescriptors = new DbaseField[fields.length + 1]; |
238 |
|
239 |
for (int i = 0; i < fields.length; i++) { |
240 |
fields[i].fieldDataAddress = tempLength; |
241 |
tempLength = tempLength + fields[i].fieldLength; |
242 |
tempFieldDescriptors[i] = fields[i]; |
243 |
} |
244 |
|
245 |
tempFieldDescriptors[fields.length] = new DbaseField();
|
246 |
tempFieldDescriptors[fields.length].fieldLength = inFieldLength; |
247 |
tempFieldDescriptors[fields.length].decimalCount = inDecimalCount; |
248 |
tempFieldDescriptors[fields.length].fieldDataAddress = tempLength; |
249 |
|
250 |
// set the field name
|
251 |
String tempFieldName = inFieldName;
|
252 |
|
253 |
if (tempFieldName == null) { |
254 |
tempFieldName = "NoName";
|
255 |
} |
256 |
|
257 |
// Fix for GEOT-42, ArcExplorer will not handle field names > 10 chars
|
258 |
// Sorry folks.
|
259 |
if (tempFieldName.length() > 10) { |
260 |
tempFieldName = tempFieldName.substring(0, 10); |
261 |
warn("FieldName " + inFieldName
|
262 |
+ " is longer than 10 characters, truncating to "
|
263 |
+ tempFieldName); |
264 |
} |
265 |
|
266 |
tempFieldDescriptors[fields.length].fieldName = tempFieldName; |
267 |
|
268 |
// the field type
|
269 |
if ((inFieldType == 'C') || (inFieldType == 'c')) { |
270 |
tempFieldDescriptors[fields.length].fieldType = 'C';
|
271 |
|
272 |
if (inFieldLength > 254) { |
273 |
warn("Field Length for "
|
274 |
+ inFieldName |
275 |
+ " set to "
|
276 |
+ inFieldLength |
277 |
+ " Which is longer than 254, not consistent with dbase III");
|
278 |
} |
279 |
} else if ((inFieldType == 'S') || (inFieldType == 's')) { |
280 |
tempFieldDescriptors[fields.length].fieldType = 'C';
|
281 |
warn("Field type for "
|
282 |
+ inFieldName |
283 |
+ " set to S which is flat out wrong people!, I am setting this to C, in the hopes you meant character.");
|
284 |
|
285 |
if (inFieldLength > 254) { |
286 |
warn("Field Length for "
|
287 |
+ inFieldName |
288 |
+ " set to "
|
289 |
+ inFieldLength |
290 |
+ " Which is longer than 254, not consistent with dbase III");
|
291 |
} |
292 |
|
293 |
tempFieldDescriptors[fields.length].fieldLength = 8;
|
294 |
} else if ((inFieldType == 'D') || (inFieldType == 'd')) { |
295 |
tempFieldDescriptors[fields.length].fieldType = 'D';
|
296 |
|
297 |
if (inFieldLength != 8) { |
298 |
warn("Field Length for " + inFieldName + " set to " |
299 |
+ inFieldLength + " Setting to 8 digets YYYYMMDD");
|
300 |
} |
301 |
|
302 |
tempFieldDescriptors[fields.length].fieldLength = 8;
|
303 |
} else if ((inFieldType == 'F') || (inFieldType == 'f')) { |
304 |
tempFieldDescriptors[fields.length].fieldType = 'F';
|
305 |
|
306 |
if (inFieldLength > 20) { |
307 |
warn("Field Length for "
|
308 |
+ inFieldName |
309 |
+ " set to "
|
310 |
+ inFieldLength |
311 |
+ " Preserving length, but should be set to Max of 20 not valid for dbase IV, and UP specification, not present in dbaseIII.");
|
312 |
} |
313 |
} else if ((inFieldType == 'N') || (inFieldType == 'n')) { |
314 |
tempFieldDescriptors[fields.length].fieldType = 'N';
|
315 |
|
316 |
if (inFieldLength > 18) { |
317 |
warn("Field Length for "
|
318 |
+ inFieldName |
319 |
+ " set to "
|
320 |
+ inFieldLength |
321 |
+ " Preserving length, but should be set to Max of 18 for dbase III specification.");
|
322 |
} |
323 |
|
324 |
if (inDecimalCount < 0) { |
325 |
warn("Field Decimal Position for " + inFieldName + " set to " |
326 |
+ inDecimalCount |
327 |
+ " Setting to 0 no decimal data will be saved.");
|
328 |
tempFieldDescriptors[fields.length].decimalCount = 0;
|
329 |
} |
330 |
|
331 |
if (inDecimalCount > (inFieldLength - 1)) { |
332 |
warn("Field Decimal Position for " + inFieldName + " set to " |
333 |
+ inDecimalCount + " Setting to " + (inFieldLength - 1) |
334 |
+ " no non decimal data will be saved.");
|
335 |
tempFieldDescriptors[fields.length].decimalCount = inFieldLength - 1;
|
336 |
} |
337 |
} else if ((inFieldType == 'L') || (inFieldType == 'l')) { |
338 |
tempFieldDescriptors[fields.length].fieldType = 'L';
|
339 |
|
340 |
if (inFieldLength != 1) { |
341 |
warn("Field Length for " + inFieldName + " set to " |
342 |
+ inFieldLength |
343 |
+ " Setting to length of 1 for logical fields.");
|
344 |
} |
345 |
|
346 |
tempFieldDescriptors[fields.length].fieldLength = 1;
|
347 |
} else {
|
348 |
// throw new DbaseFileException("Undefined field type "+inFieldType
|
349 |
// + " For column "+inFieldName);
|
350 |
} |
351 |
|
352 |
// the length of a record
|
353 |
tempLength = tempLength |
354 |
+ tempFieldDescriptors[fields.length].fieldLength; |
355 |
|
356 |
// set the new fields.
|
357 |
fields = tempFieldDescriptors; |
358 |
fieldCnt = fields.length; |
359 |
headerLength = MINIMUM_HEADER + (32 * fields.length);
|
360 |
recordLength = tempLength; |
361 |
} |
362 |
|
363 |
/**
|
364 |
* Remove a column from this DbaseFileHeader.
|
365 |
*
|
366 |
* @param inFieldName
|
367 |
* The name of the field, will ignore case and trim.
|
368 |
*
|
369 |
* @return index of the removed column, -1 if no found
|
370 |
*
|
371 |
* @todo This is really ugly, don't know who wrote it, but it needs fixin...
|
372 |
*/
|
373 |
public int removeColumn(String inFieldName) { |
374 |
int retCol = -1; |
375 |
int tempLength = 1; |
376 |
DbaseField[] tempFieldDescriptors = new DbaseField[fields.length - 1]; |
377 |
|
378 |
for (int i = 0, j = 0; i < fields.length; i++) { |
379 |
if (!inFieldName.equalsIgnoreCase(fields[i].fieldName.trim())) {
|
380 |
// if this is the last field and we still haven't found the
|
381 |
// named field
|
382 |
if ((i == j) && (i == (fields.length - 1))) { |
383 |
System.err.println("Could not find a field named '" |
384 |
+ inFieldName + "' for removal");
|
385 |
|
386 |
return retCol;
|
387 |
} |
388 |
|
389 |
tempFieldDescriptors[j] = fields[i]; |
390 |
tempFieldDescriptors[j].fieldDataAddress = tempLength; |
391 |
tempLength += tempFieldDescriptors[j].fieldLength; |
392 |
|
393 |
// only increment j on non-matching fields
|
394 |
j++; |
395 |
} else {
|
396 |
retCol = i; |
397 |
} |
398 |
} |
399 |
|
400 |
// set the new fields.
|
401 |
fields = tempFieldDescriptors; |
402 |
headerLength = 33 + (32 * fields.length); |
403 |
recordLength = tempLength; |
404 |
|
405 |
return retCol;
|
406 |
} |
407 |
|
408 |
/**
|
409 |
* DOCUMENT ME!
|
410 |
*
|
411 |
* @param inWarn
|
412 |
* DOCUMENT ME!
|
413 |
*
|
414 |
* @todo addProgessListener handling
|
415 |
*/
|
416 |
private void warn(String inWarn) { |
417 |
if (logger.isLoggable(Level.WARNING)) { |
418 |
logger.warning(inWarn); |
419 |
} |
420 |
} |
421 |
|
422 |
// Retrieve the length of the field at the given index
|
423 |
|
424 |
/**
|
425 |
* Returns the field length in bytes.
|
426 |
*
|
427 |
* @param inIndex
|
428 |
* The field index.
|
429 |
*
|
430 |
* @return The length in bytes.
|
431 |
*/
|
432 |
public int getFieldLength(int inIndex) { |
433 |
return fields[inIndex].fieldLength;
|
434 |
} |
435 |
|
436 |
// Retrieve the location of the decimal point within the field.
|
437 |
|
438 |
/**
|
439 |
* Get the decimal count of this field.
|
440 |
*
|
441 |
* @param inIndex
|
442 |
* The field index.
|
443 |
*
|
444 |
* @return The decimal count.
|
445 |
*/
|
446 |
public int getFieldDecimalCount(int inIndex) { |
447 |
return fields[inIndex].decimalCount;
|
448 |
} |
449 |
|
450 |
// Retrieve the Name of the field at the given index
|
451 |
|
452 |
/**
|
453 |
* Get the field name.
|
454 |
*
|
455 |
* @param inIndex
|
456 |
* The field index.
|
457 |
*
|
458 |
* @return The name of the field.
|
459 |
*/
|
460 |
public String getFieldName(int inIndex) { |
461 |
return fields[inIndex].fieldName;
|
462 |
} |
463 |
|
464 |
// Retrieve the type of field at the given index
|
465 |
|
466 |
/**
|
467 |
* Get the character class of the field.
|
468 |
*
|
469 |
* @param inIndex
|
470 |
* The field index.
|
471 |
*
|
472 |
* @return The dbase character representing this field.
|
473 |
*/
|
474 |
public char getFieldType(int inIndex) { |
475 |
return fields[inIndex].fieldType;
|
476 |
} |
477 |
|
478 |
/**
|
479 |
* Get the date this file was last updated.
|
480 |
*
|
481 |
* @return The Date last modified.
|
482 |
*/
|
483 |
public Date getLastUpdateDate() { |
484 |
return date;
|
485 |
} |
486 |
|
487 |
/**
|
488 |
* Return the number of fields in the records.
|
489 |
*
|
490 |
* @return The number of fields in this table.
|
491 |
*/
|
492 |
public int getNumFields() { |
493 |
if (fields == null) |
494 |
return 0; |
495 |
return fields.length;
|
496 |
} |
497 |
|
498 |
/**
|
499 |
* Return the number of records in the file
|
500 |
*
|
501 |
* @return The number of records in this table.
|
502 |
*/
|
503 |
public int getNumRecords() { |
504 |
return recordCnt;
|
505 |
} |
506 |
|
507 |
/**
|
508 |
* Get the length of the records in bytes.
|
509 |
*
|
510 |
* @return The number of bytes per record.
|
511 |
*/
|
512 |
public int getRecordLength() { |
513 |
return recordLength;
|
514 |
} |
515 |
|
516 |
/**
|
517 |
* Get the length of the header
|
518 |
*
|
519 |
* @return The length of the header in bytes.
|
520 |
*/
|
521 |
public int getHeaderLength() { |
522 |
return headerLength;
|
523 |
} |
524 |
|
525 |
/**
|
526 |
* Read the header data from the DBF file.
|
527 |
*
|
528 |
* @param in
|
529 |
* DOCUMENT ME!
|
530 |
*
|
531 |
* @throws IOException
|
532 |
* DOCUMENT ME!
|
533 |
*/
|
534 |
public void readHeader(BigByteBuffer2 in) throws IOException { |
535 |
// type of file.
|
536 |
myFileType = in.get(); |
537 |
|
538 |
if (myFileType != 0x03) { |
539 |
throw new IOException("Unsupported DBF file Type " |
540 |
+ Integer.toHexString(myFileType));
|
541 |
} |
542 |
|
543 |
// parse the update date information.
|
544 |
int tempUpdateYear = (int) in.get(); |
545 |
int tempUpdateMonth = (int) in.get(); |
546 |
int tempUpdateDay = (int) in.get(); |
547 |
tempUpdateYear = tempUpdateYear + 1900;
|
548 |
|
549 |
Calendar c = Calendar.getInstance(); |
550 |
c.set(Calendar.YEAR, tempUpdateYear);
|
551 |
c.set(Calendar.MONTH, tempUpdateMonth - 1); |
552 |
c.set(Calendar.DATE, tempUpdateDay);
|
553 |
date = c.getTime(); |
554 |
|
555 |
// read the number of records.
|
556 |
in.order(ByteOrder.LITTLE_ENDIAN);
|
557 |
recordCnt = in.getInt(); |
558 |
|
559 |
// read the length of the header structure.
|
560 |
headerLength = in.getShort(); |
561 |
|
562 |
// read the length of a record
|
563 |
recordLength = in.getShort(); |
564 |
|
565 |
in.order(ByteOrder.BIG_ENDIAN);
|
566 |
|
567 |
// skip the reserved bytes in the header.
|
568 |
in.position(in.position() + 20);
|
569 |
|
570 |
// calculate the number of Fields in the header
|
571 |
fieldCnt = (headerLength - FILE_DESCRIPTOR_SIZE - 1)
|
572 |
/ FILE_DESCRIPTOR_SIZE; |
573 |
|
574 |
// read all of the header records
|
575 |
fields = new DbaseField[fieldCnt];
|
576 |
|
577 |
for (int i = 0; i < fieldCnt; i++) { |
578 |
fields[i] = new DbaseField();
|
579 |
|
580 |
// read the field name
|
581 |
byte[] buffer = new byte[11]; |
582 |
in.get(buffer); |
583 |
fields[i].fieldName = new String(buffer); |
584 |
|
585 |
// read the field type
|
586 |
fields[i].fieldType = (char) in.get();
|
587 |
|
588 |
// read the field data address, offset from the start of the record.
|
589 |
fields[i].fieldDataAddress = in.getInt(); |
590 |
|
591 |
// read the field length in bytes
|
592 |
int tempLength = (int) in.get(); |
593 |
|
594 |
if (tempLength < 0) { |
595 |
tempLength = tempLength + 256;
|
596 |
} |
597 |
|
598 |
fields[i].fieldLength = tempLength; |
599 |
|
600 |
// read the field decimal count in bytes
|
601 |
fields[i].decimalCount = (int) in.get();
|
602 |
|
603 |
// read the reserved bytes.
|
604 |
in.position(in.position() + 14);
|
605 |
} |
606 |
|
607 |
// Last byte is a marker for the end of the field definitions.
|
608 |
in.get(); |
609 |
} |
610 |
|
611 |
/**
|
612 |
* Get the largest field size of this table.
|
613 |
*
|
614 |
* @return The largt field size iiin bytes.
|
615 |
*/
|
616 |
public int getLargestFieldSize() { |
617 |
return largestFieldSize;
|
618 |
} |
619 |
|
620 |
/**
|
621 |
* Set the number of records in the file
|
622 |
*
|
623 |
* @param inNumRecords
|
624 |
* The number of records.
|
625 |
*/
|
626 |
public void setNumRecords(int inNumRecords) { |
627 |
recordCnt = inNumRecords; |
628 |
} |
629 |
|
630 |
/**
|
631 |
* Write the header data to the DBF file.
|
632 |
*
|
633 |
* @param out
|
634 |
* A channel to write to. If you have an OutputStream you can
|
635 |
* obtain the correct channel by using
|
636 |
* java.nio.Channels.newChannel(OutputStream out).
|
637 |
*
|
638 |
* @throws IOException
|
639 |
* If errors occur.
|
640 |
*/
|
641 |
public void writeHeader(FileChannel out) throws IOException { |
642 |
// take care of the annoying case where no records have been added...
|
643 |
if (headerLength == -1) { |
644 |
headerLength = MINIMUM_HEADER; |
645 |
} |
646 |
|
647 |
// Desde el principio
|
648 |
out.position(0);
|
649 |
|
650 |
ByteBuffer buffer = ByteBuffer.allocateDirect(headerLength); |
651 |
buffer.order(ByteOrder.LITTLE_ENDIAN);
|
652 |
|
653 |
// write the output file type.
|
654 |
buffer.put((byte) MAGIC);
|
655 |
|
656 |
// write the date stuff
|
657 |
Calendar c = Calendar.getInstance(); |
658 |
c.setTime(new Date()); |
659 |
buffer.put((byte) (c.get(Calendar.YEAR) % 100)); |
660 |
buffer.put((byte) (c.get(Calendar.MONTH) + 1)); |
661 |
buffer.put((byte) (c.get(Calendar.DAY_OF_MONTH))); |
662 |
|
663 |
// write the number of records in the datafile.
|
664 |
buffer.putInt(recordCnt); |
665 |
|
666 |
// write the length of the header structure.
|
667 |
buffer.putShort((short) headerLength);
|
668 |
|
669 |
// write the length of a record
|
670 |
buffer.putShort((short) recordLength);
|
671 |
|
672 |
// // write the reserved bytes in the header
|
673 |
// for (int i=0; i<20; i++) out.writeByteLE(0);
|
674 |
buffer.position(buffer.position() + 20);
|
675 |
|
676 |
// write all of the header records
|
677 |
int tempOffset = 0; |
678 |
|
679 |
if (fields != null) { |
680 |
for (int i = 0; i < fields.length; i++) { |
681 |
// write the field name
|
682 |
for (int j = 0; j < 11; j++) { |
683 |
if (fields[i].fieldName.length() > j) {
|
684 |
buffer.put((byte) fields[i].fieldName.charAt(j));
|
685 |
} else {
|
686 |
buffer.put((byte) 0); |
687 |
} |
688 |
} |
689 |
|
690 |
// write the field type
|
691 |
buffer.put((byte) fields[i].fieldType);
|
692 |
|
693 |
// // write the field data address, offset from the start of the
|
694 |
// record.
|
695 |
buffer.putInt(tempOffset); |
696 |
tempOffset += fields[i].fieldLength; |
697 |
|
698 |
// write the length of the field.
|
699 |
buffer.put((byte) fields[i].fieldLength);
|
700 |
|
701 |
// write the decimal count.
|
702 |
buffer.put((byte) fields[i].decimalCount);
|
703 |
|
704 |
// write the reserved bytes.
|
705 |
// for (in j=0; jj<14; j++) out.writeByteLE(0);
|
706 |
buffer.position(buffer.position() + 14);
|
707 |
} |
708 |
} |
709 |
// write the end of the field definitions marker
|
710 |
buffer.put((byte) 0x0D); |
711 |
|
712 |
buffer.position(0);
|
713 |
|
714 |
int r = buffer.remaining();
|
715 |
|
716 |
while ((r -= out.write(buffer)) > 0) { |
717 |
; // do nothing
|
718 |
} |
719 |
} |
720 |
|
721 |
/**
|
722 |
* Get a simple representation of this header.
|
723 |
*
|
724 |
* @return A String representing the state of the header.
|
725 |
*/
|
726 |
public String toString() { |
727 |
StringBuffer fs = new StringBuffer(); |
728 |
|
729 |
for (int i = 0, ii = fields.length; i < ii; i++) { |
730 |
DbaseField f = fields[i]; |
731 |
fs.append(f.fieldName + " " + f.fieldType + " " + f.fieldLength |
732 |
+ " " + f.decimalCount + " " + f.fieldDataAddress + "\n"); |
733 |
} |
734 |
|
735 |
return "DB3 Header\n" + "Date : " + date + "\n" + "Records : " |
736 |
+ recordCnt + "\n" + "Fields : " + fieldCnt + "\n" + fs; |
737 |
} |
738 |
|
739 |
/**
|
740 |
* Crea un DbaseFile.
|
741 |
*
|
742 |
* @return DbaseFileHeaderNIO
|
743 |
*
|
744 |
* @throws IOException .
|
745 |
*/
|
746 |
public static DbaseFileHeaderNIO createNewDbaseHeader() throws IOException { |
747 |
DbaseFileHeaderNIO header = new DbaseFileHeaderNIO();
|
748 |
|
749 |
for (int i = 0, ii = 1; i < ii; i++) { |
750 |
// AttributeType type = featureType.getAttributeType(i);
|
751 |
Class colType = Integer.class; |
752 |
String colName = "ID"; |
753 |
int fieldLen = 10; |
754 |
|
755 |
if (fieldLen <= 0) { |
756 |
fieldLen = 255;
|
757 |
} |
758 |
|
759 |
// @todo respect field length
|
760 |
if ((colType == Integer.class) || (colType == Short.class) |
761 |
|| (colType == Byte.class)) {
|
762 |
header.addColumn(colName, 'N', Math.min(fieldLen, 10), 0); |
763 |
} else if (colType == Long.class) { |
764 |
header.addColumn(colName, 'N', Math.min(fieldLen, 19), 0); |
765 |
} else if ((colType == Double.class) || (colType == Float.class) |
766 |
|| (colType == Number.class)) {
|
767 |
int l = Math.min(fieldLen, 33); |
768 |
int d = Math.max(l - 2, 0); |
769 |
header.addColumn(colName, 'N', l, d);
|
770 |
} else if (java.util.Date.class.isAssignableFrom(colType)) { |
771 |
header.addColumn(colName, 'D', fieldLen, 0); |
772 |
} else if (colType == Boolean.class) { |
773 |
header.addColumn(colName, 'L', 1, 0); |
774 |
} else if (CharSequence.class.isAssignableFrom(colType)) { |
775 |
// Possible fix for GEOT-42 : ArcExplorer doesn't like 0 length
|
776 |
// ensure that maxLength is at least 1
|
777 |
header.addColumn(colName, 'C', Math.min(254, fieldLen), 0); |
778 |
} else if (Geometry.class.isAssignableFrom(colType)) { |
779 |
continue;
|
780 |
} else {
|
781 |
throw new IOException("Unable to write : " + colType.getName()); |
782 |
} |
783 |
} |
784 |
|
785 |
return header;
|
786 |
} |
787 |
|
788 |
public static DbaseFileHeaderNIO createDbaseHeader(DataSource ds) |
789 |
throws IOException { |
790 |
try {
|
791 |
int[] fieldTypes = new int[ds.getFieldCount()]; |
792 |
int[] fieldLength = new int[fieldTypes.length]; |
793 |
for (int i = 0; i < fieldTypes.length; i++) { |
794 |
fieldTypes[i] = ds.getFieldType(i); |
795 |
fieldLength[i] = ds.getFieldWidth(i); |
796 |
} |
797 |
|
798 |
return createDbaseHeader(ds.getFieldNames(), fieldTypes,
|
799 |
fieldLength); |
800 |
} catch (DriverException e) {
|
801 |
// TODO Auto-generated catch block
|
802 |
e.printStackTrace(); |
803 |
return null; |
804 |
} |
805 |
} |
806 |
|
807 |
/**
|
808 |
* DOCUMENT ME!
|
809 |
*
|
810 |
* @param sds
|
811 |
* DOCUMENT ME!
|
812 |
*
|
813 |
* @return DOCUMENT ME!
|
814 |
*
|
815 |
* @throws IOException
|
816 |
* DOCUMENT ME!
|
817 |
*/
|
818 |
public static DbaseFileHeaderNIO createDbaseHeader(String[] fieldNames, |
819 |
int[] fieldTypes, int[] fieldLength) throws IOException { |
820 |
DbaseFileHeaderNIO header = new DbaseFileHeaderNIO();
|
821 |
|
822 |
for (int i = 0, ii = fieldNames.length; i < ii; i++) { |
823 |
|
824 |
int type = fieldTypes[i];
|
825 |
String colName = fieldNames[i];
|
826 |
|
827 |
// /int fieldLen = ((DBFDriver)sds.getDriver()).getFieldLength(i);
|
828 |
int fieldLen = fieldLength[i]; // TODO aqu? el tama?o no es |
829 |
// correcto hay que calcularlo,
|
830 |
// ahora mismo est? puesto a pi??n.
|
831 |
int decimales = 5; |
832 |
|
833 |
// if (fieldLen <= 0) {
|
834 |
// fieldLen = 255;
|
835 |
// }
|
836 |
// TODO [AZABALA] HE INTENTADO CREAR UN TIPO Types.BIGINT y
|
837 |
// ha petado (por eso lo a?ado)
|
838 |
if ((type == Types.DOUBLE) || (type == Types.FLOAT) |
839 |
|| (type == Types.INTEGER) || (type == Types.BIGINT)) |
840 |
|
841 |
header.addColumn(colName, 'N', Math.min(fieldLen, 18), |
842 |
decimales); |
843 |
if (type == Types.DATE) |
844 |
header.addColumn(colName, 'D', fieldLen, 0); |
845 |
if ((type == Types.BIT) || (type == Types.BOOLEAN)) |
846 |
header.addColumn(colName, 'L', 1, 0); |
847 |
if ((type == Types.VARCHAR) || (type == Types.CHAR) |
848 |
|| (type == Types.LONGVARCHAR))
|
849 |
header.addColumn(colName, 'C', Math.min(254, fieldLen), 0); |
850 |
} |
851 |
|
852 |
return header;
|
853 |
} |
854 |
|
855 |
/**
|
856 |
* Class for holding the information assicated with a record.
|
857 |
*/
|
858 |
class DbaseField { |
859 |
// Field Name
|
860 |
String fieldName;
|
861 |
|
862 |
// Field Type (C N L D or M)
|
863 |
char fieldType;
|
864 |
|
865 |
// Field Data Address offset from the start of the record.
|
866 |
int fieldDataAddress;
|
867 |
|
868 |
// Length of the data in bytes
|
869 |
int fieldLength;
|
870 |
|
871 |
// Field decimal count in Binary, indicating where the decimal is
|
872 |
int decimalCount;
|
873 |
} |
874 |
|
875 |
public static DbaseFileHeaderNIO createDbaseHeader( |
876 |
FieldDescription[] fieldsDesc) {
|
877 |
DbaseFileHeaderNIO header = new DbaseFileHeaderNIO();
|
878 |
|
879 |
for (int i = 0, ii = fieldsDesc.length; i < ii; i++) { |
880 |
|
881 |
int type = fieldsDesc[i].getFieldType();
|
882 |
String colName = fieldsDesc[i].getFieldName();
|
883 |
|
884 |
int fieldLen = fieldsDesc[i].getFieldLength(); // TODO aqu? el |
885 |
// tama?o no es
|
886 |
// correcto hay que
|
887 |
// calcularlo, ahora
|
888 |
// mismo est? puesto
|
889 |
// a pi??n.
|
890 |
int decimales = fieldsDesc[i].getFieldDecimalCount();
|
891 |
|
892 |
switch (type) {
|
893 |
case Types.DOUBLE: |
894 |
case Types.FLOAT: |
895 |
case Types.INTEGER: |
896 |
case Types.BIGINT: |
897 |
case Types.SMALLINT: |
898 |
header.addColumn(colName, 'N', Math.min(fieldLen, 18), |
899 |
decimales); |
900 |
break;
|
901 |
case Types.DATE: |
902 |
header.addColumn(colName, 'D', fieldLen, 0); |
903 |
break;
|
904 |
case Types.BIT: |
905 |
case Types.BOOLEAN: |
906 |
header.addColumn(colName, 'L', 1, 0); |
907 |
break;
|
908 |
case Types.VARCHAR: |
909 |
case Types.CHAR: |
910 |
case Types.LONGVARCHAR: |
911 |
header.addColumn(colName, 'C', Math.min(254, fieldLen), 0); |
912 |
break;
|
913 |
default:
|
914 |
throw new RuntimeException("Field type " + type + " not supported in DBF writer"); |
915 |
|
916 |
} |
917 |
} // for
|
918 |
|
919 |
return header;
|
920 |
|
921 |
} |
922 |
|
923 |
public void setFieldName(int j, String newName) { |
924 |
fields[j].fieldName = newName; |
925 |
} |
926 |
} |