📄 recordstorefile.java
字号:
/* * @(#)RecordStoreFile.java 1.33 01/08/21 * Copyright (c) 2000-2001 Sun Microsystems, Inc. All Rights Reserved. * * This software is the confidential and proprietary information of Sun * Microsystems, Inc. ("Confidential Information"). You shall not * disclose such Confidential Information and shall use it only in * accordance with the terms of the license agreement you entered into * with Sun. * * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING * THIS SOFTWARE OR ITS DERIVATIVES. */package com.sun.midp.rms;import java.io.IOException;import java.util.Vector;import javax.microedition.io.Connector;import com.sun.midp.security.SecurityDomain;import com.sun.midp.midlet.Scheduler;import com.sun.midp.midlet.MIDletSuite;import com.sun.midp.io.j2me.storage.RandomAccessStream;import com.sun.midp.io.j2me.storage.File;/** * A RecordStoreFile is a file abstraction layer between a * a RecordStore and an underlying persistent storage mechanism. * The underlying storage methods are provided by the * RandomAccessStream and File classes. * * RecordStoreFile confines the namespace of a record store to * the scope of the MIDlet suite of its creating application. * It also ensures unicode recordstore names are ascii filesystem safe. * * The RecordStore class can be implemented directly using the * RandomAccessStream and File classes. However, * RecordStoreFile served as the java/native code boundry for * RMS in the MIDP 1.0 release. It exists now for * backwards compatibility with older ports. */public class RecordStoreFile { /** This class has a different security domain than the MIDlet suite */ private static SecurityDomain classSecurityDomain; /** Easily recognize record store files in the file system */ private static final String dbExtension = ".db"; /** Stream to read/write record store data to */ private RandomAccessStream recordStream; /** * Initializes the security domain for this class, so it can * perform actions that a normal MIDlet Suite cannot. * * @param theDomain <code>SecurityDomain</code> for this class. */ public static void initSecurityDomain(SecurityDomain theDomain) { if (classSecurityDomain != null) { return; } classSecurityDomain = theDomain; } /** * Constructs a new RecordStoreFile instance. * * This process involves a few discrete steps and concludes with * with opening a RandomAccessStream that this RecordStoreFile * instance will use for persistant storage. * * The steps in constructing a RecordStoreFile instance are: * <ul> * <li>The storage path for the caller's MIDlet suite * is found. * * <li>The <code>filename</code> arg is converted into an ascii * equivalent safe to use directly in the underlying * file system and appended to the storage path. See the * com.sun.midp.io.j2me.storage.File.unicodeToAsciiFilename() * method for conversion details. * * <li>Finally a ".db" extension is appeded to the file name. * This result is then connected with a new <code>RandomAccessStrem</code> * to provide to store record data for this instance. * </ul> * * @param filename the record store name associated with this * <code>RecoreStoreFile</code> instance. * * @exception IOException if there is an error opening the file. */ public RecordStoreFile(String filename) throws IOException { String path; RandomAccessStream newStream; path = getStoragePath(filename); newStream = new RandomAccessStream(classSecurityDomain); newStream.connect(path, Connector.READ_WRITE); recordStream = newStream; } /** * Looks to see if the storage file for record store * <code>filename</code> already exists. * * @param filename the name of the file to look for. * * @return true if the file exists, false if it does not. */ public static boolean exists(String filename) { String path; File file; path = getStoragePath(filename); file = new File(classSecurityDomain); return file.exists(path); } /** * Removes the storage file for record store <code>filename</code> * if it exists. * * @param filename path to the file to delete. * * @return true if successful or false if an IOException occurred * internally. */ public static boolean deleteFile(String filename) { String path; File file; path = getStoragePath(filename); file = new File(classSecurityDomain); try { file.delete(path); return true; } catch (IOException ioe) { return false; } } /** * Sets the position within <code>recordStream</code> to * <code>pos</code>. This will implicitly grow * the underlying stream if <code>pos</code> is made greater * than the current length of the storage stream. * * @param pos position within the file to move the current_pos * pointer to. * * @exception IOException if there is a problem with the seek. */ public void seek(int pos) throws IOException { recordStream.setPosition(pos); } /** * Write all of <code>buf</code> to <code>recordStream</code>. * * @param buf buffer to read out of. * * @exception IOException if a write error occurs. */ public void write(byte[] buf) throws IOException { write(buf, 0, buf.length); } /** * Write <code>buf</code> to <code>recordStream</code>, starting * at <code>offset</code> and continuing for <code>numBytes</code> * bytes. * * @param buf buffer to read out of. * @param offset starting point write offset, from beginning of buffer. * @param numBytes the number of bytes to write. * * @exception IOException if a write error occurs. */ public void write(byte[] buf, int offset, int numBytes) throws IOException { recordStream.writeBytes(buf, offset, numBytes); } /** * Read up to <code>buf.length</code> into <code>buf</code>. * * @param buf buffer to read in to. * * @return the number of bytes read. * * @exception IOException if a read error occurs. */ public int read(byte[] buf) throws IOException { return read(buf, 0, buf.length); } /** * Read up to <code>buf.length</code> into <code>buf</code> * starting at offset <code>offset</code> in <code>recordStream * </code> and continuing for up to <code>numBytes</code> bytes. * * @param buf buffer to read in to. * @param offset starting point read offset, from beginning of buffer. * @param numBytes the number of bytes to read. * * @return the number of bytes read. * * @exception IOException if a read error occurs. */ public int read(byte[] buf, int offset, int numBytes) throws IOException { return recordStream.readBytes(buf, offset, numBytes); } /** * Disconnect from <code>recordStream</code> if it is * non null. May be called more than once without error. * * @exception IOException if an error occurs closing * <code>recordStream</code>. */ public void close() throws IOException { // close recordStream if it exists if (recordStream != null) { recordStream.disconnect(); recordStream = null; } } /** * Sets the length of this <code>RecordStoreFile</code> * <code>size</code> bytes. If this file was previously * larger than <code>size</code> the extra data is lost. * * <code>size</code> must be <= the current length of * <code>recordStream</code> * * @param size new size for this file. * * @exception IOException if an error occurs, or if * <code>size</code> is less than zero. */ public void truncate(int size) throws IOException { if (recordStream != null) { recordStream.truncate(size); } } /** * 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. * * @return an array of record store names. */ public static String[] listRecordStores() { File storage; String storageRoot; Vector files; Vector names; String file; String asciiName; storage = new File(classSecurityDomain); storageRoot = getStoragePath(null); files = storage.filenamesThatStartWith(storageRoot); names = new Vector(); // work through list of strings from the directory for (int i = 0; i < files.size(); i++) { file = (String)files.elementAt(i); if (file.endsWith(dbExtension)) { asciiName = file.substring(storageRoot.length(), file.length() - 3); /* * some or all of the strings in foo may be encoded * into a system specific format. decode them before * adding to names. */ names.addElement(File.asciiFilenameToUnicode(asciiName)); } } if (names.size() == 0) { return null; } String[] rv = new String[names.size()]; names.copyInto(rv); return rv; } /** * Approximation of remaining space in storage. * * Usage Warning: This may be a slow operation if * the platform has to look at the size of each file * stored in the MIDP memory space and include its size * in the total. * * @return the aproximate space available to grow the * record store in bytes. */ public static int spaceAvailable() { return new File(classSecurityDomain).getBytesAvailableForFiles(); } /** * Given a null argument this helper functions returns a * path to where record stores should be stored for the * MIDlet suite of the calling MIDlet process. If <code>name * </code> is non null it returns the full path of that * record store in the file system: * <storage_path><ascii_converted_'name'>.db * * @param name name of target record store, or null if only the * storage path for the current MIDlet suite is desired. * * @return the relative path to where record store files for the * current MIDlet suite are stored if <code>name</code> * is null, or the complete system path for storage * of the record store <code>name</code>. */ private static String getStoragePath(String name) { String str; MIDletSuite mSuite; StringBuffer path; mSuite = Scheduler.getScheduler().getMIDletSuite(); // MIDletSuite msuite should not be null. str = mSuite.getStorageRoot(); if (name != null) { path = new StringBuffer(str); // convert the unicode filename into a system acceptable string path.append(File.unicodeToAsciiFilename(name)); path.append(dbExtension); str = path.toString(); } return str; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -