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

📄 pimitemqueue.java

📁 The Funambol J2ME Mail Client aims to be a light, easy to use, free email client for J2ME devices.
💻 JAVA
字号:
/*
 * Copyright (C) 2006-2007 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.syncml;

import com.funambol.storage.ObjectEnumeration;
import com.funambol.storage.ObjectFilter;
import com.funambol.storage.ObjectStore;
import com.funambol.storage.Serialized;
import com.funambol.util.Log;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.util.Enumeration;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;

public class PimItemQueue {
    public static final String ITEM_LIST_NAME = "SyncItemList";

    //------------------------------------------------------------- Private Data
    private ObjectStore syncItemList;
    
    //------------------------------------------------------------- Constructors
    
    /**
     * Creates a new instance of PimItemQueue
     */
    public PimItemQueue() {
        syncItemList = new ObjectStore();
        try {
            syncItemList.create(ITEM_LIST_NAME);
        } catch (RecordStoreException ex) {
            Log.error(this, ITEM_LIST_NAME + " cannot be open nor created: ");
            Log.error(ex.toString());
        }
    }
    
    //----------------------------------------------------------- Public Methods
    /**
     * Add the given sync item info to the store
     */
    public void addItemInfo(String key, char state) {
        try {
            PimItem ii = exists(key);
            
            PimItem rii = new PimItem(key, state);
            
            if (ii==null) {
                syncItemList.store(rii);
            } else {
                if (state=='D') {
                    syncItemList.store(ii.getId(), rii);
                }
            }
        } catch (RecordStoreException ex) {
            Log.error(this, ex.toString());
            ex.printStackTrace();
        } catch (IOException ex) {
            Log.error(this, ex.toString());
            ex.printStackTrace();
        }
    } 
    
    /**
     * Initialize item list
     */
    public void resetPimItemQueue() {
        try {
            syncItemList.close();
            RecordStore.deleteRecordStore(ITEM_LIST_NAME);
            syncItemList.create(ITEM_LIST_NAME);
            syncItemList.open(ITEM_LIST_NAME);
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
            Log.error(this, "Recordstore Exception in resetPimItemQueue() method");
        }
    }
    
    /**
     * remove the given sync item info from the store
     */
    public void removeItemInfo(String key) {
        try {
            PimItem sii = exists(key);
            if (sii!=null) {
                syncItemList.remove(sii.getId());
            }
        } catch (RecordStoreException ex) {
            Log.error("Item " + key + " cannot be removed");
            Log.error(ex.toString());
        } 
    } 
    
    /**
     * get sync item key and state
     */
    public PimItem getItemInfo(String key) {
        PimItem sii = exists(key);
        try {
            return (PimItem) 
                         syncItemList.retrieve(sii.getId(), new PimItem());
        } catch (IOException ex) {
            ex.printStackTrace();
            Log.error("Cannot retrieve item " + sii.getKey() + ":");
        } catch (RecordStoreException ex) {
            Log.error(ex.toString());
            ex.printStackTrace();
        }
        return null;
    }
    
    /**
     * get items of a certain state
     */
    public Enumeration getItemInfos(char state) {
        syncItemList.setObjectFilter(new ItemFilter(" ", state));
        PimItemEnumeration sie = new PimItemEnumeration(
                   (ObjectEnumeration) syncItemList.getObjects(new PimItem()));
        syncItemList.removeObjectFilter();
        return sie;
    }
    
    private PimItem exists(String key) {
        syncItemList.setObjectFilter(
                              new ItemFilter(key, ' '));
        Enumeration e = syncItemList.getObjects(new PimItem());
        PimItemEnumeration sie = 
                                 new PimItemEnumeration((ObjectEnumeration) e);
        if (sie.hasMoreElements()) {
            PimItem rii = (PimItem) sie.nextElement();
            syncItemList.removeObjectFilter();
            return rii; 
        }
        syncItemList.removeObjectFilter();
        return null;
    }
    
    class ItemFilter implements ObjectFilter {
        char state;
        String key;
        public ItemFilter(String key, char state) {
            this.state = state;
            this.key = key;
        } 
        public boolean matches(byte[] b) {
            char found = ' ';
            ByteArrayInputStream bais = new ByteArrayInputStream(b);
            DataInputStream dis = new DataInputStream(bais);
            boolean ret = false;
            if (state!=found) {
                try {
                    dis.readUTF();
                    found = dis.readChar();
                    ret = (found==state);
                } catch (IOException ex) {
                    Log.error(this, "Cannot read record:");
                    Log.error(this, ex.toString());
                    ex.printStackTrace();
                }
            } else {
                try {
                    String actualKey = dis.readUTF();
                    ret = (actualKey.equals(key));
                } catch (IOException ex) {
                    ex.printStackTrace();
                    Log.error(this, "Cannot read record:");
                    Log.error(this, ex.toString());
                }
            }
            return ret;
        }
    }
}

⌨️ 快捷键说明

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