001: /*
002: *
003: *
004: * Portions Copyright 2000-2007 Sun Microsystems, Inc. All Rights
005: * Reserved. Use is subject to license terms.
006: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License version
010: * 2 only, as published by the Free Software Foundation.
011: *
012: * This program is distributed in the hope that it will be useful, but
013: * WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * General Public License version 2 for more details (a copy is
016: * included at /legal/license.txt).
017: *
018: * You should have received a copy of the GNU General Public License
019: * version 2 along with this work; if not, write to the Free Software
020: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
021: * 02110-1301 USA
022: *
023: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
024: * Clara, CA 95054 or visit www.sun.com if you need additional
025: * information or have any questions.
026: *
027: * Copyright 2000 Motorola, Inc. All Rights Reserved.
028: * This notice does not imply publication.
029: */
030:
031: package com.sun.midp.rms;
032:
033: import javax.microedition.rms.RecordStoreException;
034:
035: /**
036: * A class implementing record store utility functions.
037: */
038:
039: public class RecordStoreUtil {
040: /**
041: * A convenience method for converting a byte array into
042: * an int (assumes big-endian byte ordering).
043: *
044: * @param data the byte array returned from the database.
045: * @param offset the offset into the array of the first byte to start from.
046: *
047: * @return an int corresponding to the first four bytes
048: * of the array passed in.
049: */
050: static int getInt(byte[] data, int offset) {
051: int r = data[offset++];
052: r = (r << 8) | ((int) (data[offset++]) & 0xff);
053: r = (r << 8) | ((int) (data[offset++]) & 0xff);
054: r = (r << 8) | ((int) (data[offset++]) & 0xff);
055: return r;
056: }
057:
058: /**
059: * A convenience method for converting a byte array into
060: * a long (assumes big-endian byte ordering).
061: *
062: * @param data the byte array returned from the database.
063: * @param offset the offset into the array of the first byte to start from.
064: * @return a long corresponding to the first eight bytes
065: * of the array passed in.
066: */
067: static long getLong(byte[] data, int offset) {
068: long r = data[offset++];
069: r = (r << 8) | ((long) (data[offset++]) & 0xff);
070: r = (r << 8) | ((long) (data[offset++]) & 0xff);
071: r = (r << 8) | ((long) (data[offset++]) & 0xff);
072: r = (r << 8) | ((long) (data[offset++]) & 0xff);
073: r = (r << 8) | ((long) (data[offset++]) & 0xff);
074: r = (r << 8) | ((long) (data[offset++]) & 0xff);
075: r = (r << 8) | ((long) (data[offset++]) & 0xff);
076: return r;
077: }
078:
079: /**
080: * A convenience method for converting an integer into
081: * a byte array.
082: *
083: * @param i the integer to turn into a byte array.
084: * @param data a place to store the bytes of <code>i</code>.
085: * @param offset starting point within <code>data<code> to
086: * store <code>i</code>.
087: *
088: * @return the number of bytes written to the array.
089: */
090: static int putInt(int i, byte[] data, int offset) {
091: data[offset++] = (byte) ((i >> 24) & 0xff);
092: data[offset++] = (byte) ((i >> 16) & 0xff);
093: data[offset++] = (byte) ((i >> 8) & 0xff);
094: data[offset] = (byte) (i & 0xff);
095: return 4;
096: }
097:
098: /**
099: * A convenience method for converting a long into
100: * a byte array.
101: *
102: * @param l the <code>long<code> to turn into a byte array.
103: * @param data a place to store the bytes of <code>l</code>.
104: * @param offset Starting point within <code>data</code> to
105: * store <code>l</code>.
106: *
107: * @return the number of bytes written to the array.
108: */
109: static int putLong(long l, byte[] data, int offset) {
110: data[offset++] = (byte) ((l >> 56) & 0xff);
111: data[offset++] = (byte) ((l >> 48) & 0xff);
112: data[offset++] = (byte) ((l >> 40) & 0xff);
113: data[offset++] = (byte) ((l >> 32) & 0xff);
114: data[offset++] = (byte) ((l >> 24) & 0xff);
115: data[offset++] = (byte) ((l >> 16) & 0xff);
116: data[offset++] = (byte) ((l >> 8) & 0xff);
117: data[offset] = (byte) (l & 0xff);
118: return 8;
119: }
120:
121: /**
122: * A convenience method for calculating the block size given the data size.
123: *
124: * @param dataSize the size of the data in the block.
125: *
126: * @return an int corresponding to the size of the block padded to
127: * a multiple of the BLOCK HEADER SIZE.
128: */
129: static int calculateBlockSize(int dataSize) {
130: final int block_header_size = AbstractRecordStoreImpl.BLOCK_HEADER_SIZE;
131: int remainder = dataSize % block_header_size; // & 0x07;
132:
133: if (remainder == 0) {
134: return dataSize + block_header_size;
135: } else {
136: return dataSize + (block_header_size - remainder)
137: + block_header_size;
138: }
139: }
140:
141: /**
142: * Looks to see if the storage file for record store
143: * identified by <code>uidPath</code> exists
144: *
145: * @param suiteId ID of the MIDlet suite that owns the record store
146: * @param name name of the record store
147: * @param extension the extension for the record store file
148: *
149: * @return true if the file exists, false if it does not.
150: */
151: native static boolean exists(int suiteId, String name, int extension);
152:
153: /**
154: * Removes the storage file for record store <code>filename</code>
155: * if it exists.
156: *
157: * @param suiteId ID of the MIDlet suite that owns the record store
158: * @param name name of the record store
159: * @param extension the extension for the record store file
160: *
161: * @throws RecordStoreException if deletion encountered an error
162: * internally.
163: */
164: native static void deleteFile(int suiteId, String name,
165: int extension) throws RecordStoreException;
166:
167: /**
168: * Removes record store file without throwing an exception on failure.
169: *
170: * @param suiteId ID of the MIDlet suite that owns the record store
171: * @param name name of the record store
172: * @param extension the extension for the record store file
173: *
174: * @return <code>true</code> if file was found and deleted successfully,
175: * <code>false</code> otherwise.
176: */
177: static boolean quietDeleteFile(int suiteId, String name,
178: int extension) {
179: try {
180: deleteFile(suiteId, name, extension);
181: } catch (Throwable t) {
182: return false;
183: }
184: return true;
185: }
186:
187: }
|