?? recordstore.java
字號:
* for space and create it. */ int space = RecordStoreFile.spaceAvailable(); if (space - DB_INIT.length < 0) { throw new RecordStoreFullException(); } db = new RecordStore(recordStoreName, createIfNecessary); /* * Now add the new record store to the cache */ db.opencount = 1; dbCache.addElement(db); return db; } } /** * Returns an array of the names of record stores owned by the * MIDlet suite. Note that if the MIDlet suite does not * have any record stores, this function will return NULL. * * The order of RecordStore names returned is implementation * dependent. * * @return an array of the names of record stores owned by the * MIDlet suite. Note that if the MIDlet suite does not * have any record stores, this function will return NULL. */ public static String[] listRecordStores() { // static calls synchronize on dbCacheLock synchronized (dbCacheLock) { String[] returnstrings = RecordStoreFile.listRecordStores(); return returnstrings; } } /** * Deletes the named record store. MIDlet suites are only allowed to * operate on their own record stores, including deletions. If the * record store is currently open by a MIDlet when this method * is called, or if the named record store does not exist, a * RecordStoreException will be thrown. * * @param recordStoreName the MIDlet suite unique record store to delete. * * @exception RecordStoreException if a record store-related exception * occurred. * @exception RecordStoreNotFoundException if the record store could * not be found. */ public static void deleteRecordStore(String recordStoreName) throws RecordStoreException, RecordStoreNotFoundException { // Check the record store cache for a db with the same name synchronized (dbCacheLock) { RecordStore db; for (int n = 0; n < dbCache.size(); n++) { db = (RecordStore)dbCache.elementAt(n); if (db.recordStoreName.equals(recordStoreName)) { // cannot delete an open record store throw new RecordStoreException("deleteRecordStore error:" + " record store is" + " still open"); } } // this record store is not currently open if (RecordStoreFile.exists(recordStoreName)) { boolean success = RecordStoreFile.deleteFile(recordStoreName); if (!success) { throw new RecordStoreException("deleteRecordStore " + "failed"); } } else { throw new RecordStoreNotFoundException("deleteRecordStore " + "error: file " + "not found"); } } } /* * Public RecordStore update operations */ /** * Adds a new record to the record store. The recordId for this * new record is returned. This is a blocking atomic operation. * The record is written to persistent storage before the * method returns. * * @param data the data to be stored in this record. If the record * is to have zero-length data (no data), this parameter * may be null. * @param offset the index into the data buffer of the first relevant * byte for this record. * @param numBytes the number of bytes of the data buffer to use for * this record (may be zero). * * @return the recordId for the new record. * * @exception RecordStoreNotOpenException if the record store is not open. * @exception RecordStoreException if a different record store-related * exception occurred. * @exception RecordStoreFullException if the operation cannot be * completed because the record store has no more room. * @exception NullPointerException if <code>data</code> is null but * <code>numBytes</code> is greater than zero. */ public int addRecord(byte[] data, int offset, int numBytes) throws RecordStoreNotOpenException, RecordStoreException, RecordStoreFullException { synchronized (rsLock) { checkOpen(); if ((data == null) && (numBytes > 0)) { throw new NullPointerException("illegal arguments: null " + "data, numBytes > 0"); } // get recordId for new record, update db's dbNextRecordID int id = dbNextRecordID++; /* * Find the offset where this record should be stored and * seek to that location in the file. allocateNewRecordStorage() * allocates the space for this record. */ RecordHeader rh = allocateNewRecordStorage(id, numBytes); try { if (data != null) { rh.write(data, offset); } } catch (java.io.IOException ioe) { throw new RecordStoreException("error writing new record " + "data"); } // Update the state changes to the db file. dbNumLiveRecords++; dbVersion++; storeDBState(); // tell listeners a record has been added notifyRecordAddedListeners(id); // Return the new record id return id; } } /** * The record is deleted from the record store. The recordId for this * record is NOT reused. * * @param recordId the ID of the record to delete. * * @exception RecordStoreNotOpenException if the record store is not open. * @exception InvalidRecordIDException if the recordId is invalid. * @exception RecordStoreException if a general record store exception * occurs. */ public void deleteRecord(int recordId) throws RecordStoreNotOpenException, InvalidRecordIDException, RecordStoreException { synchronized (rsLock) { checkOpen(); RecordHeader rh = null; // record header try { rh = findRecord(recordId, false); freeRecord(rh); // calls rh.store recHeadCache.invalidate(rh.id); } catch (java.io.IOException ioe) { throw new RecordStoreException("error updating file after" + " record deletion"); } // update database header info and sync to file dbNumLiveRecords--; dbVersion++; storeDBState(); // tell listeners a record has been deleted notifyRecordDeletedListeners(recordId); } } /** * This method is called when the MIDlet requests to have the * record store closed. Note that the record store will not * actually be closed until closeRecordStore() is called as many * times as openRecordStore() was called. In other words, the * MIDlet needs to make a balanced number of close calls as open * calls before the record store is closed.<p> * * When the record store is closed, all listeners are removed. * If the MIDlet attempts to perform operations on the * RecordStore object after it has been closed, the methods will * throw a RecordStoreNotOpenException. * * @exception RecordStoreNotOpenException if the record store is not open. * @exception RecordStoreException if a different record store-related * exception occurred. */ public void closeRecordStore() throws RecordStoreNotOpenException, RecordStoreException { synchronized (rsLock) { synchronized (dbCacheLock) { checkOpen(); /* * Find the record store within the record store cache. * A linear search is OK assuming there won't be many * concurrently open record stores. */ RecordStore db = null; for (int n = 0; n < dbCache.size(); n++) { db = (RecordStore)dbCache.elementAt(n); if (db == this) { db.opencount--; break; } } if (db.opencount <= 0) { // free stuff - final close dbCache.removeElement(db); try { // closing now...no need to listen if (!recordListener.isEmpty()) { recordListener.removeAllElements(); } // close native fd if (dbFirstFreeBlockOffset != 0) { compactRecords(); // compact before close // truncate file to compacted size dbraf.truncate(dbDataEnd); } dbraf.close(); } catch (java.io.IOException ioe) { throw new RecordStoreException("error closing .db " + "file"); } finally { dbraf = null; recHeadCache = null; } } } } } /** * Returns the size (in bytes) of the MIDlet data available * in the given record. * * @param recordId the id of the record to use in this operation. * * @return the size (in bytes) of the MIDlet data available * in the given record. * * @exception RecordStoreNotOpenException if the record store is not open. * @exception InvalidRecordIDException if the recordId is invalid. * @exception RecordStoreException if a general record store exception * occurs. */ public int getRecordSize(int recordId) throws RecordStoreNotOpenException, InvalidRecordIDException, RecordStoreException { synchronized (rsLock) { checkOpen(); try { // throws InvalidRecordIDException RecordHeader rh = findRecord(recordId, true); return (rh.dataLenOrNextFree); } catch (java.io.IOException ioe) { throw new RecordStoreException("error reading record data"); } } } /** * Returns the data stored in the given record. * * @param recordId the id of the record to use in this operation. * @param buffer the byte array in which to copy the data. * @param offset the index into the buffer in which to start copying. * * @exception RecordStoreNotOpenException if the record store is not open. * @exception InvalidRecordIDException if the recordId is invalid. * @exception RecordStoreException if a general record store exception * occurs. * @return the number of bytes copied into the buffer, starting at * index <code>offset</code>. * * @see #setRecord */ public int getRecord(int recordId, byte[] buffer, int offset) throws RecordStoreNotOpenException, InvalidRecordIDException, RecordStoreException { synchronized (rsLock) { checkOpen(); RecordHeader rh; try { // throws InvalidRecordIDException rh = findRecord(recordId, true); rh.read(buffer, offset); } catch (java.io.IOException ioe) { throw new RecordStoreException("error reading record data"); } return rh.dataLenOrNextFree; } } /** * Returns a copy of the data stored in the given record. * * @param recordId the id of the record to use in this operation. * * @exception RecordStoreNotOpenException if the record store is not open. * @exception InvalidRecordIDException if the recordId is invalid. * @exception RecordStoreException if a general record store exception * occurs. * * @return the data stored in the given record. Note that if the record * has no data, this method will return null. * * @see #setRecord */ public byte[] getRecord(int recordId) throws RecordStoreNotOpenException, InvalidRecordIDException, RecordStoreException { synchronized (rsLock) { checkOpen(); int size = 0; byte[] data = null; try { // throws InvalidRecordIDException RecordHeader rh = findRecord(recordId, true); if (rh.dataLenOrNextFree == 0) { return null; } data = new byte[rh.dataLenOrNextFree]; rh.read(data, 0); } catch (java.io.IOException ioe) { throw new RecordStoreException("error reading record data"); } return data; } } /** * Sets the data in the given record to that passed in. After * this method returns, a call to <code>getRecord(int recordId)</code> * will return an array of numBytes size containing the data supplied here. * * @param recordId the id of the record to use in this operation. * @param newData the new data to store in the record. * @param offset the index into the data buffer of the first relevant * byte for this record. * @param numBytes the number of bytes of the data buffer to use for * this record. * * @exception RecordStoreNotOpenException if the record store is not open. * @exception InvalidRecordIDException if the recordId is invalid. * @exception RecordStoreException if a general record store exception * occurs. * @exception RecordStoreFullException if the operation cannot be * completed because the record store has no more room. * * @see #getRecord */ public void setRecord(int recordId, byte[] newData, int offset, int numBytes) throws RecordStoreNotOpenException, InvalidRecordIDException, RecordStoreException, RecordStoreFullException { synchronized (rsLock) { checkOpen(); if ((newData == null) && (numBytes > 0)) { throw new NullPointerException(); } RecordHeader rh = null;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -