1 2 20 21 package javax.microedition.rms; 22 23 import java.util.Enumeration; 24 import java.util.Hashtable; 25 import java.util.Vector; 26 27 28 public class RecordStore 29 { 30 33 private static Hashtable recordStores = new Hashtable(); 34 35 private String name; 36 private boolean open = false; 37 private int version = 0; 38 private long lastModified = 0; 39 private int nextRecordID = 1; 40 41 44 private Vector recordListeners = new Vector(); 45 private Hashtable records = new Hashtable(); 46 47 48 private RecordStore(String name) 49 { 50 this.name = name; 51 } 52 53 54 public static void deleteRecordStore(String recordStoreName) 55 throws RecordStoreException, RecordStoreNotFoundException 56 { 57 RecordStore recordStore = (RecordStore) recordStores.get(recordStoreName); 58 if (recordStore == null) { 59 throw new RecordStoreNotFoundException(); 60 } 61 if (recordStore.open) { 62 throw new RecordStoreException(); 63 } 64 65 recordStores.remove(recordStoreName); 66 } 67 68 69 public static RecordStore openRecordStore(String recordStoreName, boolean createIfNecessary) 70 throws RecordStoreException, RecordStoreFullException, RecordStoreNotFoundException 71 { 72 RecordStore recordStore = (RecordStore) recordStores.get(recordStoreName); 73 if (recordStore == null) { 74 if (!createIfNecessary) { 75 throw new RecordStoreNotFoundException(); 76 } 77 recordStore = new RecordStore(recordStoreName); 78 recordStores.put(recordStoreName, recordStore); 79 } 80 recordStore.open = true; 81 82 return recordStore; 83 } 84 85 86 public void closeRecordStore() 87 throws RecordStoreNotOpenException, RecordStoreException 88 { 89 if (!open) { 90 throw new RecordStoreNotOpenException(); 91 } 92 93 recordListeners.removeAllElements(); 94 open = false; 95 } 96 97 98 public static String[] listRecordStores() 99 { 100 String[] result = new String[recordStores.size()]; 101 102 int i = 0; 103 for (Enumeration e = recordStores.keys(); e.hasMoreElements(); ) { 104 result[i] = (String) e.nextElement(); 105 i++; 106 } 107 108 return result; 109 } 110 111 112 public String getName() 113 { 114 return name; 115 } 116 117 118 public int getVersion() 119 throws RecordStoreNotOpenException 120 { 121 if (!open) { 122 throw new RecordStoreNotOpenException(); 123 } 124 125 synchronized (this) { 126 return version; 127 } 128 } 129 130 131 public int getNumRecords() 132 throws RecordStoreNotOpenException 133 { 134 if (!open) { 135 throw new RecordStoreNotOpenException(); 136 } 137 138 synchronized (this) { 139 return records.size(); 140 } 141 } 142 143 144 public int getSizeAvailable() 145 { 146 return (int) Runtime.getRuntime().freeMemory(); 147 } 148 149 150 public long getLastModified() 151 throws RecordStoreNotOpenException 152 { 153 if (!open) { 154 throw new RecordStoreNotOpenException(); 155 } 156 157 synchronized (this) { 158 return lastModified; 159 } 160 } 161 162 163 public void addRecordListener(RecordListener listener) 164 { 165 if (!recordListeners.contains(listener)) { 166 recordListeners.addElement(listener); 167 } 168 } 169 170 171 public void removeRecordListener(RecordListener listener) 172 { 173 recordListeners.removeElement(listener); 174 } 175 176 177 public int getNextRecordID() 178 throws RecordStoreNotOpenException, RecordStoreException 179 { 180 if (!open) { 181 throw new RecordStoreNotOpenException(); 182 } 183 184 synchronized (this) { 185 return nextRecordID; 186 } 187 } 188 189 190 public int addRecord(byte[] data, int offset, int numBytes) 191 throws RecordStoreNotOpenException, RecordStoreException, RecordStoreFullException 192 { 193 if (!open) { 194 throw new RecordStoreNotOpenException(); 195 } 196 if (data == null && numBytes > 0) { 197 throw new NullPointerException(); 198 } 199 200 byte[] recordData = new byte[numBytes]; 201 if (data != null) { 202 System.arraycopy(data, offset, recordData, 0, numBytes); 203 } 204 205 int curRecordID; 206 synchronized (this) { 207 records.put(new Integer(nextRecordID), recordData); 208 version++; 209 curRecordID = nextRecordID; 210 nextRecordID++; 211 lastModified = System.currentTimeMillis(); 212 } 213 214 fireAddedRecordListener(curRecordID); 215 216 return curRecordID; 217 } 218 219 220 public void deleteRecord(int recordId) 221 throws RecordStoreNotOpenException, InvalidRecordIDException, RecordStoreException 222 { 223 if (!open) { 224 throw new RecordStoreNotOpenException(); 225 } 226 227 synchronized (this) { 228 if (records.remove(new Integer(recordId)) == null) { 229 throw new InvalidRecordIDException(); 230 } 231 version++; 232 lastModified = System.currentTimeMillis(); 233 } 234 235 fireDeletedRecordListener(recordId); 236 } 237 238 239 public int getRecordSize(int recordId) 240 throws RecordStoreNotOpenException, InvalidRecordIDException, RecordStoreException 241 { 242 if (!open) { 243 throw new RecordStoreNotOpenException(); 244 } 245 246 synchronized (this) { 247 byte[] data = (byte[]) records.get(new Integer(recordId)); 248 if (data == null) { 249 throw new InvalidRecordIDException(); 250 } 251 252 return data.length; 253 } 254 } 255 256 257 public int getRecord(int recordId, byte[] buffer, int offset) 258 throws RecordStoreNotOpenException, InvalidRecordIDException, RecordStoreException 259 { 260 int recordSize; 261 synchronized (this) { 262 recordSize = getRecordSize(recordId); 263 System.arraycopy(records.get(new Integer(recordId)), 0, buffer, offset, recordSize); 264 } 265 266 return recordSize; 267 } 268 269 270 public byte[] getRecord(int recordId) 271 throws RecordStoreNotOpenException, InvalidRecordIDException, RecordStoreException 272 { 273 byte[] data; 274 275 synchronized (this) { 276 data = new byte[getRecordSize(recordId)]; 277 getRecord(recordId, data, 0); 278 } 279 280 return data; 281 } 282 283 284 public void setRecord(int recordId, byte[] newData, int offset, int numBytes) 285 throws RecordStoreNotOpenException, InvalidRecordIDException, RecordStoreException, RecordStoreFullException 286 { 287 if (!open) { 288 throw new RecordStoreNotOpenException(); 289 } 290 291 byte[] recordData = new byte[numBytes]; 292 System.arraycopy(newData, offset, recordData, 0, numBytes); 293 294 synchronized (this) { 295 Integer id = new Integer(recordId); 296 if (records.remove(id) == null) { 297 throw new InvalidRecordIDException(); 298 } 299 records.put(id, recordData); 300 version++; 301 lastModified = System.currentTimeMillis(); 302 } 303 304 fireChangedRecordListener(recordId); 305 } 306 307 308 309 public RecordEnumeration enumerateRecords(RecordFilter filter, RecordComparator comparator, boolean keepUpdated) 310 throws RecordStoreNotOpenException 311 { 312 if (!open) { 313 throw new RecordStoreNotOpenException(); 314 } 315 316 return new RecordEnumerationImpl(filter, comparator, keepUpdated); 317 } 318 319 320 private void fireAddedRecordListener(int recordId) 321 { 322 for (Enumeration e = recordListeners.elements(); e.hasMoreElements(); ) { 323 ((RecordListener) e.nextElement()).recordAdded(this, recordId); 324 } 325 } 326 327 328 private void fireChangedRecordListener(int recordId) 329 { 330 for (Enumeration e = recordListeners.elements(); e.hasMoreElements(); ) { 331 ((RecordListener) e.nextElement()).recordChanged(this, recordId); 332 } 333 } 334 335 336 private void fireDeletedRecordListener(int recordId) 337 { 338 for (Enumeration e = recordListeners.elements(); e.hasMoreElements(); ) { 339 ((RecordListener) e.nextElement()).recordDeleted(this, recordId); 340 } 341 } 342 343 344 class RecordEnumerationImpl implements RecordEnumeration 345 { 346 347 RecordFilter filter; 348 RecordComparator comparator; 349 boolean keepUpdated; 350 351 354 Vector enumerationRecords = new Vector(); 355 int currentRecord; 356 357 358 RecordListener recordListener = new RecordListener() 359 { 360 361 public void recordAdded(RecordStore recordStore, int recordId) 362 { 363 rebuild(); 364 } 365 366 367 public void recordChanged(RecordStore recordStore, int recordId) 368 { 369 rebuild(); 370 } 371 372 373 public void recordDeleted(RecordStore recordStore, int recordId) 374 { 375 rebuild(); 376 } 377 378 }; 379 380 381 RecordEnumerationImpl(RecordFilter filter, RecordComparator comparator, boolean keepUpdated) 382 { 383 this.filter = filter; 384 this.comparator = comparator; 385 this.keepUpdated = keepUpdated; 386 387 rebuild(); 388 389 if (keepUpdated) { 390 addRecordListener(recordListener); 391 } 392 } 393 394 395 public int numRecords() 396 { 397 return enumerationRecords.size(); 398 } 399 400 401 public byte[] nextRecord() 402 throws InvalidRecordIDException, RecordStoreNotOpenException, RecordStoreException 403 { 404 if (!open) { 405 throw new RecordStoreNotOpenException(); 406 } 407 408 if (currentRecord >= numRecords()) { 409 throw new InvalidRecordIDException(); 410 } 411 412 byte[] result = ((EnumerationRecord) enumerationRecords.elementAt(currentRecord)).value; 413 currentRecord++; 414 415 return result; 416 } 417 418 419 public int nextRecordId() 420 throws InvalidRecordIDException 421 { 422 if (currentRecord >= numRecords()) { 423 throw new InvalidRecordIDException(); 424 } 425 426 int result = ((EnumerationRecord) enumerationRecords.elementAt(currentRecord)).recordId; 427 currentRecord++; 428 429 return result; 430 } 431 432 433 434 public byte[] previousRecord() 435 throws InvalidRecordIDException, RecordStoreNotOpenException, RecordStoreException 436 { 437 if (!open) { 438 throw new RecordStoreNotOpenException(); 439 } 440 if (currentRecord < 0) { 441 throw new InvalidRecordIDException(); 442 } 443 444 byte[] result = ((EnumerationRecord) enumerationRecords.elementAt(currentRecord)).value; 445 currentRecord--; 446 447 return result; 448 } 449 450 451 public int previousRecordId() 452 throws InvalidRecordIDException 453 { 454 if (currentRecord < 0) { 455 throw new InvalidRecordIDException(); 456 } 457 458 int result = ((EnumerationRecord) enumerationRecords.elementAt(currentRecord)).recordId; 459 currentRecord--; 460 461 return result; 462 } 463 464 465 public boolean hasNextElement() 466 { 467 if (currentRecord == numRecords()) { 468 return false; 469 } else { 470 return true; 471 } 472 } 473 474 475 public boolean hasPreviousElement() 476 { 477 if (currentRecord == 0) { 478 return false; 479 } else { 480 return true; 481 } 482 } 483 484 485 public void reset() 486 { 487 currentRecord = 0; 488 } 489 490 491 public void rebuild() 492 { 493 enumerationRecords.removeAllElements(); 494 495 int position; 496 for (Enumeration e = records.keys(); e.hasMoreElements(); ) { 497 Object key = e.nextElement(); 498 if (filter != null && !filter.matches((byte[]) records.get(key))) { 499 continue; 500 } 501 byte[] tmp_data = (byte[]) records.get(key); 502 if (comparator != null) { 504 for (position = 0; position < enumerationRecords.size(); position++) { 505 if (comparator.compare(tmp_data, (byte[]) enumerationRecords.elementAt(position)) 506 == RecordComparator.FOLLOWS) { 507 break; 508 } 509 } 510 } else { 511 position = enumerationRecords.size(); 512 } 513 enumerationRecords.insertElementAt( 514 new EnumerationRecord(((Integer) key).intValue(), tmp_data), position); 515 } 516 currentRecord = 0; 517 } 518 519 520 public void keepUpdated(boolean keepUpdated) 521 { 522 if (keepUpdated) { 523 if (!this.keepUpdated) { 524 rebuild(); 525 addRecordListener(recordListener); 526 } 527 } else { 528 removeRecordListener(recordListener); 529 } 530 531 this.keepUpdated = keepUpdated; 532 } 533 534 535 public boolean isKeptUpdated() 536 { 537 return keepUpdated; 538 } 539 540 541 public void destroy() 542 { 543 } 544 545 546 class EnumerationRecord 547 { 548 549 int recordId; 550 byte[] value; 551 552 553 EnumerationRecord(int recordId, byte[] value) 554 { 555 this.recordId = recordId; 556 this.value = value; 557 } 558 559 } 560 561 } 562 563 } 564 565 | Popular Tags |