01: package org.geotools.dbffile;
02:
03: import java.io.IOException;
04:
05: import com.vividsolutions.jump.io.EndianDataInputStream;
06:
07: /**
08: * class to hold infomation about the fields in the file
09: */
10: public class DbfFieldDef implements DbfConsts {
11: static final boolean DEBUG = false;
12: public StringBuffer fieldname = new StringBuffer(DBF_NAMELEN);
13: public char fieldtype;
14: public int fieldstart;
15: public int fieldlen;
16: public int fieldnumdec;
17:
18: public DbfFieldDef() { /* do nothing*/
19: }
20:
21: public DbfFieldDef(String fieldname, char fieldtype, int fieldlen,
22: int fieldnumdec) {
23: this .fieldname = new StringBuffer(fieldname);
24: this .fieldname.setLength(DBF_NAMELEN);
25: this .fieldtype = fieldtype;
26: this .fieldlen = fieldlen;
27: this .fieldnumdec = fieldnumdec;
28: }
29:
30: public String toString() {
31: return new String("" + fieldname + " " + fieldtype + " "
32: + fieldlen + "." + fieldnumdec);
33: }
34:
35: public void setup(int pos, EndianDataInputStream dFile)
36: throws IOException {
37:
38: //two byte character modification thanks to Hisaji ONO
39: byte[] strbuf = new byte[DBF_NAMELEN]; // <---- byte array buffer for storing string's byte data
40: int j = -1;
41: int term = -1;
42: for (int i = 0; i < DBF_NAMELEN; i++) {
43: byte b = dFile.readByteLE();
44: if (b == 0) {
45: if (term == -1)
46: term = j;
47: continue;
48: }
49: j++;
50: strbuf[j] = b; // <---- read string's byte data
51: }
52: if (term == -1)
53: term = j;
54: String name = new String(strbuf, 0, term + 1);
55:
56: fieldname.append(name.trim()); // <- append byte array to String Buffer
57:
58: if (DEBUG)
59: System.out.println("Fieldname " + fieldname);
60: fieldtype = (char) dFile.readUnsignedByteLE();
61: fieldstart = pos;
62: dFile.skipBytes(4);
63: switch (fieldtype) {
64: case 'C':
65: case 'c':
66: case 'D':
67: case 'L':
68: case 'M':
69: case 'G':
70: fieldlen = (int) dFile.readUnsignedByteLE();
71: fieldnumdec = (int) dFile.readUnsignedByteLE();
72: fieldnumdec = 0;
73: break;
74: case 'N':
75: case 'n':
76: case 'F':
77: case 'f':
78: fieldlen = (int) dFile.readUnsignedByteLE();
79: fieldnumdec = (int) dFile.readUnsignedByteLE();
80: break;
81: default:
82: System.out.println("Help - wrong field type: " + fieldtype);
83: }
84: if (DEBUG)
85: System.out.println("Fieldtype " + fieldtype + " width "
86: + fieldlen + "." + fieldnumdec);
87:
88: dFile.skipBytes(14);
89:
90: }
91: }
|