recordstoreimpl.java
来自「This is a resource based on j2me embedde」· Java 代码 · 共 1,191 行 · 第 1/4 页
JAVA
1,191 行
/* * * * Portions Copyright 2000-2007 Sun Microsystems, Inc. All Rights * Reserved. Use is subject to license terms. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License version 2 for more details (a copy is * included at /legal/license.txt). * * You should have received a copy of the GNU General Public License * version 2 along with this work; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions. * * Copyright 2000 Motorola, Inc. All Rights Reserved. * This notice does not imply publication. */package com.sun.midp.rms;import java.io.IOException;import javax.microedition.rms.*;import com.sun.midp.security.Permissions;import com.sun.midp.security.SecurityToken;import com.sun.midp.log.Logging;import com.sun.midp.log.LogChannels;/** * A class implementing a MIDP a record store. */public class RecordStoreImpl implements AbstractRecordStoreImpl { /** used to compact the records of the record store */ private byte[] compactBuffer = new byte[COMPACT_BUFFER_SIZE]; /** * Internal indicator for AUTHMODE_ANY with read only access * AUTHMODE_ANY_RO has a value of 2. */ final static int AUTHMODE_ANY_RO = 2; /** unique id for suite that owns this record store */ int suiteId; /** * lock used to synchronize this record store between concurrently * running MIDlets */ AbstractRecordStoreLock recordStoreLock; /** data block header stored here */ byte[] dbHeader; /** record store index */ private RecordStoreIndex dbIndex; /** record store data */ private RecordStoreFile dbFile; /** * Deletes the named record store. MIDlet suites are only allowed * to delete their own record stores. If the named record store is * open (by a MIDlet in this suite or a MIDlet in a different * MIDlet suite) when this method is called, a * RecordStoreException will be thrown. If the named record store * does not exist a RecordStoreNotFoundException will be * thrown. Calling this method does NOT result in recordDeleted * calls to any registered listeners of this RecordStore. * * @param token security token for authorization * @param suiteId ID of the MIDlet suite that owns the record store * @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(SecurityToken token, int suiteId, String recordStoreName) throws RecordStoreException, RecordStoreNotFoundException { token.checkIfPermissionAllowed(Permissions.MIDP); // check if file exists and delete it if (RecordStoreUtil.exists( RmsEnvironment.getSecureFilenameBase(suiteId), recordStoreName, RecordStoreFile.DB_EXTENSION)) { boolean success = RecordStoreIndex.deleteIndex( suiteId, recordStoreName); RecordStoreUtil.deleteFile( RmsEnvironment.getSecureFilenameBase(suiteId), recordStoreName, RecordStoreFile.DB_EXTENSION); if (!success) { throw new RecordStoreException("deleteRecordStore " + "failed"); } } else { throw new RecordStoreNotFoundException("deleteRecordStore " + "error: file " + "not found"); } } /** * Open (and possibly create) a record store associated with the * given MIDlet suite. If this method is called by a MIDlet when * the record store is already open by a MIDlet in the MIDlet suite, * this method returns a reference to the same RecordStoreImpl object. * * @param token security token for authorization * @param suiteId ID of the MIDlet suite that owns the record store * @param recordStoreName the MIDlet suite unique name for the * record store, consisting of between one and 32 Unicode * characters inclusive. * @param createIfNecessary if true, the record store will be * created if necessary * * @return <code>RecordStore</code> object for the record store * * @exception RecordStoreException if a record store-related * exception occurred * @exception RecordStoreNotFoundException if the record store * could not be found * @exception RecordStoreFullException if the operation cannot be * completed because the record store is full * @exception IllegalArgumentException if * recordStoreName is invalid */ public static RecordStoreImpl openRecordStore(SecurityToken token, int suiteId, String recordStoreName, boolean createIfNecessary) throws RecordStoreException, RecordStoreFullException, RecordStoreNotFoundException { token.checkIfPermissionAllowed(Permissions.MIDP); return new RecordStoreImpl(token, suiteId, recordStoreName, createIfNecessary); } /** * Changes the access mode for this RecordStore. The authorization * mode choices are: * * <ul> * <li><code>AUTHMODE_PRIVATE</code> - Only allows the MIDlet * suite that created the RecordStore to access it. This * case behaves identically to * <code>openRecordStore(recordStoreName, * createIfNecessary)</code>.</li> * <li><code>AUTHMODE_ANY</code> - Allows any MIDlet to access the * RecordStore. Note that this makes your recordStore * accessible by any other MIDlet on the device. This * could have privacy and security issues depending on * the data being shared. Please use carefully.</li> * </ul> * * <p>The owning MIDlet suite may always access the RecordStore and * always has access to write and update the store. Only the * owning MIDlet suite can change the mode of a RecordStore.</p> * * @param authmode the mode under which to check or create access. * Must be one of AUTHMODE_PRIVATE or AUTHMODE_ANY. * @param writable true if the RecordStore is to be writable by * other MIDlet suites that are granted access * * @exception RecordStoreException if a record store-related * exception occurred * @exception SecurityException if this MIDlet Suite is not * allowed to change the mode of the RecordStore * @exception IllegalArgumentException if authmode is invalid */ public void setMode(int authmode, boolean writable) throws RecordStoreException { synchronized (recordStoreLock) { recordStoreLock.obtain(); try { int newAuthMode = authmode; if ((authmode == RecordStore.AUTHMODE_ANY) && (writable == false)) { newAuthMode = AUTHMODE_ANY_RO; } RecordStoreUtil.putInt(newAuthMode, dbHeader, RS1_AUTHMODE); try { // write out the changes to the db header dbFile.seek(RS1_AUTHMODE); dbFile.write(dbHeader, RS1_AUTHMODE, 4); // dbFile.commitWrite(); } catch (java.io.IOException ioe) { throw new RecordStoreException("error writing record " + "store attributes"); } } finally { recordStoreLock.release(); } } } /** * 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 * and all RecordEnumerations associated with it become invalid. * 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 (recordStoreLock) { recordStoreLock.obtain(); try { // close native fd compactRecords(); // compact before close dbFile.close(); dbIndex.close(); } catch (java.io.IOException ioe) { throw new RecordStoreException("error closing .db file. " + ioe); } finally { recordStoreLock.release(); dbFile = null; } } } /** * 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. * * @param token security token for authorization * @param suiteId ID of the MIDlet suite that owns the record store * * @return 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(SecurityToken token, int suiteId) { token.checkIfPermissionAllowed(Permissions.MIDP); return RecordStoreFile.listRecordStores(suiteId); } /** * Get the authorization mode for this record store. * * @return authorization mode */ public int getAuthMode() { return RecordStoreUtil.getInt(dbHeader, RS1_AUTHMODE); } /** * Each time a record store is modified (by * <code>addRecord</code>, <code>setRecord</code>, or * <code>deleteRecord</code> methods) its <em>version</em> is * incremented. This can be used by MIDlets to quickly tell if
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?