Package org.nustaq.serialization
Class FSTConfiguration
java.lang.Object
org.nustaq.serialization.FSTConfiguration
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
Modifier and TypeClassDescriptionstatic interface
Security: disallow packages/classes upon deserializationprotected static class
protected static class
static class
protected static class
static interface
if all attempts fail to find a class this guy is asked.protected static class
static interface
-
Field Summary
-
Constructor Summary
ModifierConstructorDescriptionprotected
FSTConfiguration
(ConcurrentHashMap<org.nustaq.serialization.FSTConfiguration.FieldKey, FSTClazzInfo.FSTFieldInfo> sharedFieldInfos) -
Method Summary
Modifier and TypeMethodDescriptionbyte[]
asByteArray
(Object object) convenience.utility/debug method.asObject
(byte[] b) conveniencebyte[]
asSharedByteArray
(Object object, int[] length) Warning: avoids allocation + copying.int
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.void
clear cached softref's and ThreadLocal.static void
clear global deduplication caches.shorthand for registerCrossPlatformClassMapping(_,_)static FSTConfiguration
Configuration for use on Android.protected static FSTConfiguration
createAndroidDefaultConfiguration
(ConcurrentHashMap<org.nustaq.serialization.FSTConfiguration.FieldKey, FSTClazzInfo.FSTFieldInfo> shared) static FSTConfiguration
createConfiguration
(org.nustaq.serialization.FSTConfiguration.ConfType ct, boolean shareRefs) protected static FSTConfiguration
createConfiguration
(org.nustaq.serialization.FSTConfiguration.ConfType ct, boolean shareRefs, ConcurrentHashMap<org.nustaq.serialization.FSTConfiguration.FieldKey, FSTClazzInfo.FSTFieldInfo> shared) static FSTConfiguration
the standard FSTConfigurationprotected static FSTConfiguration
createDefaultConfiguration
(ConcurrentHashMap<org.nustaq.serialization.FSTConfiguration.FieldKey, FSTClazzInfo.FSTFieldInfo> shared) static FSTConfiguration
static FSTConfiguration
createJsonConfiguration
(boolean prettyPrint, boolean shareReferences) create a json conf with given attributes.protected static FSTConfiguration
createJsonConfiguration
(boolean prettyPrint, boolean shareReferences, ConcurrentHashMap<org.nustaq.serialization.FSTConfiguration.FieldKey, FSTClazzInfo.FSTFieldInfo> shared) static FSTConfiguration
static FSTConfiguration
Warning: MinBin contains full metainformation (fieldnames,..), so its way slower than the other configs.protected static FSTConfiguration
createMinBinConfiguration
(ConcurrentHashMap<org.nustaq.serialization.FSTConfiguration.FieldKey, FSTClazzInfo.FSTFieldInfo> shared) static FSTConfiguration
special configuration used internally for struct emulationstatic FSTConfiguration
Returns a configuration using Unsafe to read write dataprotected static FSTConfiguration
createUnsafeBinaryConfiguration
(ConcurrentHashMap<org.nustaq.serialization.FSTConfiguration.FieldKey, FSTClazzInfo.FSTFieldInfo> shared) <T> T
deepCopy
(T metadata) void
encodeToStream
(OutputStream out, Object toSerialize) helper to write series of objects to streams/files > Integer.MAX_VALUE.getCachedObject
(Class cl) getClassForCPName
(String name) getClassInfo
(Class type) getClazzInfo
(Class rowClass) <T> T
get cross platform symbolic class identifierbyte[]
getCrossPlatformBinaryCache
(String symbolicName) static FSTConfiguration
protected FSTObjectInput
getIn()
getInstantiator
(Class clazz) getName()
debug helpergetObjectInput
(byte[] arr) take the given array as input. the array is NOT copied.getObjectInput
(byte[] arr, int len) take the given array as input. the array is NOT copied.utility for thread safety and reuse.getObjectInputCopyFrom
(byte[] arr, int off, int len) take the given array and copy it to input. the array IS copiedgetObjectOutput
(byte[] outByte) utility for thread safety and reuse.protected FSTObjectOutput
getOut()
protected static FSTConfiguration
boolean
boolean
boolean
boolean
ignored currentlyboolean
boolean
special for structsstatic void
debug only, very slow (creates config with each call).void
registerClass
(Class... c) Preregister a class (use at init time).void
registerCrossPlatformClassBinaryCache
(String fulLQName, byte[] binary) registerCrossPlatformClassMapping
(String[][] keysAndVals) init right after creation of configuration, not during operation as it is not threadsafe regarding mutation currently only for minbin serializationregisterCrossPlatformClassMapping
(String shortName, String fqName) registerCrossPlatformClassMappingUseSimpleName
(Class... classes) init right after creation of configuration, not during operation as it is not threadsafe regarding mutationvoid
registerIntToObjectMapFactory
(FSTInt2ObjectMapFactory intToObjectMapFactory) void
registerSerializer
(Class clazz, FSTObjectSerializer ser, boolean alsoForAllSubclasses) register a custom serializer for a given class or the class and all of its subclasses.void
returnObject
(Object cached) reuse heavy weight objects.void
setClassLoader
(ClassLoader classLoader) void
setCoderSpecific
(Object coderSpecific) void
setCrossPlatform
(boolean crossPlatform) setForceClzInit
(boolean forceClzInit) always execute default fields init, even if no transients (so would get overwritten anyway) required for lossy codecs (kson)setForceSerializable
(boolean forceSerializable) treat unserializable classes same as if they would be serializable.void
setInstantiator
(FSTClassInstantiator instantiator) void
setJsonFieldNames
(FSTJsonFieldNames fieldNames) void
setLastResortResolver
(FSTConfiguration.LastResortClassResolver lastResortResolver) void
debug helpervoid
setPreferSpeed
(boolean preferSpeed) this options lets FST favour speed of encoding over size of the encoded object.void
patch default serializer lookup. set to null to delete.void
setShareReferences
(boolean shareReferences) if false, identical objects will get serialized twice.void
setStreamCoderFactory
(FSTConfiguration.StreamCoderFactory streamCoderFactory) allows to use subclassed stream codecs.void
setStructMode
(boolean ignoreSerialInterfaces) ignores all serialization related interfaces (Serializable, Externalizable) and serializes all classes using the default scheme.toString()
-
Field Details
-
isAndroid
public static boolean isAndroid
-
-
Constructor Details
-
FSTConfiguration
protected FSTConfiguration(ConcurrentHashMap<org.nustaq.serialization.FSTConfiguration.FieldKey, FSTClazzInfo.FSTFieldInfo> sharedFieldInfos)
-
-
Method Details
-
getJsonFieldNames
-
setJsonFieldNames
-
getVerifier
-
setVerifier
-
getIntToObjectMapFactory
-
getName
debug helper- Returns:
-
setName
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(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, ConcurrentHashMap<org.nustaq.serialization.FSTConfiguration.FieldKey, FSTClazzInfo.FSTFieldInfo> shared) -
prettyPrintJson
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(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
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
-
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(ConcurrentHashMap<org.nustaq.serialization.FSTConfiguration.FieldKey, FSTClazzInfo.FSTFieldInfo> shared) -
registerSerializer
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
-
setClassLoader
-
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
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
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
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
-
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. -
registerClass
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
-
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
-
getCrossPlatformBinaryCache
-
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
-
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
-
getCPNameForClass
get cross platform symbolic class identifier- Parameters:
cl
-- Returns:
-
getClassForCPName
-
asObject
convenience -
asByteArray
convenience. (object must be serializable) -
asJsonString
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
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
- Parameters:
in
-- Returns:
- Throws:
Exception
- See Also:
-
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
-