recordstoreimpl.java

来自「This is a resource based on j2me embedde」· Java 代码 · 共 1,191 行 · 第 1/4 页

JAVA
1,191
字号
     * anything has been modified.     *     * The initial version number is implementation dependent.     * The increment is a positive integer greater than 0.     * The version number increases only when the RecordStore is updated.     *     * The increment value need not be constant and may vary with each     * update.     *     * @return the current record store version     */    public int getVersion() throws RecordStoreNotOpenException {        return RecordStoreUtil.getInt(dbHeader, RS4_VERSION);    }    /**     * Returns the number of records currently in the record store.     *     * @return the number of records currently in the record store     */    public int getNumRecords() {        return RecordStoreUtil.getInt(dbHeader, RS3_NUM_LIVE);    }    /**     * Returns the amount of space, in bytes, that the record store     * occupies. The size returned includes any overhead associated     * with the implementation, such as the data structures     * used to hold the state of the record store, etc.     *     * @return the size of the record store in bytes     */    public int getSize() {        return DB_HEADER_SIZE + RecordStoreUtil.getInt(dbHeader, RS6_DATA_SIZE);    }    /**     * Returns the amount of additional room (in bytes) available for     * this record store to grow. Note that this is not necessarily     * the amount of extra MIDlet-level data which can be stored,     * as implementations may store additional data structures with     * each record to support integration with native applications,     * synchronization, etc.     *     * @return the amount of additional room (in bytes) available for     *          this record store to grow     */    public int getSizeAvailable() {        int fileSpace = dbFile.spaceAvailable(suiteId) -                        BLOCK_HEADER_SIZE - DB_HEADER_SIZE;        int limitSpace = RMSConfig.STORAGE_SUITE_LIMIT -                         RecordStoreUtil.getInt(dbHeader, RS6_DATA_SIZE) -                         BLOCK_HEADER_SIZE - DB_HEADER_SIZE;        int rv = (fileSpace < limitSpace) ? fileSpace : limitSpace;        return (rv < 0) ? 0 : rv;    }    /**     * Returns the last time the record store was modified, in the     * format used by System.currentTimeMillis().     *     * @return the last time the record store was modified, in the     *          format used by System.currentTimeMillis()     */    public long getLastModified() {        return RecordStoreUtil.getLong(dbHeader, RS5_LAST_MODIFIED);    }    /**     * Returns the recordId of the next record to be added to the     * record store. This can be useful for setting up pseudo-relational     * relationships. That is, if you have two or more     * record stores whose records need to refer to one another, you can     * predetermine the recordIds of the records that will be created     * in one record store, before populating the fields and allocating     * the record in another record store. Note that the recordId returned     * is only valid while the record store remains open and until a call     * to <code>addRecord()</code>.     *     * @return the recordId of the next record to be added to the     *          record store     */    public int getNextRecordID() {        return RecordStoreUtil.getInt(dbHeader, RS2_NEXT_ID);    }    /**     * 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 SecurityException if the MIDlet has read-only access     *          to the RecordStore     */    public int addRecord(byte[] data, int offset, int numBytes)        throws RecordStoreNotOpenException, RecordStoreException,            RecordStoreFullException {        synchronized (recordStoreLock) {            recordStoreLock.obtain();            try {                int recordId = getNextRecordID();                try {                    // add a block for this record                    addBlock(recordId, data, offset, numBytes);                    // update the db header                    RecordStoreUtil.putInt(recordId+1, dbHeader, RS2_NEXT_ID);                    RecordStoreUtil.putInt(getNumRecords()+1, dbHeader,                             RS3_NUM_LIVE);                    RecordStoreUtil.putInt(getVersion()+1, dbHeader,                             RS4_VERSION);                    RecordStoreUtil.putLong(System.currentTimeMillis(),                             dbHeader, RS5_LAST_MODIFIED);                    // write out the changes to the db header                    dbFile.seek(RS2_NEXT_ID);                    dbFile.write(dbHeader, RS2_NEXT_ID, 3*4+8);                    // dbFile.commitWrite();                } catch (java.io.IOException ioe) {                    throw new RecordStoreException("error writing new record "                            + "data");                }                // Return the new record id                return recordId;            } finally {                recordStoreLock.release();            }        }    }    /**     * 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     * @exception SecurityException if the MIDlet has read-only access     *          to the RecordStore     */    public void deleteRecord(int recordId)        throws RecordStoreNotOpenException, InvalidRecordIDException,            RecordStoreException {        synchronized (recordStoreLock) {            recordStoreLock.obtain();            try {                byte[] header = new byte[BLOCK_HEADER_SIZE];                int blockOffset = dbIndex.getRecordHeader(recordId, header);                // free the block                freeBlock(blockOffset, header);                // update the db index                dbIndex.deleteRecordIndex(recordId);                // update the db header                RecordStoreUtil.putInt(getNumRecords()-1, dbHeader,                         RS3_NUM_LIVE);                RecordStoreUtil.putInt(getVersion()+1, dbHeader, RS4_VERSION);                RecordStoreUtil.putLong(System.currentTimeMillis(), dbHeader,                        RS5_LAST_MODIFIED);                // save the updated db header                dbFile.seek(RS3_NUM_LIVE);                dbFile.write(dbHeader, RS3_NUM_LIVE, 2*4+8);                // dbFile.commitWrite();            } catch (java.io.IOException ioe) {                throw new RecordStoreException("error updating file after" +                        " record deletion");            } finally {                recordStoreLock.release();            }        }    }    /**     * 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 (recordStoreLock) {            recordStoreLock.obtain();            try {                byte[] header = new byte[BLOCK_HEADER_SIZE];                try {                dbIndex.getRecordHeader(recordId, header);                } catch (java.io.IOException ioe) {                    throw new RecordStoreException("error reading record data");                }                return RecordStoreUtil.getInt(header, 4);            } finally {                recordStoreLock.release();            }        }    }    /**     * 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     * @exception ArrayIndexOutOfBoundsException if the record is     *          larger than the buffer supplied     *     * @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 (recordStoreLock) {            recordStoreLock.obtain();            try {                byte[] header = new byte[BLOCK_HEADER_SIZE];                int blockOffset = dbIndex.getRecordHeader(recordId, header);                int dataSize = RecordStoreUtil.getInt(header, 4);                dbFile.seek(blockOffset+BLOCK_HEADER_SIZE);                return dbFile.read(buffer, offset, dataSize);            } catch (java.io.IOException ioe) {                throw new RecordStoreException("error reading record data");            } finally {                recordStoreLock.release();            }        }    }    /**     * 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 (recordStoreLock) {            recordStoreLock.obtain();            try {

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?