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

📄 cpickle.java

📁 无线通信的主要编程软件,是无线通信工作人员的必备工具,关天相关教程我会在后续传上.
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
    final static char EMPTY_LIST      = ']';    final static char OBJ             = 'o';    final static char PUT             = 'p';    final static char BINPUT          = 'q';    final static char LONG_BINPUT     = 'r';    final static char SETITEM         = 's';    final static char TUPLE           = 't';    final static char EMPTY_TUPLE     = ')';    final static char SETITEMS        = 'u';    final static char BINFLOAT        = 'G';    private static PyDictionary dispatch_table = null;    private static PyDictionary safe_constructors = null;    private static PyClass BuiltinFunctionType =                            PyJavaClass.lookup(PyReflectedFunction.class);    private static PyClass BuiltinMethodType =                            PyJavaClass.lookup(PyMethod.class);    private static PyClass ClassType =                            PyJavaClass.lookup(PyClass.class);    private static PyClass DictionaryType =                            PyJavaClass.lookup(PyDictionary.class);    private static PyClass StringMapType =                            PyJavaClass.lookup(PyStringMap.class);    private static PyClass FloatType =                            PyJavaClass.lookup(PyFloat.class);    private static PyClass FunctionType =                            PyJavaClass.lookup(PyFunction.class);    private static PyClass InstanceType =                            PyJavaClass.lookup(PyInstance.class);    private static PyClass IntType =                            PyJavaClass.lookup(PyInteger.class);    private static PyClass ListType =                            PyJavaClass.lookup(PyList.class);    private static PyClass LongType =                            PyJavaClass.lookup(PyLong.class);    private static PyClass NoneType =                            PyJavaClass.lookup(PyNone.class);    private static PyClass StringType =                            PyJavaClass.lookup(PyString.class);    private static PyClass TupleType =                            PyJavaClass.lookup(PyTuple.class);    private static PyClass FileType =                            PyJavaClass.lookup(PyFile.class);    private static PyObject dict;    /**     * Initialization when module is imported.     */    public static void classDictInit(PyObject dict) {        cPickle.dict = dict;        // XXX: Hack for JPython 1.0.1. By default __builtin__ is not in        // sys.modules.        imp.importName("__builtin__", true);        PyModule copyreg = (PyModule)importModule("copy_reg");        dispatch_table = (PyDictionary)copyreg.__getattr__("dispatch_table");        safe_constructors = (PyDictionary)                                    copyreg.__getattr__("safe_constructors");        PickleError       = buildClass("PickleError", Py.Exception,                                       "_PickleError", "");        PicklingError     = buildClass("PicklingError", PickleError,                                       "_empty__init__", "");        UnpickleableError = buildClass("UnpickleableError", PicklingError,                                       "_UnpickleableError", "");        UnpicklingError   = buildClass("UnpicklingError", PickleError,                                       "_empty__init__", "");    }    // An empty __init__ method    public static PyObject _empty__init__(PyObject[] arg, String[] kws) {        PyObject dict = new PyStringMap();        dict.__setitem__("__module__", new PyString("cPickle"));        return dict;    }    public static PyObject _PickleError(PyObject[] arg, String[] kws) {        PyObject dict = _empty__init__(arg, kws);        dict.__setitem__("__init__", getJavaFunc("_PickleError__init__"));        dict.__setitem__("__str__", getJavaFunc("_PickleError__str__"));        return dict;    }    public static void _PickleError__init__(PyObject[] arg, String[] kws) {        ArgParser ap = new ArgParser("__init__", arg, kws, "self", "args");        PyObject self = ap.getPyObject(0);        PyObject args = ap.getList(1);        self.__setattr__("args", args);    }    public static PyString _PickleError__str__(PyObject[] arg, String[] kws) {        ArgParser ap = new ArgParser("__str__", arg, kws, "self");        PyObject self = ap.getPyObject(0);        PyObject args = self.__getattr__("args");        if (args.__len__() > 0 && args.__getitem__(0).__len__()  > 0)            return args.__getitem__(0).__str__();        else            return new PyString("(what)");    }    public static PyObject _UnpickleableError(PyObject[] arg, String[] kws) {        PyObject dict = _empty__init__(arg, kws);        dict.__setitem__("__init__",                                getJavaFunc("_UnpickleableError__init__"));        dict.__setitem__("__str__",                                getJavaFunc("_UnpickleableError__str__"));        return dict;    }    public static void _UnpickleableError__init__(PyObject[] arg,                                                  String[] kws)    {        ArgParser ap = new ArgParser("__init__", arg, kws, "self", "args");        PyObject self = ap.getPyObject(0);        PyObject args = ap.getList(1);        self.__setattr__("args", args);    }    public static PyString _UnpickleableError__str__(PyObject[] arg,                                                     String[] kws)    {        ArgParser ap = new ArgParser("__str__", arg, kws, "self");        PyObject self = ap.getPyObject(0);        PyObject args = self.__getattr__("args");        PyObject a = args.__len__() > 0 ? args.__getitem__(0) :                                new PyString("(what)");        return new PyString("Cannot pickle %s objects").__mod__(a).__str__();    }    public cPickle() {    }    /**     * Returns a pickler instance.     * @param file      a file-like object, can be a cStringIO.StringIO,     *                  a PyFile or any python object which implements a     *                  <i>write</i> method. The data will be written as text.     * @returns a new Pickler instance.     */    public static Pickler Pickler(PyObject file) {        return new Pickler(file, false);    }    /**     * Returns a pickler instance.     * @param file      a file-like object, can be a cStringIO.StringIO,     *                  a PyFile or any python object which implements a     *                  <i>write</i> method.     * @param bin       when true, the output will be written as binary data.     * @returns         a new Pickler instance.     */    public static Pickler Pickler(PyObject file, boolean bin) {        return new Pickler(file, bin);    }    /**     * Returns a unpickler instance.     * @param file      a file-like object, can be a cStringIO.StringIO,     *                  a PyFile or any python object which implements a     *                  <i>read</i> and <i>readline</i> method.     * @returns         a new Unpickler instance.     */    public static Unpickler Unpickler(PyObject file) {        return new Unpickler(file);    }    /**     * Shorthand function which pickles the object on the file.     * @param object    a data object which should be pickled.     * @param file      a file-like object, can be a cStringIO.StringIO,     *                  a PyFile or any python object which implements a     *                  <i>write</i>  method. The data will be written as     *                  text.     * @returns         a new Unpickler instance.     */    public static void dump(PyObject object, PyObject file) {        dump(object, file, false);    }    /**     * Shorthand function which pickles the object on the file.     * @param object    a data object which should be pickled.     * @param file      a file-like object, can be a cStringIO.StringIO,     *                  a PyFile or any python object which implements a     *                  <i>write</i> method.     * @param bin       when true, the output will be written as binary data.     * @returns         a new Unpickler instance.     */    public static void dump(PyObject object, PyObject file, boolean bin) {        new Pickler(file, bin).dump(object);    }    /**     * Shorthand function which pickles and returns the string representation.     * @param object    a data object which should be pickled.     * @returns         a string representing the pickled object.     */    public static String dumps(PyObject object) {        return dumps(object, false);    }    /**     * Shorthand function which pickles and returns the string representation.     * @param object    a data object which should be pickled.     * @param bin       when true, the output will be written as binary data.     * @returns         a string representing the pickled object.     */    public static String dumps(PyObject object, boolean bin) {        cStringIO.StringIO file = cStringIO.StringIO();        dump(object, file, bin);        return file.getvalue();    }    /**     * Shorthand function which unpickles a object from the file and returns     * the new object.     * @param file      a file-like object, can be a cStringIO.StringIO,     *                  a PyFile or any python object which implements a     *                  <i>read</i> and <i>readline</i> method.     * @returns         a new object.     */    public static Object load(PyObject file) {        return new Unpickler(file).load();    }    /**     * Shorthand function which unpickles a object from the string and     * returns the new object.     * @param str       a strings which must contain a pickled object     *                  representation.     * @returns         a new object.     */    public static Object loads(PyObject str) {        cStringIO.StringIO file = cStringIO.StringIO(str.toString());        return new Unpickler(file).load();    }    // Factory for creating IOFile representation.    private static IOFile createIOFile(PyObject file) {        Object f = file.__tojava__(cStringIO.StringIO.class);        if (f != Py.NoConversion)            return new cStringIOFile((cStringIO.StringIO)file);        else if (__builtin__.isinstance(file, FileType))            return new FileIOFile(file);        else            return new ObjectIOFile(file);    }    // IOFiles encapsulates and optimise access to the different file    // representation.    interface IOFile {        public abstract void write(String str);        // Usefull optimization since most data written are chars.        public abstract void write(char str);        public abstract void flush();        public abstract String read(int len);        // Usefull optimization since all readlines removes the        // trainling newline.        public abstract String readlineNoNl();    }    // Use a cStringIO as a file.    static class cStringIOFile implements IOFile {        cStringIO.StringIO file;        cStringIOFile(PyObject file) {            this.file = (cStringIO.StringIO)file.__tojava__(Object.class);        }        public void write(String str) {            file.write(str);        }        public void write(char ch) {            file.writeChar(ch);        }        public void flush() {}        public String read(int len) {            return file.read(len);        }        public String readlineNoNl() {            return file.readlineNoNl();        }    }    // Use a PyFile as a file.    static class FileIOFile implements IOFile {        PyFile file;        FileIOFile(PyObject file) {            this.file = (PyFile)file.__tojava__(PyFile.class);            if (this.file.closed)                throw Py.ValueError("I/O operation on closed file");        }        public void write(String str) {            file.write(str);        }        public void write(char ch) {            file.write(cStringIO.getString(ch));        }        public void flush() {}        public String read(int len) {            return file.read(len).toString();        }        public String readlineNoNl() {            String line = file.readline().toString();            return line.substring(0, line.length()-1);        }    }    // Use any python object as a file.    static class ObjectIOFile implements IOFile {        char[] charr = new char[1];        StringBuffer buff = new StringBuffer();        PyObject write;        PyObject read;        PyObject readline;        final int BUF_SIZE = 256;        ObjectIOFile(PyObject file) {//          this.file = file;            write = file.__findattr__("write");            read = file.__findattr__("read");            readline = file.__findattr__("readline");        }        public void write(String str) {            buff.append(str);            if (buff.length() > BUF_SIZE)                flush();        }        public void write(char ch) {            buff.append(ch);            if (buff.length() > BUF_SIZE)                flush();        }        public void flush() {            write.__call__(new PyString(buff.toString()));            buff.setLength(0);        }        public String read(int len) {            return read.__call__(new PyInteger(len)).toString();        }        public String readlineNoNl() {            String line = readline.__call__().toString();            return line.substring(0, line.length()-1);        }    }    /**     * The Pickler object

⌨️ 快捷键说明

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