⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cache.java

📁 The Funambol J2ME Mail Client aims to be a light, easy to use, free email client for J2ME devices.
💻 JAVA
字号:
/*
 * Copyright (C) 2006 Funambol
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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 for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package com.funambol.mailclient;

import com.funambol.mailclient.cm.Contact;
import com.funambol.mailclient.cm.ContactManagerException;
import com.funambol.mailclient.cm.rms.SerializableContact;
import com.funambol.mailclient.cm.rms.ContactEnumeration;
import com.funambol.mailclient.mm.MessageInfo;
import com.funambol.storage.ComplexSerializer;
import com.funambol.storage.Serializable;
import com.funambol.util.Log;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreNotOpenException;

public class Cache {
    
    //---------------------------------------------------------------- Constants
    /** Name of cache RMS*/
    public static final String CACHE = "MailclientCache";
    /** Number of RMS record for inbox cache*/
    public static final int INBOX = 1;
    /** Number of RMS record for vCard cache*/
    public static final int VCARD = 2;
    /** Number of RMS record for outbox cache: Not yet implemented*/
    public static final int OUTBOX = 3;
    /** Number of RMS record for sent cache: Not yet implemented*/
    public static final int SENT = 4;
    /** Number of RMS record for draft cache: Not yet implemented*/
    public static final int DRAFT = 5;
    /** Number of RMS cache records*/
    private static int NUMBER_OF_CACHES = 5;
    /** Default Number of item for messages cache*/
    private static int MESSAGE_SNAPSHOT_SIZE = 10;
    /** Default Number of item for contacts cache*/
    public static int CONTACT_SNAPSHOT_SIZE = 10;
    
    
    //------------------------------------------------------------- Constructors
    /**
     * No instances of this Class will be created. This is just a collection of 
     * static methods
     */
    private Cache() {
    }
    
    /**
     * Use this method from outside the cache to prevent cache duplication
     * Cache must be cleared every time the account is reset
     */
    public static void resetCache() {
        try {
            RecordStore.deleteRecordStore(CACHE);
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
            Log.debug("Cache RMS not found");
        }
    }
    
    /**
     * Instant snapshot to the cached message store 10 first items
     * @param cacheNumber is the number of the record of the cached RMS 
     * @param MessageInfo[] is the array of MessageInfo to be cached
     * @throw RecordStoreException
     * @throw IOException
     */
    public static void saveMsgSnapShot(int cacheNumber, MessageInfo[] mi) 
                                      throws RecordStoreException, IOException {
        saveMsgSnapShot(cacheNumber, mi, MESSAGE_SNAPSHOT_SIZE);
    }
    
    /**
     * Instant snapshot to the cached message store 10 first items
     * @param cacheNumber is the number of the record of the cached RMS 
     * @param MessageInfo[] is the array of MessageInfo to be cached
     * @param itemNumber is the size of the snapshot 
     * @throw RecordStoreException
     * @throw IOException
     */
    public static void saveMsgSnapShot(int cacheNumber, MessageInfo[] mi, 
                      int itemNumber) throws RecordStoreException, IOException {
        Log.debug("Saving Messages Snapshot");
        RecordStore rs = RecordStore.openRecordStore(CACHE, true);
        
        if (rs.getNumRecords()==0) {
            initRMSCache(rs);
        }
        
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream dout = new DataOutputStream(baos);
        int size = Math.min(mi.length, itemNumber);
        dout.writeInt(size);
        for (int i=0; i<size; i++) {
            mi[i].serialize(dout);
        }
        
        rs.setRecord(cacheNumber, baos.toByteArray(), 0, 
                                                     baos.toByteArray().length);
        rs.closeRecordStore();
    }
    
    /**
     * Instant snapshot to the Contact Store's 10 first items
     * @param cacheNumber is the cache id 
     * @param c is the contact array to be serialized
     * @throw RecordStoreException
     * @throw IOException
     * @throw ContactManagerException
     */
    public static void saveContactSnapShot(int cacheNumber, Contact[] c) 
             throws ContactManagerException, RecordStoreException, IOException {
        saveContactSnapShot(cacheNumber, c, CONTACT_SNAPSHOT_SIZE);
    }
    
    /**
     * Instant snapshot to the Contact Store's 10 first items
     * @param cacheNumber is the cache id 
     * @param c is the contact array to be serialized
     * @param itemNumber is the size of the snapshot 
     * @throw RecordStoreException
     * @throw IOException
     * @throw ContactManagerException
     */
    public static void saveContactSnapShot(int cacheNumber, Contact[] c, int itemNumber) 
             throws ContactManagerException, RecordStoreException, IOException {
        Log.debug("Saving Contact Snapshot");
        
        RecordStore rs = RecordStore.openRecordStore(CACHE, true);
        if (rs.getNumRecords()==0) {
            initRMSCache(rs);
        }
        
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream dout = new DataOutputStream(baos);
        
        int size = Math.min(c.length, itemNumber);
        dout.writeInt(size);
        
        for (int i=0; i<size; i++) {
            SerializableContact sc = new SerializableContact(
                    c[i].getNickName(),
                    c[i].getFirstName(),
                    c[i].getLastName(),
                    c[i].getDefaultEmail(),
                    c[i].getEmail_2(),
                    c[i].getEmail_3(),
                    c[i].getHomePhone(),
                    c[i].getJobPhone(),
                    c[i].getMobilePhone());
            sc.setContactId(c[i].getContactId());    
            sc.serialize(dout);
        }
        rs.setRecord(cacheNumber, baos.toByteArray(), 0, 
                                                     baos.toByteArray().length);
        rs.closeRecordStore();
    }
    
    /**
     * Get last <code>itemNumber</code> cached entries
     * @param cacheNumber is the number of the record in the cache RMS 
     * @return MessageInfo[] containing first <code>itemNumber</code> 
     * MessageInfo
     * @throw RecordStoreException
     * @throw IOException
     */
    public static MessageInfo[] getMsgSnapShot(int cacheNumber) 
                                      throws RecordStoreException, IOException {
        return (MessageInfo[]) getMsgSnapShot(cacheNumber, MESSAGE_SNAPSHOT_SIZE);
    }
    
    /**
     * Get last <code>itemNumber</code> cached entries
     * @param cacheNumber is the number of the record in the cache RMS 
     * @param itemNumber is the size of the snapshot 
     * @return MessageInfo[] containing first <code>itemNumber</code> 
     * MessageInfo
     * @throw RecordStoreException
     * @throw IOException
     */
    public static Serializable[] getMsgSnapShot(int cacheNumber, int itemNumber) 
                                      throws RecordStoreException, IOException {
        Log.debug("Getting Messages Snapshot");
        RecordStore rs = RecordStore.openRecordStore(CACHE, true);
        MessageInfo[] mi = null;
        
        if (rs.getNumRecords()==0) {
            mi = new MessageInfo[0];
            return mi;
        }
        
        byte[] record=rs.getRecord(cacheNumber);
        
        Log.debug("Cache byte array length: "+record.length);
        
        ByteArrayInputStream bais = new ByteArrayInputStream(record);
        DataInputStream din = new DataInputStream(bais);
        
        int size = Math.min(din.readInt(), itemNumber);
        
        if (size>itemNumber) {
            Log.debug("Cache is greater than required array size");
        }
        
        mi = new MessageInfo[size];
        
        for (int i=0; i<size; i++) {
            mi[i] = new MessageInfo();
            mi[i].deserialize(din);
        }
        
        
        Log.debug("mi.len: "+mi.length);
        rs.closeRecordStore();
        
        return mi;
    }

    /**
     * Get last <code>itemNumber</code> cached entries
     * @param cacheNumber is the number of the record in the cache RMS 
     * @param itemNumber is the size of the snapshot 
     * @return Contact[] containing first <code>itemNumber</code> Contacts
     * @throw RecordStoreException
     * @throw IOException
     * @throw ContactManagerException
     */
    public static Contact[] getContactSnapShot(int cacheNumber) 
        throws RecordStoreException, IOException, ContactManagerException {
        return getContactSnapShot(cacheNumber, CONTACT_SNAPSHOT_SIZE);
    }
    
    /**
     * Get last <code>itemNumber</code> cached entries
     * @param cacheNumber is the number of the record in the cache RMS 
     * @param itemNumber is the size of the snapshot 
     * @return Contact[] containing first <code>itemNumber</code> Contacts
     * @throw RecordStoreException
     * @throw IOException
     * @throw ContactManagerException
     */
    public static Contact[] getContactSnapShot(int cacheNumber, int itemNumber) 
        throws RecordStoreException, IOException, ContactManagerException {
        Log.debug("Getting Contact Snapshot");
        int size=0;
        RecordStore rs = RecordStore.openRecordStore(CACHE, true);
        
        if (rs.getNumRecords()==0) {
            return new Contact[0];
        }
        
        Contact[] c = null;
        ByteArrayInputStream bais = new ByteArrayInputStream(rs.getRecord(cacheNumber));
        DataInputStream din = new DataInputStream(bais);
        
        size=din.readInt();
        Log.debug("size: " +size);
        if (size>itemNumber) {
            size=itemNumber;
            Log.debug("Cache size ["+size+"] is greater than required array " +
                    "size [" + itemNumber + "]");
        } 
        
        c = new Contact[size];
        
        
        for (int i=0; i<size; i++) {
            SerializableContact sc = new SerializableContact();
            sc.deserialize(din);
            c[i]=new Contact(
                    sc.getNickName(),
                    sc.getFirstName(),
                    sc.getLastName(),
                    sc.getDefaultEmail(),
                    sc.getEmail_2(),
                    sc.getEmail_3(),
                    sc.getHomePhone(),
                    sc.getJobPhone(),
                    sc.getMobilePhone());
            c[i].setContactId(sc.getContactId());
        }
        rs.closeRecordStore();
        
        return c;
    }
    
    //----------------------------------------------------------- Public Methods
    /**
     * Initialize 5 records that will be set as the RMS mailclient cache
     * @param rs is the cache related RecordStore
     * @throw RecordStoreException
     */
    private static void initRMSCache(RecordStore rs) throws RecordStoreException, IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(baos);
        dos.writeInt(0);
        byte[] a = baos.toByteArray();
        
        for (int i=1; i<=NUMBER_OF_CACHES; i++) {
            rs.addRecord(a, 0, a.length);
        }
    }
}

⌨️ 快捷键说明

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