Class Unpickler


  • public class Unpickler
    extends java.lang.Object
    Unpickles an object graph from a pickle data inputstream. Supports all pickle protocol versions. Maps the python objects on the corresponding java equivalents or similar types. This class is NOT threadsafe! (Don't use the same pickler from different threads) See the README.txt for a table of the type mappings.
    Author:
    Irmen de Jong (irmen@razorvine.net)
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected int HIGHEST_PROTOCOL
      The highest Python Pickle protocol version supported by this library.
      protected java.io.InputStream input
      The stream where the pickle data is read from.
      protected java.util.Map<java.lang.Integer,​java.lang.Object> memo
      Internal cache of memoized objects.
      protected static java.lang.Object NO_RETURN_VALUE
      Used as return value for dispatch(short) in the general case (because the object graph is built on the stack)
      protected static java.util.Map<java.lang.String,​IObjectConstructor> objectConstructors
      Registry of object constructors that are used to create the appropriate Java objects for the given Python module.typename references.
      protected UnpickleStack stack
      The stack that is used for building the resulting object graph.
    • Constructor Summary

      Constructors 
      Constructor Description
      Unpickler()
      Create an unpickler.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()
      Close the unpickler and frees the resources such as the unpickle stack and memo table.
      protected java.lang.Object dispatch​(short key)
      Process a single pickle stream opcode.
      java.lang.Object load​(java.io.InputStream stream)
      Read a pickled object representation from the given input stream.
      java.lang.Object loads​(byte[] pickledata)
      Read a pickled object representation from the given pickle data bytes.
      protected java.lang.Object next_buffer()
      Buffer support for protocol 5 out of band data If you want to unpickle such pickles, you'll have to subclass the unpickler and override this method to return the buffer data you want.
      protected java.lang.Object persistentLoad​(java.lang.Object pid)
      Hook for the persistent id feature where an id is replaced externally by the appropriate object.
      static void registerConstructor​(java.lang.String module, java.lang.String classname, IObjectConstructor constructor)
      Register additional object constructors for custom classes.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • NO_RETURN_VALUE

        protected static final java.lang.Object NO_RETURN_VALUE
        Used as return value for dispatch(short) in the general case (because the object graph is built on the stack)
      • HIGHEST_PROTOCOL

        protected final int HIGHEST_PROTOCOL
        The highest Python Pickle protocol version supported by this library.
        See Also:
        Constant Field Values
      • memo

        protected java.util.Map<java.lang.Integer,​java.lang.Object> memo
        Internal cache of memoized objects.
      • stack

        protected UnpickleStack stack
        The stack that is used for building the resulting object graph.
      • input

        protected java.io.InputStream input
        The stream where the pickle data is read from.
      • objectConstructors

        protected static final java.util.Map<java.lang.String,​IObjectConstructor> objectConstructors
        Registry of object constructors that are used to create the appropriate Java objects for the given Python module.typename references.
    • Constructor Detail

      • Unpickler

        public Unpickler()
        Create an unpickler.
    • Method Detail

      • registerConstructor

        public static void registerConstructor​(java.lang.String module,
                                               java.lang.String classname,
                                               IObjectConstructor constructor)
        Register additional object constructors for custom classes.
      • load

        public java.lang.Object load​(java.io.InputStream stream)
                              throws PickleException,
                                     java.io.IOException
        Read a pickled object representation from the given input stream.
        Returns:
        the reconstituted object hierarchy specified in the file.
        Throws:
        PickleException
        java.io.IOException
      • loads

        public java.lang.Object loads​(byte[] pickledata)
                               throws PickleException,
                                      java.io.IOException
        Read a pickled object representation from the given pickle data bytes.
        Returns:
        the reconstituted object hierarchy specified in the file.
        Throws:
        PickleException
        java.io.IOException
      • close

        public void close()
        Close the unpickler and frees the resources such as the unpickle stack and memo table.
      • next_buffer

        protected java.lang.Object next_buffer()
                                        throws PickleException,
                                               java.io.IOException
        Buffer support for protocol 5 out of band data If you want to unpickle such pickles, you'll have to subclass the unpickler and override this method to return the buffer data you want.
        Throws:
        PickleException
        java.io.IOException
      • dispatch

        protected java.lang.Object dispatch​(short key)
                                     throws PickleException,
                                            java.io.IOException
        Process a single pickle stream opcode.
        Throws:
        PickleException
        java.io.IOException
      • persistentLoad

        protected java.lang.Object persistentLoad​(java.lang.Object pid)
        Hook for the persistent id feature where an id is replaced externally by the appropriate object.
        Parameters:
        pid - the persistent id from the pickle
        Returns:
        the actual object that belongs to that id. The default implementation throws a PickleException, telling you that you should implement this function yourself in a subclass of the Unpickler.