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

📄 cpickle.java

📁 无线通信的主要编程软件,是无线通信工作人员的必备工具,关天相关教程我会在后续传上.
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
            if (using_appends)                file.write(MARK);            for (int i = 0; i < len; i++) {                save(object.__finditem__(i));                if (!using_appends)                    file.write(APPEND);            }            if (using_appends)                file.write(APPENDS);        }        final private void save_dict(PyObject object) {            if (bin)                file.write(EMPTY_DICT);            else {                file.write(MARK);                file.write(DICT);            }            put(putMemo(get_id(object), object));            PyObject list = object.invoke("keys");            int len = list.__len__();            boolean using_setitems = (bin && len > 1);            if (using_setitems)                file.write(MARK);            for (int i = 0; i < len; i++) {                PyObject key = list.__finditem__(i);                PyObject value = object.__finditem__(key);                save(key);                save(value);                if (!using_setitems)                     file.write(SETITEM);            }            if (using_setitems)                file.write(SETITEMS);        }        final private void save_inst(PyInstance object) {            if (object instanceof PyJavaInstance)                throw new PyException(PicklingError,                            "Unable to pickle java objects.");            PyClass cls = object.__class__;            PySequence args = null;            PyObject getinitargs = object.__findattr__("__getinitargs__");            if (getinitargs != null) {                args = (PySequence)getinitargs.__call__();                // XXX Assert it's a sequence                keep_alive(args);            }            file.write(MARK);            if (bin)                save(cls);            if (args != null) {                int len = args.__len__();                for (int i = 0; i < len; i++)                    save(args.__finditem__(i));            }            int mid = putMemo(get_id(object), object);            if (bin) {                file.write(OBJ);                put(mid);            } else {                file.write(INST);                file.write(cls.__findattr__("__module__").toString());                file.write("\n");                file.write(cls.__name__);                file.write("\n");                put(mid);            }            PyObject stuff = null;            PyObject getstate = object.__findattr__("__getstate__");            if (getstate == null) {                stuff = object.__dict__;            } else {                stuff = getstate.__call__();                keep_alive(stuff);            }            save(stuff);            file.write(BUILD);        }        final private void save_global(PyObject object) {            save_global(object, null);        }        final private void save_global(PyObject object, PyObject name) {            if (name == null)                name = object.__findattr__("__name__");            PyObject module = object.__findattr__("__module__");            if (module == null || module == Py.None)                module = whichmodule(object, name);            file.write(GLOBAL);            file.write(module.toString());            file.write("\n");            file.write(name.toString());            file.write("\n");            put(putMemo(get_id(object), object));        }        final private int getMemoPosition(int id, Object o) {            return memo.findPosition(id, o);        }        final private int putMemo(int id, PyObject object) {            int memo_len = memo.size() + 1;            memo.put(id, memo_len, object);            return memo_len;        }        /**         * Keeps a reference to the object x in the memo.         *         * Because we remember objects by their id, we have         * to assure that possibly temporary objects are kept         * alive by referencing them.         * We store a reference at the id of the memo, which should         * normally not be used unless someone tries to deepcopy         * the memo itself...         */        final private void keep_alive(PyObject obj) {            int id = System.identityHashCode(memo);            PyList list = (PyList) memo.findValue(id, memo);            if (list == null) {                list = new PyList();                memo.put(id, -1, list);            }            list.append(obj);        }    }    private static Hashtable classmap = new Hashtable();    final private static PyObject whichmodule(PyObject cls,                                              PyObject clsname)    {        PyObject name = (PyObject)classmap.get(cls);        if (name != null)            return name;        name = new PyString("__main__");        // For use with JPython1.0.x        //PyObject modules = sys.modules;        // For use with JPython1.1.x        //PyObject modules = Py.getSystemState().modules;        PyObject sys = imp.importName("sys", true);        PyObject modules = sys.__findattr__("modules");        PyObject keylist = modules.invoke("keys");        int len = keylist.__len__();        for (int i = 0; i < len; i++) {            PyObject key = keylist.__finditem__(i);            PyObject value = modules.__finditem__(key);            if (!key.equals("__main__") &&                    value.__findattr__(clsname.toString().intern()) == cls) {                name = key;                break;            }        }        classmap.put(cls, name);        //System.out.println(name);        return name;    }    /*     * A very specialized and simplified version of PyStringMap. It can     * only use integers as keys and stores both an integer and an object     * as value. It is very private!     */    static private class PickleMemo {        //Table of primes to cycle through        private final int[] primes = {            13, 61, 251, 1021, 4093,            5987, 9551, 15683, 19609, 31397,            65521, 131071, 262139, 524287, 1048573, 2097143,            4194301, 8388593, 16777213, 33554393, 67108859,            134217689, 268435399, 536870909, 1073741789,};        private transient int[] keys;        private transient int[] position;        private transient Object[] values;        private int size;        private transient int filled;        private transient int prime;        public PickleMemo(int capacity) {            prime = 0;            keys = null;            values = null;            resize(capacity);        }        public PickleMemo() {            this(4);        }        public synchronized int size() {            return size;        }        private int findIndex(int key, Object value) {            int[] table = keys;            int maxindex = table.length;            int index = (key & 0x7fffffff) % maxindex;            // Fairly aribtrary choice for stepsize...            int stepsize = maxindex / 5;            // Cycle through possible positions for the key;            //int collisions = 0;            while (true) {                int tkey = table[index];                if (tkey == key && value == values[index]) {                    return index;                }                if (values[index] == null) return -1;                index = (index+stepsize) % maxindex;            }        }        public int findPosition(int key, Object value) {            int idx = findIndex(key, value);            if (idx < 0) return -1;            return position[idx];        }        public Object findValue(int key, Object value) {            int idx = findIndex(key, value);            if (idx < 0) return null;            return values[idx];        }        private final void insertkey(int key, int pos, Object value) {            int[] table = keys;            int maxindex = table.length;            int index = (key & 0x7fffffff) % maxindex;            // Fairly aribtrary choice for stepsize...            int stepsize = maxindex / 5;            // Cycle through possible positions for the key;            while (true) {                int tkey = table[index];                if (values[index] == null) {                    table[index] = key;                    position[index] = pos;                    values[index] = value;                    filled++;                    size++;                    break;                } else if (tkey == key && values[index] == value) {                    position[index] = pos;                    break;                }                index = (index+stepsize) % maxindex;            }        }        private synchronized final void resize(int capacity) {            int p = prime;            for(; p<primes.length; p++) {                if (primes[p] >= capacity) break;            }            if (primes[p] < capacity) {                throw Py.ValueError("can't make hashtable of size: " +                                    capacity);            }            capacity = primes[p];            prime = p;            int[] oldKeys = keys;            int[] oldPositions = position;            Object[] oldValues = values;            keys = new int[capacity];            position = new int[capacity];            values = new Object[capacity];            size = 0;            filled = 0;            if (oldValues != null) {                int n = oldValues.length;                for(int i=0; i<n; i++) {                    Object value = oldValues[i];                    if (value == null) continue;                    insertkey(oldKeys[i], oldPositions[i], value);                }            }        }        public void put(int key, int pos, Object value) {            if (2*filled > keys.length) resize(keys.length+1);            insertkey(key, pos, value);        }    }    /**     * The Unpickler object. Unpickler instances are create by the factory     * methods Unpickler.     * @see cPickle#Unpickler(PyObject)     */    static public class Unpickler {        private IOFile file;        public Hashtable memo = new Hashtable();        /**         * For the benefit of persistency modules written using pickle,         * it supports the notion of a reference to an object outside         * the pickled data stream.         * Such objects are referenced by a name, which is an arbitrary         * string of printable ASCII characters.         * The resolution of such names is not defined by the pickle module         * -- the persistent object module will have to add a method         * persistent_load().         */        public PyObject persistent_load = null;        private PyObject mark = new PyString("spam");        private int stackTop;        private PyObject[] stack;        Unpickler(PyObject file) {            this.file = createIOFile(file);        }        /**         * Unpickle and return an instance of the object represented by         * the file.         */        public PyObject load() {            stackTop = 0;            stack = new PyObject[10];            while (true) {                String s = file.read(1);//              System.out.println("load:" + s);//              for (int i = 0; i < stackTop; i++)//                  System.out.println("   " + stack[i]);                if (s.length() < 1)                    load_eof();                char key = s.charAt(0);                switch (key) {                case PERSID:          load_persid(); break;                case BINPERSID:       load_binpersid(); break;                case NONE:            load_none(); break;                case INT:             load_int(); break;

⌨️ 快捷键说明

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