Package org.nustaq.serialization
Class FSTConfiguration
java.lang.Object
org.nustaq.serialization.FSTConfiguration
public class FSTConfiguration
extends java.lang.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)
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceFSTConfiguration.ClassSecurityVerifierSecurity: disallow packages/classes upon deserializationprotected static classFSTConfiguration.FBinaryStreamCoderFactoryprotected static classFSTConfiguration.FSTDefaultStreamCoderFactorystatic classFSTConfiguration.JacksonAccessWorkaroundprotected static classFSTConfiguration.JSonStreamCoderFactorystatic interfaceFSTConfiguration.LastResortClassResolverif all attempts fail to find a class this guy is asked.protected static classFSTConfiguration.MinBinStreamCoderFactorystatic interfaceFSTConfiguration.StreamCoderFactory -
Field Summary
Fields Modifier and Type Field Description static booleanisAndroid -
Constructor Summary
Constructors Modifier Constructor Description protectedFSTConfiguration(java.util.concurrent.ConcurrentHashMap<org.nustaq.serialization.FSTConfiguration.FieldKey,FSTClazzInfo.FSTFieldInfo> sharedFieldInfos) -
Method Summary
Modifier and Type Method Description byte[]asByteArray(java.lang.Object object)convenience.java.lang.StringasJsonString(java.lang.Object o)utility/debug method.java.lang.ObjectasObject(byte[] b)conveniencebyte[]asSharedByteArray(java.lang.Object object, int[] length)Warning: avoids allocation + copying.intcalcObjectSizeBytesNotAUtility(java.lang.Object obj)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.voidclearCaches()clear cached softref's and ThreadLocal.static voidclearGlobalCaches()clear global deduplication caches.FSTConfigurationcpMap(java.lang.String shortName, java.lang.Class clz)shorthand for registerCrossPlatformClassMapping(_,_)static FSTConfigurationcreateAndroidDefaultConfiguration()Configuration for use on Android.protected static FSTConfigurationcreateAndroidDefaultConfiguration(java.util.concurrent.ConcurrentHashMap<org.nustaq.serialization.FSTConfiguration.FieldKey,FSTClazzInfo.FSTFieldInfo> shared)static FSTConfigurationcreateConfiguration(org.nustaq.serialization.FSTConfiguration.ConfType ct, boolean shareRefs)protected static FSTConfigurationcreateConfiguration(org.nustaq.serialization.FSTConfiguration.ConfType ct, boolean shareRefs, java.util.concurrent.ConcurrentHashMap<org.nustaq.serialization.FSTConfiguration.FieldKey,FSTClazzInfo.FSTFieldInfo> shared)static FSTConfigurationcreateDefaultConfiguration()the standard FSTConfigurationprotected static FSTConfigurationcreateDefaultConfiguration(java.util.concurrent.ConcurrentHashMap<org.nustaq.serialization.FSTConfiguration.FieldKey,FSTClazzInfo.FSTFieldInfo> shared)static FSTConfigurationcreateJsonConfiguration()static FSTConfigurationcreateJsonConfiguration(boolean prettyPrint, boolean shareReferences)create a json conf with given attributes.protected static FSTConfigurationcreateJsonConfiguration(boolean prettyPrint, boolean shareReferences, java.util.concurrent.ConcurrentHashMap<org.nustaq.serialization.FSTConfiguration.FieldKey,FSTClazzInfo.FSTFieldInfo> shared)static FSTConfigurationcreateJsonNoRefConfiguration()static FSTConfigurationcreateMinBinConfiguration()Warning: MinBin contains full metainformation (fieldnames,..), so its way slower than the other configs.protected static FSTConfigurationcreateMinBinConfiguration(java.util.concurrent.ConcurrentHashMap<org.nustaq.serialization.FSTConfiguration.FieldKey,FSTClazzInfo.FSTFieldInfo> shared)FSTDecodercreateStreamDecoder()FSTEncodercreateStreamEncoder()static FSTConfigurationcreateStructConfiguration()special configuration used internally for struct emulationstatic FSTConfigurationcreateUnsafeBinaryConfiguration()Returns a configuration using Unsafe to read write dataprotected static FSTConfigurationcreateUnsafeBinaryConfiguration(java.util.concurrent.ConcurrentHashMap<org.nustaq.serialization.FSTConfiguration.FieldKey,FSTClazzInfo.FSTFieldInfo> shared)java.lang.ObjectdecodeFromStream(java.io.InputStream in)<T> TdeepCopy(T metadata)FSTConfigurationderiveConfiguration()voidencodeToStream(java.io.OutputStream out, java.lang.Object toSerialize)helper to write series of objects to streams/files > Integer.MAX_VALUE.java.lang.ObjectgetCachedObject(java.lang.Class cl)java.lang.StringgetClassForCPName(java.lang.String name)FSTClazzInfogetClassInfo(java.lang.Class type)java.lang.ClassLoadergetClassLoader()FSTClazzNameRegistrygetClassRegistry()FSTClazzInfogetClazzInfo(java.lang.Class rowClass)FSTClazzInfoRegistrygetCLInfoRegistry()<T> TgetCoderSpecific()java.lang.StringgetCPNameForClass(java.lang.Class cl)get cross platform symbolic class identifierbyte[]getCrossPlatformBinaryCache(java.lang.String symbolicName)static FSTConfigurationgetDefaultConfiguration()protected FSTObjectInputgetIn()FSTClassInstantiatorgetInstantiator(java.lang.Class clazz)FSTInt2ObjectMapFactorygetIntToObjectMapFactory()FSTJsonFieldNamesgetJsonFieldNames()FSTConfiguration.LastResortClassResolvergetLastResortResolver()java.lang.StringgetName()debug helperFSTObjectInputgetObjectInput()FSTObjectInputgetObjectInput(byte[] arr)take the given array as input. the array is NOT copied.FSTObjectInputgetObjectInput(byte[] arr, int len)take the given array as input. the array is NOT copied.FSTObjectInputgetObjectInput(java.io.InputStream in)utility for thread safety and reuse.FSTObjectInputgetObjectInputCopyFrom(byte[] arr, int off, int len)take the given array and copy it to input. the array IS copiedFSTObjectOutputgetObjectOutput()FSTObjectOutputgetObjectOutput(byte[] outByte)FSTObjectOutputgetObjectOutput(java.io.OutputStream out)utility for thread safety and reuse.protected FSTObjectOutputgetOut()FSTConfiguration.StreamCoderFactorygetStreamCoderFactory()FSTConfiguration.ClassSecurityVerifiergetVerifier()protected static FSTConfigurationinitDefaultFstConfigurationInternal(FSTConfiguration conf)booleanisCrossPlatform()booleanisForceClzInit()booleanisForceSerializable()booleanisPreferSpeed()ignored currentlybooleanisShareReferences()booleanisStructMode()special for structsstatic voidprettyPrintJson(java.lang.Object o)debug only, very slow (creates config with each call).voidregisterClass(java.lang.Class... c)Preregister a class (use at init time).voidregisterCrossPlatformClassBinaryCache(java.lang.String fulLQName, byte[] binary)FSTConfigurationregisterCrossPlatformClassMapping(java.lang.String[][] keysAndVals)init right after creation of configuration, not during operation as it is not threadsafe regarding mutation currently only for minbin serializationFSTConfigurationregisterCrossPlatformClassMapping(java.lang.String shortName, java.lang.String fqName)FSTConfigurationregisterCrossPlatformClassMappingUseSimpleName(java.lang.Class... classes)init right after creation of configuration, not during operation as it is not threadsafe regarding mutationFSTConfigurationregisterCrossPlatformClassMappingUseSimpleName(java.util.List<java.lang.Class> classes)voidregisterIntToObjectMapFactory(FSTInt2ObjectMapFactory intToObjectMapFactory)voidregisterSerializer(java.lang.Class clazz, FSTObjectSerializer ser, boolean alsoForAllSubclasses)register a custom serializer for a given class or the class and all of its subclasses.voidreturnObject(java.lang.Object cached)reuse heavy weight objects.voidsetClassLoader(java.lang.ClassLoader classLoader)voidsetCoderSpecific(java.lang.Object coderSpecific)voidsetCrossPlatform(boolean crossPlatform)FSTConfigurationsetForceClzInit(boolean forceClzInit)always execute default fields init, even if no transients (so would get overwritten anyway) required for lossy codecs (kson)FSTConfigurationsetForceSerializable(boolean forceSerializable)treat unserializable classes same as if they would be serializable.voidsetInstantiator(FSTClassInstantiator instantiator)voidsetJsonFieldNames(FSTJsonFieldNames fieldNames)voidsetLastResortResolver(FSTConfiguration.LastResortClassResolver lastResortResolver)voidsetName(java.lang.String name)debug helpervoidsetPreferSpeed(boolean preferSpeed)this options lets FST favour speed of encoding over size of the encoded object.voidsetSerializerRegistryDelegate(FSTSerializerRegistryDelegate del)patch default serializer lookup. set to null to delete.voidsetShareReferences(boolean shareReferences)if false, identical objects will get serialized twice.voidsetStreamCoderFactory(FSTConfiguration.StreamCoderFactory streamCoderFactory)allows to use subclassed stream codecs.voidsetStructMode(boolean ignoreSerialInterfaces)ignores all serialization related interfaces (Serializable, Externalizable) and serializes all classes using the default scheme.FSTConfigurationsetVerifier(FSTConfiguration.ClassSecurityVerifier verifier)java.lang.StringtoString()
-
Field Details
-
isAndroid
public static boolean isAndroid
-
-
Constructor Details
-
FSTConfiguration
protected FSTConfiguration(java.util.concurrent.ConcurrentHashMap<org.nustaq.serialization.FSTConfiguration.FieldKey,FSTClazzInfo.FSTFieldInfo> sharedFieldInfos)
-
-
Method Details
-
getJsonFieldNames
-
setJsonFieldNames
-
getVerifier
-
setVerifier
-
getIntToObjectMapFactory
-
getName
public java.lang.String getName()debug helper- Returns:
-
setName
public void setName(java.lang.String name)debug helper- Parameters:
name-
-
getDefaultConfiguration
-
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(java.util.concurrent.ConcurrentHashMap<org.nustaq.serialization.FSTConfiguration.FieldKey,FSTClazzInfo.FSTFieldInfo> shared) -
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
-
createJsonConfiguration
protected static FSTConfiguration createJsonConfiguration(boolean prettyPrint, boolean shareReferences, java.util.concurrent.ConcurrentHashMap<org.nustaq.serialization.FSTConfiguration.FieldKey,FSTClazzInfo.FSTFieldInfo> shared) -
prettyPrintJson
public static void prettyPrintJson(java.lang.Object o)debug only, very slow (creates config with each call). Creates new conf so custom serializers are ignored.- Parameters:
o-
-
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(java.util.concurrent.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, java.util.concurrent.ConcurrentHashMap<org.nustaq.serialization.FSTConfiguration.FieldKey,FSTClazzInfo.FSTFieldInfo> shared) -
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(java.util.concurrent.ConcurrentHashMap<org.nustaq.serialization.FSTConfiguration.FieldKey,FSTClazzInfo.FSTFieldInfo> shared) -
initDefaultFstConfigurationInternal
-
registerIntToObjectMapFactory
-
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(java.util.concurrent.ConcurrentHashMap<org.nustaq.serialization.FSTConfiguration.FieldKey,FSTClazzInfo.FSTFieldInfo> shared) -
registerSerializer
public void registerSerializer(java.lang.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
-
setLastResortResolver
-
setForceClzInit
always execute default fields init, even if no transients (so would get overwritten anyway) required for lossy codecs (kson)- Parameters:
forceClzInit-- Returns:
-
getInstantiator
-
setInstantiator
-
getCoderSpecific
public <T> T getCoderSpecific() -
setCoderSpecific
public void setCoderSpecific(java.lang.Object coderSpecific) -
setClassLoader
public void setClassLoader(java.lang.ClassLoader classLoader) -
createStructConfiguration
special configuration used internally for struct emulation- Returns:
-
getStreamCoderFactory
-
setStreamCoderFactory
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(java.lang.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(java.lang.Object obj) throws java.io.IOExceptionfor 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:
java.io.IOException
-
setSerializerRegistryDelegate
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 java.lang.Object getCachedObject(java.lang.Class cl) -
isForceSerializable
public boolean isForceSerializable() -
setForceSerializable
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(java.lang.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
-
getCLInfoRegistry
-
getClassLoader
public java.lang.ClassLoader getClassLoader() -
getClassInfo
-
getObjectInput
utility for thread safety and reuse. Do not close the resulting stream. However you should close the given InputStream 'in'- Parameters:
in-- Returns:
-
getObjectInput
-
getObjectInput
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
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
take the given array and copy it to input. the array IS copied- Parameters:
arr-len-- Returns:
-
getIn
-
getOut
-
getObjectOutput
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
- Returns:
- a recycled outputstream reusing its last recently used byte[] buffer
-
getObjectOutput
-
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
-
setCrossPlatform
public void setCrossPlatform(boolean crossPlatform) -
isCrossPlatform
public boolean isCrossPlatform() -
deepCopy
public <T> T deepCopy(T metadata) -
createStreamEncoder
-
createStreamDecoder
-
registerCrossPlatformClassBinaryCache
public void registerCrossPlatformClassBinaryCache(java.lang.String fulLQName, byte[] binary) -
getCrossPlatformBinaryCache
public byte[] getCrossPlatformBinaryCache(java.lang.String symbolicName) -
registerCrossPlatformClassMapping
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(java.lang.String shortName, java.lang.String fqName) -
cpMap
shorthand for registerCrossPlatformClassMapping(_,_)- Parameters:
shortName- - class name in json type fieldclz- - class- Returns:
-
registerCrossPlatformClassMappingUseSimpleName
init right after creation of configuration, not during operation as it is not threadsafe regarding mutation -
registerCrossPlatformClassMappingUseSimpleName
public FSTConfiguration registerCrossPlatformClassMappingUseSimpleName(java.util.List<java.lang.Class> classes) -
getCPNameForClass
public java.lang.String getCPNameForClass(java.lang.Class cl)get cross platform symbolic class identifier- Parameters:
cl-- Returns:
-
getClassForCPName
public java.lang.String getClassForCPName(java.lang.String name) -
asObject
public java.lang.Object asObject(byte[] b)convenience -
asByteArray
public byte[] asByteArray(java.lang.Object object)convenience. (object must be serializable) -
asSharedByteArray
public byte[] asSharedByteArray(java.lang.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 java.lang.String asJsonString(java.lang.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(java.io.OutputStream out, java.lang.Object toSerialize) throws java.io.IOExceptionhelper 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:
java.io.IOException- See Also:
decodeFromStream(java.io.InputStream)
-
decodeFromStream
public java.lang.Object decodeFromStream(java.io.InputStream in) throws java.lang.Exception- Parameters:
in-- Returns:
- Throws:
java.lang.Exception- See Also:
encodeToStream(java.io.OutputStream,java.lang.Object)
-
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 java.lang.String toString()- Overrides:
toStringin classjava.lang.Object
-