1 10 package com.sqlmagic.tinysql; 11 12 import java.util.*; 13 import java.lang.*; 14 import java.io.*; 15 16 17 23 public class DBFHeader 24 { 25 String tableName = null; 26 short file_type = 0; short file_update_year = 0; 28 short file_update_month = 0; 29 short file_update_day = 0; 30 int numFields = 0; int numRecords = 0; int headerLength = 0; int recordLength = 0; 35 41 final static int BULK_SIZE = 32; final static int FLAG_INDEX = 0; final static int DATE_INDEX = 1; final static int NUMBER_OF_REC_INDEX = 4; final static int LENGTH_OF_HEADER_INDEX = 8; final static int LENGTH_OF_REC_INDEX = 10; final static int RESERVED_INDEX = 12; 49 50 55 DBFHeader(RandomAccessFile ff) throws tinySQLException 56 { 57 try { 58 ff.seek(FLAG_INDEX); 59 file_type = Utils.fixByte(ff.readByte()); 60 61 file_update_year = Utils.fixByte(ff.readByte()); 63 file_update_month = Utils.fixByte(ff.readByte()); 64 file_update_day = Utils.fixByte(ff.readByte()); 65 66 byte[] b = new byte[4]; 69 70 ff.readFully(b); 73 74 numRecords = (int)Utils.vax_to_long(b); 77 78 b = new byte[2]; 81 82 ff.readFully(b); 85 headerLength = Utils.vax_to_short(b); 86 87 ff.readFully(b); 90 recordLength = Utils.vax_to_short(b); 91 92 numFields = (int) (headerLength - 33)/32; 95 96 100 Utils.log("HEADER=" + this.toString()); 101 102 } catch (Exception e) { 103 throw new tinySQLException(e.getMessage()); 104 } 105 } 106 107 108 114 DBFHeader(int numFields, int recordLength) throws tinySQLException 115 { 116 this.numFields = numFields; 117 this.recordLength = recordLength; 118 Utils.log("DBFHeader", "numFields=" + numFields + " recordLength=" + recordLength); 119 } 120 121 122 127 public RandomAccessFile create(String dataDir, String tableName 128 ) throws tinySQLException 129 { 130 this.tableName = tableName; 131 132 try { 133 mkDataDirectory(dataDir); 136 137 dropTable(dataDir, tableName); 140 141 String fullPath = dataDir + File.separator + tableName + dbfFileTable.dbfExtension; 142 RandomAccessFile ff = new RandomAccessFile(fullPath, "rw"); 143 144 write(ff); 145 146 148 return ff; 149 150 } catch (Exception e) { 151 throw new tinySQLException(e.getMessage()); 152 } 153 } 154 155 156 159 public void write(RandomAccessFile ff) throws tinySQLException 160 { 161 try { 162 ff.seek(FLAG_INDEX); 165 ff.writeByte((byte)0x03); 166 167 setTimestamp(ff); 169 setNumRecords(ff, 0); 170 171 setHeaderLength(ff, numFields); 172 173 setRecordLength(ff, recordLength); 174 175 setReserved(ff); 176 177 } catch (Exception e) { 178 throw new tinySQLException(e.getMessage()); 179 } 180 } 181 182 183 186 void mkDataDirectory(String dataDir) throws NullPointerException 187 { 188 File dd = new File( dataDir ); 189 190 if (!dd.exists()) { 191 dd.mkdir(); 192 } 193 } 194 195 public void setTimestamp(RandomAccessFile ff) throws tinySQLException 196 { 197 try { 198 java.util.Calendar cal = java.util.Calendar.getInstance(); 199 cal.setTime(new java.util.Date ()); 200 int dd = cal.get(java.util.Calendar.DAY_OF_MONTH); 201 int mm = cal.get(java.util.Calendar.MONTH) + 1; 202 int yy = cal.get(java.util.Calendar.YEAR); 203 yy = yy % 100; ff.seek(DATE_INDEX); 205 ff.write(yy); 206 ff.write(mm); 207 ff.write(dd); 208 } catch (Exception e) { 209 throw new tinySQLException(e.getMessage()); 210 } 211 } 212 213 214 218 public void setNumRecords(RandomAccessFile ff, int numRecords) throws tinySQLException 219 { 220 this.numRecords = numRecords; 221 writeNumRecords(ff, numRecords); 222 } 223 224 225 231 public static void writeNumRecords(RandomAccessFile ff, int numRecords) throws tinySQLException 232 { 233 try { 234 byte[] b = Utils.intToLittleEndian(numRecords); 235 ff.seek(NUMBER_OF_REC_INDEX); 236 ff.write(b); 237 } catch (Exception e) { 238 throw new tinySQLException(e.getMessage()); 239 } 240 } 241 242 243 247 public void setHeaderLength(RandomAccessFile ff, int numFields) throws tinySQLException 248 { 249 this.numFields = numFields; 250 try { 251 int headerLength = (DBFHeader.BULK_SIZE+1) + numFields * DBFHeader.BULK_SIZE; 252 ff.seek(DBFHeader.LENGTH_OF_HEADER_INDEX); 253 ff.write(Utils.shortToLittleEndian((short)headerLength)); 254 } catch (Exception e) { 255 throw new tinySQLException(e.getMessage()); 256 } 257 } 258 259 260 264 public void setRecordLength(RandomAccessFile ff, int recordLength) throws tinySQLException 265 { 266 this.recordLength = recordLength; 267 try { 268 ff.seek(DBFHeader.LENGTH_OF_REC_INDEX); 269 ff.write(Utils.shortToLittleEndian((short)recordLength)); 270 } catch (Exception e) { 271 throw new tinySQLException(e.getMessage()); 272 } 273 } 274 275 276 280 public void setReserved(RandomAccessFile ff) throws tinySQLException 281 { 282 try { 283 ff.seek(DBFHeader.RESERVED_INDEX); 284 byte[] reserved = Utils.forceToSize(null, 285 DBFHeader.BULK_SIZE - DBFHeader.RESERVED_INDEX, 286 (byte)0); 287 ff.write(reserved); } catch (Exception e) { 289 throw new tinySQLException(e.getMessage()); 290 } 291 } 292 293 294 static void dropTable (String dataDir, String fname) throws tinySQLException { 295 try { 296 297 Utils.delFile(dataDir, fname + dbfFileTable.dbfExtension); 299 300 } catch (Exception e) { 301 throw new tinySQLException(e.getMessage()); 302 } 303 } 304 305 306 } 307 308 | Popular Tags |