How to unpickle a file in Java?

旧街凉风 提交于 2021-02-18 19:45:15

问题


I have a 'pickle' format datafile that was created by a Python process (actually, the MCDungeon cache file). I want to read this from a Java program.

To do this, I have used this code:

    public HashMap<String, Object> getDataFileStream(String filename) {
        HashMap<String, Object> data = new HashMap<String, Object>();
        File f = new File(filename);
        InputStream fs = null;
        try {
            fs = new FileInputStream(f);
        } catch (FileNotFoundException e) {
            Log.warning("Pickle file '"+filename+"' not found!");
            return null;
        }

        PyFile picklefile = new PyFile(fs);    
        PyDictionary phash = null;
        try {
            phash = (PyDictionary) cPickle.load(picklefile);        
        } catch ( PyException e3 ) {
            log.severe("Cannot unpickle! (Python error)");
            e3.printStackTrace(); // throws a null pointer exception
            return null;    
        } catch ( Exception e ) {
            log.severe("Cannot unpickle! Err: " + e.getClass().getName());
            e.printStackTrace();
            return null;
        }
    ConcurrentMap<PyObject, PyObject> aMap = phash.getMap();
    for (Map.Entry<PyObject, PyObject> entry : aMap.entrySet()) {
        String keyval = entry.getKey().toString();
        PyObject tileentity = (PyList) entry.getValue();
        try {
            data.put(keyval, pythonToJava(tileentity));
        } catch (Exception e) {
            data.put(keyval, tileentity);
        }
    }
    return data;
}

I have included the JYthon library, and also the pythonToJava function (elsewhere).

The file I am passing is definitely a valid Picklefile as it can be read by the Python process successfully.

However, when running this function, I get a PyException thrown in the cPickle.load function, and calling the printStackTrace given a NullPointer exception (the line number 71 corresponds to the e3.printStackTrace() line above)

[12:42:18 ERROR]: [DynmapMCDungeon] Cannot unpickle! (Python error)
java.lang.NullPointerException
        at org.steveshipway.dynmap.PickleLoader.getDataFileStream(PickleLoader.j
ava:71) ~[?:?]
        at org.steveshipway.dynmap.Dungeon.getDungeons(Dungeon.java:28) ~[?:?]
        at org.steveshipway.dynmap.DynmapMCDungeon.activate(DynmapMCDungeon.java
:179) ~[?:?]

When I load the Pickle data in manually and pass to the function, I get a NullPointerException error in the cPickle.load function:

[13:56:57 INFO]: [DynmapMCDungeon] Reading in MCDungeon pickle...
[13:56:57 ERROR]: [DynmapMCDungeon] Cannot unpickle the MCDungeon cache! Err: java.lang.NullPointerException
[13:56:57 WARN]: java.lang.NullPointerException
[13:56:57 WARN]:        at java.util.Objects.requireNonNull(Unknown Source)
[13:56:57 WARN]:        at java.util.Arrays$ArrayList.<init>(Unknown Source)
[13:56:57 WARN]:        at java.util.Arrays.asList(Unknown Source)
[13:56:57 WARN]:        at org.python.core.PyList.<init>(PyList.java:52)
[13:56:57 WARN]:        at org.python.core.PyList.<init>(PyList.java:64)
[13:56:57 WARN]:        at org.python.modules.cPickle$Unpickler.load_empty_list(cPickle.java:1909)
[13:56:57 WARN]:        at org.python.modules.cPickle$Unpickler.load(cPickle.java:1620)
[13:56:57 WARN]:        at org.python.modules.cPickle.load(cPickle.java:636)
[13:56:57 WARN]:        at org.steveshipway.dynmap.PickleLoader.getDataFileStream(PickleLoader.java:64)

My questions are:

  1. Why am I getting an error when I try to print the stack trace?

  2. What am I doing wrong with loading the picklefile? Is there a better way to achieve this?

Thanks in advance for any pointers (preferably not null ones, I have enough of those already!)


回答1:


To unpickle .pkl files I have used Jython. Jython has provides python2 support in Java and the pickle file should be serialized with protocol code 0,1 or 2.

{
    PythonInterpreter interpreter = new PythonInterpreter();
    interpreter.exec("Your Python code here");

    PyObject getAtrsFunc = interpreter.get("your_python_function_name");
    PyObject funcRes = getImageAtrsFunc.__call__(new PyString("Example_input_String_to_py_function"), new PyInteger(javaIntegerValue));
    //Lets say function returns integer array as an output
    //To convert output to java usable object 
    int [] resAtrs = (int []) funcRes.__tojava__(int [].class);
}

To use jython in maven project add

<dependency>
    <groupId>org.python</groupId>
    <artifactId>jython-standalone</artifactId>
    <version>2.7.0</version>
</dependency>

To your pom file




回答2:


The problem seems to be that I was using Jython 2.5.3. Upgrading to Jython 2.7.0 solved the unpickling issue (though I then get problems with coercing the Python datatypes into Java types, but that's seaprate)



来源:https://stackoverflow.com/questions/35616060/how-to-unpickle-a-file-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!