Class FSTConfiguration

java.lang.Object
org.nustaq.serialization.FSTConfiguration

public class FSTConfiguration extends Object
Created with IntelliJ IDEA. User: ruedi Date: 18.11.12 Time: 20:41 Holds a serialization configuration/metadata. Reuse this class !!! construction is very expensive. (just keep static instances around or use thread locals)
  • Field Details

    • isAndroid

      public static boolean isAndroid
  • Constructor Details

  • Method Details

    • getJsonFieldNames

      public FSTJsonFieldNames getJsonFieldNames()
    • setJsonFieldNames

      public void setJsonFieldNames(FSTJsonFieldNames fieldNames)
    • getVerifier

    • setVerifier

    • getIntToObjectMapFactory

      public FSTInt2ObjectMapFactory getIntToObjectMapFactory()
    • getName

      public String getName()
      debug helper
      Returns:
    • setName

      public void setName(String name)
      debug helper
      Parameters:
      name -
    • getDefaultConfiguration

      public static FSTConfiguration getDefaultConfiguration()
    • createMinBinConfiguration

      public static FSTConfiguration createMinBinConfiguration()
      Warning: MinBin contains full metainformation (fieldnames,..), so its way slower than the other configs. It should be used in case of cross language (e.g. java - javascript) serialization only. Additionally you can read MinBin serialized streams without access to original classes. See MBPrinter on an example on how to read a MinBin stream without having access to original classes. Useful for cross language serialization/long term archiving. Warning: MinBin serialization ('binary JSon') is much slower than the other serialization configurations.
      Returns:
      a configuration to encode MinBin format.
    • createMinBinConfiguration

      protected static FSTConfiguration createMinBinConfiguration(ConcurrentHashMap<org.nustaq.serialization.FSTConfiguration.FieldKey,FSTClazzInfo.FSTFieldInfo> shared)
    • createJsonNoRefConfiguration

      public static FSTConfiguration createJsonNoRefConfiguration()
      Returns:
      a configuration encoding to JSon without support for reference sharing (=> NO cyclic object graphs)
    • createJsonConfiguration

      public static FSTConfiguration createJsonConfiguration(boolean prettyPrint, boolean shareReferences)
      create a json conf with given attributes. Note that shared refs = true for jason might be not as stable as for binary encodings as fst relies on stream positions to identify objects within a given input, so any inbetween formatting will break proper reference resolution. WARNING: use of sharedrefs = true is Deprecated as its flakey
      Parameters:
      prettyPrint -
      shareReferences -
      Returns:
    • createJsonConfiguration

      public static FSTConfiguration createJsonConfiguration()
    • createJsonConfiguration

      protected static FSTConfiguration createJsonConfiguration(boolean prettyPrint, boolean shareReferences, ConcurrentHashMap<org.nustaq.serialization.FSTConfiguration.FieldKey,FSTClazzInfo.FSTFieldInfo> shared)
    • prettyPrintJson

      public static void prettyPrintJson(Object o)
      debug only, very slow (creates config with each call). Creates new conf so custom serializers are ignored.
      Parameters:
      o -
    • createAndroidDefaultConfiguration

      public static FSTConfiguration createAndroidDefaultConfiguration()
      Configuration for use on Android. Its binary compatible with getDefaultConfiguration(). So one can write on server with getDefaultConf and read on mobile client with getAndroidConf().
      Returns:
    • createAndroidDefaultConfiguration

      protected static FSTConfiguration createAndroidDefaultConfiguration(ConcurrentHashMap<org.nustaq.serialization.FSTConfiguration.FieldKey,FSTClazzInfo.FSTFieldInfo> shared)
    • createConfiguration

      public static FSTConfiguration createConfiguration(org.nustaq.serialization.FSTConfiguration.ConfType ct, boolean shareRefs)
    • createConfiguration

      protected static FSTConfiguration createConfiguration(org.nustaq.serialization.FSTConfiguration.ConfType ct, boolean shareRefs, ConcurrentHashMap<org.nustaq.serialization.FSTConfiguration.FieldKey,FSTClazzInfo.FSTFieldInfo> shared)
    • createDefaultConfiguration

      public static FSTConfiguration createDefaultConfiguration()
      the standard FSTConfiguration. - safe (no unsafe r/w) - platform independent byte order - moderate compression note that if you are just read/write from/to byte arrays, its faster to use DefaultCoder. This should be used most of the time.
      Returns:
    • createDefaultConfiguration

      protected static FSTConfiguration createDefaultConfiguration(ConcurrentHashMap<org.nustaq.serialization.FSTConfiguration.FieldKey,FSTClazzInfo.FSTFieldInfo> shared)
    • initDefaultFstConfigurationInternal

      protected static FSTConfiguration initDefaultFstConfigurationInternal(FSTConfiguration conf)
    • registerIntToObjectMapFactory

      public void registerIntToObjectMapFactory(FSTInt2ObjectMapFactory intToObjectMapFactory)
    • createUnsafeBinaryConfiguration

      public static FSTConfiguration createUnsafeBinaryConfiguration()
      Returns a configuration using Unsafe to read write data. - platform dependent byte order - no value compression attempts - makes heavy use of Unsafe, which can be dangerous in case of version conflicts Use only in case it makes a significant difference and you absolutely need the performance gain. Performance gains depend on data. There are cases where this is even slower, in some scenarios (many native arrays) it can be several times faster. see also OffHeapCoder, OnHeapCoder.
    • createUnsafeBinaryConfiguration

      protected static FSTConfiguration createUnsafeBinaryConfiguration(ConcurrentHashMap<org.nustaq.serialization.FSTConfiguration.FieldKey,FSTClazzInfo.FSTFieldInfo> shared)
    • registerSerializer

      public void registerSerializer(Class clazz, FSTObjectSerializer ser, boolean alsoForAllSubclasses)
      register a custom serializer for a given class or the class and all of its subclasses. Serializers must be configured identical on read/write side and should be set before actually making use of the Configuration.
      Parameters:
      clazz -
      ser -
      alsoForAllSubclasses -
    • isForceClzInit

      public boolean isForceClzInit()
    • getLastResortResolver

      public FSTConfiguration.LastResortClassResolver getLastResortResolver()
    • setLastResortResolver

      public void setLastResortResolver(FSTConfiguration.LastResortClassResolver lastResortResolver)
    • setForceClzInit

      public FSTConfiguration setForceClzInit(boolean forceClzInit)
      always execute default fields init, even if no transients (so would get overwritten anyway) required for lossy codecs (kson)
      Parameters:
      forceClzInit -
      Returns:
    • getInstantiator

      public FSTClassInstantiator getInstantiator(Class clazz)
    • setInstantiator

      public void setInstantiator(FSTClassInstantiator instantiator)
    • getCoderSpecific

      public <T> T getCoderSpecific()
    • setCoderSpecific

      public void setCoderSpecific(Object coderSpecific)
    • setClassLoader

      public void setClassLoader(ClassLoader classLoader)
    • createStructConfiguration

      public static FSTConfiguration createStructConfiguration()
      special configuration used internally for struct emulation
      Returns:
    • getStreamCoderFactory

      public FSTConfiguration.StreamCoderFactory getStreamCoderFactory()
    • setStreamCoderFactory

      public void setStreamCoderFactory(FSTConfiguration.StreamCoderFactory streamCoderFactory)
      allows to use subclassed stream codecs. Can also be used to change class loading behaviour, as clasForName is part of a codec's interface. e.g. new StreamCoderFactory() {
      Parameters:
      streamCoderFactory -
    • returnObject

      public void returnObject(Object cached)
      reuse heavy weight objects. If a FSTStream is closed, objects are returned and can be reused by new stream instances. the objects are held in soft references, so there should be no memory issues. FIXME: point of contention !
      Parameters:
      cached -
    • isPreferSpeed

      public boolean isPreferSpeed()
      ignored currently
      Returns:
    • setPreferSpeed

      public void setPreferSpeed(boolean preferSpeed)
      this options lets FST favour speed of encoding over size of the encoded object. Warning: this option alters the format of the written stream, so both reader and writer should have the same setting, else exceptions will occur
      Parameters:
      preferSpeed -
    • calcObjectSizeBytesNotAUtility

      public int calcObjectSizeBytesNotAUtility(Object obj) throws IOException
      for optimization purposes, do not use to benchmark processing time or in a regular program as this methods creates a temporary binaryoutputstream and serializes the object in order to measure the size.
      Throws:
      IOException
    • setSerializerRegistryDelegate

      public void setSerializerRegistryDelegate(FSTSerializerRegistryDelegate del)
      patch default serializer lookup. set to null to delete. Should be set prior to any serialization going on (serializer lookup is cached).
      Parameters:
      del -
    • getCachedObject

      public Object getCachedObject(Class cl)
    • isForceSerializable

      public boolean isForceSerializable()
    • setForceSerializable

      public FSTConfiguration setForceSerializable(boolean forceSerializable)
      treat unserializable classes same as if they would be serializable.
      Parameters:
      forceSerializable - //
    • clearGlobalCaches

      public static void clearGlobalCaches()
      clear global deduplication caches. Useful for class reloading scenarios, else counter productive as j.reflect.Fiwld + Construtors will be instantiated more than once per class.
    • clearCaches

      public void clearCaches()
      clear cached softref's and ThreadLocal.
    • isShareReferences

      public boolean isShareReferences()
    • setShareReferences

      public void setShareReferences(boolean shareReferences)
      if false, identical objects will get serialized twice. Gains speed as long there are no double objects/cyclic references (typical for small snippets as used in e.g. RPC) Cycles and Objects referenced more than once will not be detected (if set to false). Additionally JDK compatibility is not supported (read/writeObject and stuff). Use case is highperformance serialization of plain cycle free data (e.g. messaging). Can perform significantly faster (20-40%).
      Parameters:
      shareReferences -
    • registerClass

      public void registerClass(Class... c)
      Preregister a class (use at init time). This avoids having to write class names. Its a very simple and effective optimization (frequently > 2 times faster for small objects). Read and write side need to have classes preregistered in the exact same order. The list does not have to be complete. Just add your most frequently serialized classes here to get significant gains in speed and smaller serialized representation size.
    • getClassRegistry

      public FSTClazzNameRegistry getClassRegistry()
    • getCLInfoRegistry

      public FSTClazzInfoRegistry getCLInfoRegistry()
    • getClassLoader

      public ClassLoader getClassLoader()
    • getClassInfo

      public FSTClazzInfo getClassInfo(Class type)
    • getObjectInput

      public FSTObjectInput getObjectInput(InputStream in)
      utility for thread safety and reuse. Do not close the resulting stream. However you should close the given InputStream 'in'
      Parameters:
      in -
      Returns:
    • getObjectInput

      public FSTObjectInput getObjectInput()
    • getObjectInput

      public FSTObjectInput getObjectInput(byte[] arr)
      take the given array as input. the array is NOT copied. WARNING: the input streams takes over ownership and might overwrite content of this array in subsequent IO operations.
      Parameters:
      arr -
      Returns:
    • getObjectInput

      public FSTObjectInput getObjectInput(byte[] arr, int len)
      take the given array as input. the array is NOT copied. WARNING: the input streams takes over ownership and might overwrite content of this array in subsequent IO operations.
      Parameters:
      arr -
      len -
      Returns:
    • getObjectInputCopyFrom

      public FSTObjectInput getObjectInputCopyFrom(byte[] arr, int off, int len)
      take the given array and copy it to input. the array IS copied
      Parameters:
      arr -
      len -
      Returns:
    • getIn

      protected FSTObjectInput getIn()
    • getOut

      protected FSTObjectOutput getOut()
    • getObjectOutput

      public FSTObjectOutput getObjectOutput(OutputStream out)
      utility for thread safety and reuse. Do not close the resulting stream. However you should close the given OutputStream 'out'
      Parameters:
      out - - can be null (temp bytearrays stream is created then)
      Returns:
    • getObjectOutput

      public FSTObjectOutput getObjectOutput()
      Returns:
      a recycled outputstream reusing its last recently used byte[] buffer
    • getObjectOutput

      public FSTObjectOutput getObjectOutput(byte[] outByte)
    • setStructMode

      public void setStructMode(boolean ignoreSerialInterfaces)
      ignores all serialization related interfaces (Serializable, Externalizable) and serializes all classes using the default scheme. Warning: this is a special mode of operation which fail serializing/deserializing many standard JDK classes.
      Parameters:
      ignoreSerialInterfaces -
    • isStructMode

      public boolean isStructMode()
      special for structs
      Returns:
    • getClazzInfo

      public FSTClazzInfo getClazzInfo(Class rowClass)
    • setCrossPlatform

      public void setCrossPlatform(boolean crossPlatform)
    • isCrossPlatform

      public boolean isCrossPlatform()
    • deepCopy

      public <T> T deepCopy(T metadata)
    • createStreamEncoder

      public FSTEncoder createStreamEncoder()
    • createStreamDecoder

      public FSTDecoder createStreamDecoder()
    • registerCrossPlatformClassBinaryCache

      public void registerCrossPlatformClassBinaryCache(String fulLQName, byte[] binary)
    • getCrossPlatformBinaryCache

      public byte[] getCrossPlatformBinaryCache(String symbolicName)
    • registerCrossPlatformClassMapping

      public FSTConfiguration registerCrossPlatformClassMapping(String[][] keysAndVals)
      init right after creation of configuration, not during operation as it is not threadsafe regarding mutation currently only for minbin serialization
      Parameters:
      keysAndVals - { { "symbolicName", "fullQualifiedClazzName" }, .. }
    • registerCrossPlatformClassMapping

      public FSTConfiguration registerCrossPlatformClassMapping(String shortName, String fqName)
    • cpMap

      public FSTConfiguration cpMap(String shortName, Class clz)
      shorthand for registerCrossPlatformClassMapping(_,_)
      Parameters:
      shortName - - class name in json type field
      clz - - class
      Returns:
    • registerCrossPlatformClassMappingUseSimpleName

      public FSTConfiguration registerCrossPlatformClassMappingUseSimpleName(Class... classes)
      init right after creation of configuration, not during operation as it is not threadsafe regarding mutation
    • registerCrossPlatformClassMappingUseSimpleName

      public FSTConfiguration registerCrossPlatformClassMappingUseSimpleName(List<Class> classes)
    • getCPNameForClass

      public String getCPNameForClass(Class cl)
      get cross platform symbolic class identifier
      Parameters:
      cl -
      Returns:
    • getClassForCPName

      public String getClassForCPName(String name)
    • asObject

      public Object asObject(byte[] b)
      convenience
    • asByteArray

      public byte[] asByteArray(Object object)
      convenience. (object must be serializable)
    • asSharedByteArray

      public byte[] asSharedByteArray(Object object, int[] length)
      Warning: avoids allocation + copying. The returned byteArray is a direct pointer to underlying buffer. the int length[] is expected to have at least on element. The buffer can be larger than written data, therefore length[0] will contain written length. The buffer content must be used (e.g. sent to network, copied to offheap) before doing another asByteArray on the current Thread.
    • asJsonString

      public String asJsonString(Object o)
      utility/debug method. Use "asByteArray" for programmatic use as the byte array will already by UTF-8 and ready to be sent on network.
      Parameters:
      o -
      Returns:
    • encodeToStream

      public void encodeToStream(OutputStream out, Object toSerialize) throws IOException
      helper to write series of objects to streams/files > Integer.MAX_VALUE. it - serializes the object - writes the length of the serialized object to the stream - the writes the serialized object data on reader side (e.g. from a blocking socketstream, the reader then - reads the length - reads [length] bytes from the stream - deserializes
      Parameters:
      out -
      toSerialize -
      Throws:
      IOException
      See Also:
    • decodeFromStream

      public Object decodeFromStream(InputStream in) throws Exception
      Parameters:
      in -
      Returns:
      Throws:
      Exception
      See Also:
    • deriveConfiguration

      public FSTConfiguration deriveConfiguration()
      Returns:
      a configzration sharing as much as possible state with the callee. Its valid to register different serializers at the derived configzration obtained.
    • toString

      public String toString()
      Overrides:
      toString in class Object