Class KryoNamespace

  • All Implemented Interfaces:
    com.esotericsoftware.kryo.pool.KryoFactory, com.esotericsoftware.kryo.pool.KryoPool

    public final class KryoNamespace
    extends java.lang.Object
    implements com.esotericsoftware.kryo.pool.KryoFactory, com.esotericsoftware.kryo.pool.KryoPool
    Pool of Kryo instances, with classes pre-registered.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  KryoNamespace.Builder
      KryoNamespace builder.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int DEFAULT_BUFFER_SIZE
      Default buffer size used for serialization.
      static int FLOATING_ID
      ID to use if this KryoNamespace does not define registration id.
      static int INITIAL_ID
      Smallest ID free to use for user defined registrations.
      static int MAX_BUFFER_SIZE  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      com.esotericsoftware.kryo.Kryo borrow()  
      com.esotericsoftware.kryo.Kryo create()
      Creates a Kryo instance.
      <T> T deserialize​(byte[] bytes)
      Deserializes given byte array to Object using Kryo instance in pool.
      <T> T deserialize​(java.io.InputStream stream)
      Deserializes given InputStream to an Object using Kryo instance in pool.
      <T> T deserialize​(java.io.InputStream stream, int bufferSize)
      Deserializes given InputStream to an Object using Kryo instance in pool.
      <T> T deserialize​(java.nio.ByteBuffer buffer)
      Deserializes given byte buffer to Object using Kryo instance in pool.
      static KryoNamespace.Builder newBuilder()
      Creates a new KryoNamespace builder.
      KryoNamespace populate​(int instances)
      Populates the Kryo pool.
      void release​(com.esotericsoftware.kryo.Kryo kryo)  
      <T> T run​(com.esotericsoftware.kryo.pool.KryoCallback<T> callback)  
      byte[] serialize​(java.lang.Object obj)
      Serializes given object to byte array using Kryo instance in pool.
      byte[] serialize​(java.lang.Object obj, int bufferSize)
      Serializes given object to byte array using Kryo instance in pool.
      void serialize​(java.lang.Object obj, java.io.OutputStream stream)
      Serializes given object to OutputStream using Kryo instance in pool.
      void serialize​(java.lang.Object obj, java.io.OutputStream stream, int bufferSize)
      Serializes given object to OutputStream using Kryo instance in pool.
      void serialize​(java.lang.Object obj, java.nio.ByteBuffer buffer)
      Serializes given object to byte buffer using Kryo instance in pool.
      int size()
      Gets the number of classes registered in this Kryo namespace.
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

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

      • FLOATING_ID

        public static final int FLOATING_ID
        ID to use if this KryoNamespace does not define registration id.
        See Also:
        Constant Field Values
      • INITIAL_ID

        public static final int INITIAL_ID
        Smallest ID free to use for user defined registrations.
        See Also:
        Constant Field Values
    • Method Detail

      • populate

        public KryoNamespace populate​(int instances)
        Populates the Kryo pool.
        Parameters:
        instances - to add to the pool
        Returns:
        this
      • serialize

        public byte[] serialize​(java.lang.Object obj)
        Serializes given object to byte array using Kryo instance in pool.

        Note: Serialized bytes must be smaller than MAX_BUFFER_SIZE.

        Parameters:
        obj - Object to serialize
        Returns:
        serialized bytes
      • serialize

        public byte[] serialize​(java.lang.Object obj,
                                int bufferSize)
        Serializes given object to byte array using Kryo instance in pool.
        Parameters:
        obj - Object to serialize
        bufferSize - maximum size of serialized bytes
        Returns:
        serialized bytes
      • serialize

        public void serialize​(java.lang.Object obj,
                              java.nio.ByteBuffer buffer)
        Serializes given object to byte buffer using Kryo instance in pool.
        Parameters:
        obj - Object to serialize
        buffer - to write to
      • serialize

        public void serialize​(java.lang.Object obj,
                              java.io.OutputStream stream)
        Serializes given object to OutputStream using Kryo instance in pool.
        Parameters:
        obj - Object to serialize
        stream - to write to
      • serialize

        public void serialize​(java.lang.Object obj,
                              java.io.OutputStream stream,
                              int bufferSize)
        Serializes given object to OutputStream using Kryo instance in pool.
        Parameters:
        obj - Object to serialize
        stream - to write to
        bufferSize - size of the buffer in front of the stream
      • deserialize

        public <T> T deserialize​(byte[] bytes)
        Deserializes given byte array to Object using Kryo instance in pool.
        Type Parameters:
        T - deserialized Object type
        Parameters:
        bytes - serialized bytes
        Returns:
        deserialized Object
      • deserialize

        public <T> T deserialize​(java.nio.ByteBuffer buffer)
        Deserializes given byte buffer to Object using Kryo instance in pool.
        Type Parameters:
        T - deserialized Object type
        Parameters:
        buffer - input with serialized bytes
        Returns:
        deserialized Object
      • deserialize

        public <T> T deserialize​(java.io.InputStream stream)
        Deserializes given InputStream to an Object using Kryo instance in pool.
        Type Parameters:
        T - deserialized Object type
        Parameters:
        stream - input stream
        Returns:
        deserialized Object
      • deserialize

        public <T> T deserialize​(java.io.InputStream stream,
                                 int bufferSize)
        Deserializes given InputStream to an Object using Kryo instance in pool.
        Type Parameters:
        T - deserialized Object type
        Parameters:
        stream - input stream
        bufferSize - size of the buffer in front of the stream
        Returns:
        deserialized Object
      • size

        public int size()
        Gets the number of classes registered in this Kryo namespace.
        Returns:
        size of namespace
      • create

        public com.esotericsoftware.kryo.Kryo create()
        Creates a Kryo instance.
        Specified by:
        create in interface com.esotericsoftware.kryo.pool.KryoFactory
        Returns:
        Kryo instance
      • borrow

        public com.esotericsoftware.kryo.Kryo borrow()
        Specified by:
        borrow in interface com.esotericsoftware.kryo.pool.KryoPool
      • release

        public void release​(com.esotericsoftware.kryo.Kryo kryo)
        Specified by:
        release in interface com.esotericsoftware.kryo.pool.KryoPool
      • run

        public <T> T run​(com.esotericsoftware.kryo.pool.KryoCallback<T> callback)
        Specified by:
        run in interface com.esotericsoftware.kryo.pool.KryoPool
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object