Class BinaryUtils
- java.lang.Object
-
- org.apache.ignite.internal.binary.BinaryUtils
-
public class BinaryUtils extends Object
Binary utils.
-
-
Field Summary
Fields Modifier and Type Field Description static BinariesFactorybinariesFactorystatic booleanFIELDS_SORTED_ORDERWhether to sort field in binary objects (doesn't affect Binarylizable).static shortFLAG_COMPACT_FOOTERFlag: compact footer, no field IDs.static shortFLAG_CUSTOM_DOTNET_TYPEFlag: raw data contains .NET type information.static Map<Byte,Class<?>>FLAG_TO_CLASSstatic StringMAPPING_FILE_EXTENSIONActual file name "{type_id}.classname{platform_id}".static Map<Class<?>,Byte>PLAIN_CLASS_TO_FLAGstatic booleanUSE_STR_SERIALIZATION_VER_2
-
Constructor Summary
Constructors Constructor Description BinaryUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static booleanarrayEq(Object a1, Object a2)Check for arrays equality.static BinaryMetadatabinaryMetadata(int typeId, String typeName, @Nullable Map<String,BinaryFieldMetadata> fields, @Nullable String affKeyFieldName, @Nullable Collection<T2<Integer,List<Integer>>> schemasAndFieldIds, boolean isEnum, @Nullable Map<String,Integer> enumMap)static BinaryMetadataHandlercachingMetadataHandler()static voidcheckProtocolVersion(byte protoVer)Check protocol version.static voidclearCache()Clears binary caches.static BinaryObjectExdetach(Object o)static voiddetachAllowedIfPossible(Object o)static ObjectdoReadOptimized(BinaryInputStream in, BinaryContext ctx, @Nullable ClassLoader clsLdr)Read object serialized using optimized marshaller.static String[]doReadStringArray(BinaryInputStream in)static Objectfield(Object obj, int fieldId)static intfieldId(BinaryReaderEx reader, int order)Gets field by its order.static intfieldIdLength(short flags)Get field ID length.static intfieldOffsetLength(short flags)Get offset length for the given flags.static intfieldOffsetRelative(BinaryPositionReadable stream, int pos, int fieldOffsetSize)Get relative field offset.static StringfieldTypeName(int typeId)static IgniteBiTuple<Integer,Integer>footerAbsolute(BinaryPositionReadable in, int start)Get object's footer.static intfooterStartAbsolute(BinaryPositionReadable in, int start)Get object's footer.static int[]getSchema(BinaryContext ctx, IntFunction<BinaryType> metadataProvider, int typeId, int schemaId)Gets the schema.static inthashCode(byte[] data, int startPos, int endPos)static booleanhasSchema(short flags)Check if raw-only flag is set.static booleanimmutable(Object obj)static voidinitUseBinaryArrays()InitializeUSE_BINARY_ARRAYSvalue withIgniteCommonsSystemProperties.IGNITE_USE_BINARY_ARRAYSsystem property value.static booleanisBinaryArray(Object val)static booleanisBinaryEnumArray(Object val)static booleanisBinaryEnumObject(Object val)static booleanisBinaryObjectExImpl(Object val)static booleanisBinaryObjectImpl(Object val)static booleanisBinaryType(Class<?> cls)Tells whether provided type is binary.static booleanisObjectArray(Class<?> cls)static booleanisPlainArrayType(int type)Checks whether an array type values can or can not contain references to other object.static booleanisPlainType(int type)static booleanisSpecialCollection(Class cls)Check if class represents a collection which must be treated specially.static booleanisSpecialMap(Class cls)Check if class represents a map which must be treated specially.static booleanknownArray(Object arr)static booleanknownCollection(Object col)static booleanknownMap(Object map)static intlength(BinaryPositionReadable in, int start)Get binary object length.static bytemappedFilePlatformId(String fileName)static intmappedTypeId(String fileName)static StringmappingFileName(byte platformId, int typeId)static BinaryMetadatamergeMetadata(@Nullable BinaryMetadata oldMeta, BinaryMetadata newMeta)Merge old and new metas.static BinaryMetadatamergeMetadata(@Nullable BinaryMetadata oldMeta, BinaryMetadata newMeta, @Nullable Set<Integer> changedSchemas)Merge old and new metas.static <V> Collection<V>newKnownCollection(Object col)Attempts to create a new collection of the same known type.static <K,V>
Map<K,V>newKnownMap(Object map)Attempts to create a new map of the same known type.static <K,V>
Map<K,V>newMap(Map<K,V> map)Attempts to create a new map of the same type asmaphas.static BinaryMetadataHandlernoopMetadataHandler()static Object[]rawArrayFromBinary(Object obj)static intrawOffsetAbsolute(BinaryPositionReadable in, int start)Get absolute raw offset of the object.static BinaryReaderExreader(BinaryContext ctx, BinaryInputStream in, ClassLoader ldr, boolean forUnmarshal)Creates reader instance.static BinaryReaderExreader(BinaryContext ctx, BinaryInputStream in, ClassLoader ldr, boolean skipHdrCheck, boolean forUnmarshal)Constructor.static BinaryReaderExreader(BinaryContext ctx, BinaryInputStream in, ClassLoader ldr, BinaryReaderEx reader, boolean forUnmarshal)Creates reader instance.static StringreadMapping(File file)static Collection<T2<Integer,int[]>>schemasAndFieldsIds(BinaryMetadata meta)static bytetypeByClass(Class<?> cls)static StringtypeName(Object obj)static ObjectunmarshallJdbc(byte type, BinaryReaderEx reader, boolean binObjAllow, boolean keepBinary)Unmarshall JDBC supported type.static Map<Class<?>,Function<Object,Object>>unwrapFuncForSizeCalc()static ObjectunwrapTemporary(Object obj)static booleanuseBinaryArrays()static voidvalidateEnumValues(String typeName, @Nullable Map<String,Integer> enumValues)Checks enum values mapping.static voidwritePlainObject(BinaryWriterEx writer, Object val)Write value with flag. e.g. writePlainObject(writer, (byte)77) will write two byte: {BYTE, 77}.static BinaryWriterExwriter(BinaryContext ctx, boolean failIfUnregistered, int typeId)static BinaryWriterExwriter(BinaryContext ctx, BinaryOutputStream out)static BinaryWriterExwriter(BinaryContext ctx, BinaryOutputStream out, org.apache.ignite.internal.binary.BinaryWriterSchemaHolder schema)
-
-
-
Field Detail
-
MAPPING_FILE_EXTENSION
public static final String MAPPING_FILE_EXTENSION
Actual file name "{type_id}.classname{platform_id}". Wheretype_idis integer type id andplatform_idis byte fromPlatformType- See Also:
- Constant Field Values
-
USE_STR_SERIALIZATION_VER_2
public static final boolean USE_STR_SERIALIZATION_VER_2
-
FLAG_COMPACT_FOOTER
public static final short FLAG_COMPACT_FOOTER
Flag: compact footer, no field IDs.- See Also:
- Constant Field Values
-
FLAG_CUSTOM_DOTNET_TYPE
public static final short FLAG_CUSTOM_DOTNET_TYPE
Flag: raw data contains .NET type information. Always 0 in Java. Keep it here for information only.- See Also:
- Constant Field Values
-
FIELDS_SORTED_ORDER
public static boolean FIELDS_SORTED_ORDER
Whether to sort field in binary objects (doesn't affect Binarylizable).
-
binariesFactory
public static final BinariesFactory binariesFactory
-
-
Method Detail
-
hasSchema
public static boolean hasSchema(short flags)
Check if raw-only flag is set.- Parameters:
flags- Flags.- Returns:
Trueif set.
-
fieldTypeName
public static String fieldTypeName(int typeId)
- Parameters:
typeId- Field type ID.- Returns:
- Field type name or
nullif unknown.
-
writePlainObject
public static void writePlainObject(BinaryWriterEx writer, Object val)
Write value with flag. e.g. writePlainObject(writer, (byte)77) will write two byte: {BYTE, 77}.- Parameters:
writer- Wval- Value.
-
unwrapTemporary
public static Object unwrapTemporary(Object obj)
- Parameters:
obj- Value to unwrap.- Returns:
- Unwrapped value.
-
isPlainType
public static boolean isPlainType(int type)
- Returns:
trueif content of serialized value cannot contain references to other object.
-
isPlainArrayType
public static boolean isPlainArrayType(int type)
Checks whether an array type values can or can not contain references to other object.- Parameters:
type- Array type.- Returns:
trueif content of serialized array value cannot contain references to other object.
-
typeByClass
public static byte typeByClass(Class<?> cls)
- Parameters:
cls- Class.- Returns:
- Binary field type.
-
isBinaryType
public static boolean isBinaryType(Class<?> cls)
Tells whether provided type is binary.- Parameters:
cls- Class to check.- Returns:
- Whether type is binary.
-
knownMap
public static boolean knownMap(Object map)
- Parameters:
map- Map to check.- Returns:
Trueif this map type is supported.
-
newKnownMap
public static <K,V> Map<K,V> newKnownMap(Object map)
Attempts to create a new map of the same known type. Will return null if map type is not supported.- Parameters:
map- Map.- Returns:
- New map of the same type or null.
-
newMap
public static <K,V> Map<K,V> newMap(Map<K,V> map)
Attempts to create a new map of the same type asmaphas. Otherwise returns newHashMapinstance.- Parameters:
map- Original map.- Returns:
- New map.
-
knownCollection
public static boolean knownCollection(Object col)
- Parameters:
col- Collection to check.- Returns:
- True if this is a collection of a known type.
-
knownArray
public static boolean knownArray(Object arr)
- Parameters:
arr- Array to check.- Returns:
trueif this array is of a known type.
-
newKnownCollection
public static <V> Collection<V> newKnownCollection(Object col)
Attempts to create a new collection of the same known type. Will return null if collection type is unknown.- Parameters:
col- Collection.- Returns:
- New empty collection.
-
checkProtocolVersion
public static void checkProtocolVersion(byte protoVer)
Check protocol version.- Parameters:
protoVer- Protocol version.
-
length
public static int length(BinaryPositionReadable in, int start)
Get binary object length.- Parameters:
in- Input stream.start- Start position.- Returns:
- Length.
-
footerStartAbsolute
public static int footerStartAbsolute(BinaryPositionReadable in, int start)
Get object's footer.- Parameters:
in- Input stream.start- Start position.- Returns:
- Footer start.
-
footerAbsolute
public static IgniteBiTuple<Integer,Integer> footerAbsolute(BinaryPositionReadable in, int start)
Get object's footer.- Parameters:
in- Input stream.start- Start position.- Returns:
- Footer.
-
rawOffsetAbsolute
public static int rawOffsetAbsolute(BinaryPositionReadable in, int start)
Get absolute raw offset of the object.- Parameters:
in- Input stream.start- Object start position inside the stream.- Returns:
- Raw offset.
-
fieldOffsetLength
public static int fieldOffsetLength(short flags)
Get offset length for the given flags.- Parameters:
flags- Flags.- Returns:
- Offset size.
-
fieldIdLength
public static int fieldIdLength(short flags)
Get field ID length.- Parameters:
flags- Flags.- Returns:
- Field ID length.
-
fieldOffsetRelative
public static int fieldOffsetRelative(BinaryPositionReadable stream, int pos, int fieldOffsetSize)
Get relative field offset.- Parameters:
stream- Stream.pos- Position.fieldOffsetSize- Field offset size.- Returns:
- Relative field offset.
-
mergeMetadata
public static BinaryMetadata mergeMetadata(@Nullable @Nullable BinaryMetadata oldMeta, BinaryMetadata newMeta)
Merge old and new metas.- Parameters:
oldMeta- Old meta.newMeta- New meta.- Returns:
- New meta if old meta was null, old meta if no changes detected, merged meta otherwise.
- Throws:
BinaryObjectException- If merge failed due to metadata conflict.
-
mergeMetadata
public static BinaryMetadata mergeMetadata(@Nullable @Nullable BinaryMetadata oldMeta, BinaryMetadata newMeta, @Nullable @Nullable Set<Integer> changedSchemas)
Merge old and new metas.- Parameters:
oldMeta- Old meta.newMeta- New meta.changedSchemas- Set for holding changed schemas.- Returns:
- New meta if old meta was null, old meta if no changes detected, merged meta otherwise.
- Throws:
BinaryObjectException- If merge failed due to metadata conflict.
-
isSpecialCollection
public static boolean isSpecialCollection(Class cls)
Check if class represents a collection which must be treated specially.- Parameters:
cls- Class.- Returns:
Trueif this is a special collection class.
-
isSpecialMap
public static boolean isSpecialMap(Class cls)
Check if class represents a map which must be treated specially.- Parameters:
cls- Class.- Returns:
Trueif this is a special map class.
-
doReadStringArray
public static String[] doReadStringArray(BinaryInputStream in) throws BinaryObjectException
- Returns:
- Value.
- Throws:
BinaryObjectException- In case of error.
-
doReadOptimized
public static Object doReadOptimized(BinaryInputStream in, BinaryContext ctx, @Nullable @Nullable ClassLoader clsLdr)
Read object serialized using optimized marshaller.- Returns:
- Result.
-
unmarshallJdbc
public static Object unmarshallJdbc(byte type, BinaryReaderEx reader, boolean binObjAllow, boolean keepBinary)
Unmarshall JDBC supported type.- Parameters:
type- Type.reader- Binary reader.binObjAllow- Allow to read non plaint objects.keepBinary- Whether to deserialize objects or keep in binary format.- Returns:
- Read object.
-
mappedTypeId
public static int mappedTypeId(String fileName)
- Parameters:
fileName- Name of file with marshaller mapping information.
-
mappedFilePlatformId
public static byte mappedFilePlatformId(String fileName)
- Parameters:
fileName- Name of file with marshaller mapping information.
-
mappingFileName
public static String mappingFileName(byte platformId, int typeId)
- Parameters:
platformId- Platform id.typeId- Type id.
-
validateEnumValues
public static void validateEnumValues(String typeName, @Nullable @Nullable Map<String,Integer> enumValues) throws BinaryObjectException
Checks enum values mapping.- Parameters:
typeName- Name of the type.enumValues- Enum name to ordinal mapping.- Throws:
BinaryObjectException
-
rawArrayFromBinary
public static Object[] rawArrayFromBinary(Object obj)
- Parameters:
obj-BinaryArrayorObject[].- Returns:
- Objects array.
-
isObjectArray
public static boolean isObjectArray(Class<?> cls)
-
typeName
public static String typeName(Object obj)
- Returns:
- Type name of the specified object. If
BinaryObjectwas specified its type will be returned.
-
clearCache
public static void clearCache()
Clears binary caches.
-
getSchema
public static int[] getSchema(BinaryContext ctx, IntFunction<BinaryType> metadataProvider, int typeId, int schemaId)
Gets the schema.- Parameters:
ctx- Binary context.metadataProvider- Function to provide binary type metadata.typeId- Type id.schemaId- Schema id.
-
unwrapFuncForSizeCalc
public static Map<Class<?>,Function<Object,Object>> unwrapFuncForSizeCalc()
- Returns:
- Unwrap function for object size calculation.
-
isBinaryEnumArray
public static boolean isBinaryEnumArray(Object val)
- Parameters:
val- Value to check.- Returns:
Trueifvalinstance ofBinaryEnumArray.
-
isBinaryEnumObject
public static boolean isBinaryEnumObject(Object val)
- Parameters:
val- Value to check.- Returns:
Trueifvalinstance of binary Enum object.
-
reader
public static BinaryReaderEx reader(BinaryContext ctx, BinaryInputStream in, ClassLoader ldr, boolean forUnmarshal)
Creates reader instance.- Parameters:
ctx- Context.in- Input stream.ldr- Class loader.forUnmarshal-Trueif reader is needed to unmarshal object.
-
reader
public static BinaryReaderEx reader(BinaryContext ctx, BinaryInputStream in, ClassLoader ldr, BinaryReaderEx reader, boolean forUnmarshal)
Creates reader instance.- Parameters:
ctx- Context.in- Input stream.ldr- Class loader.reader- BinaryReaderEx.forUnmarshal-Trueif reader is need to unmarshal object.
-
reader
public static BinaryReaderEx reader(BinaryContext ctx, BinaryInputStream in, ClassLoader ldr, boolean skipHdrCheck, boolean forUnmarshal)
Constructor.- Parameters:
ctx- Context.in- Input stream.ldr- Class loader.skipHdrCheck- Whether to skip header check.forUnmarshal-Trueif reader is need to unmarshal object.
-
writer
public static BinaryWriterEx writer(BinaryContext ctx, boolean failIfUnregistered, int typeId)
- Parameters:
ctx- Context.failIfUnregistered- Flag to fail while writing object of unregistered type.typeId- Type id.- Returns:
- Writer instance.
-
writer
public static BinaryWriterEx writer(BinaryContext ctx, BinaryOutputStream out)
- Parameters:
ctx- Context.out- Output stream.- Returns:
- Writer instance.
-
writer
public static BinaryWriterEx writer(BinaryContext ctx, BinaryOutputStream out, org.apache.ignite.internal.binary.BinaryWriterSchemaHolder schema)
- Parameters:
ctx- Context.out- Output stream.- Returns:
- Writer instance.
-
cachingMetadataHandler
public static BinaryMetadataHandler cachingMetadataHandler()
- Returns:
- Instance of caching handler.
-
noopMetadataHandler
public static BinaryMetadataHandler noopMetadataHandler()
- Returns:
- Instance of noop handler.
-
isBinaryObjectExImpl
public static boolean isBinaryObjectExImpl(Object val)
- Parameters:
val- Value to check.- Returns:
Trueifvalinstance ofBinaryObjectExImpl.
-
isBinaryObjectImpl
public static boolean isBinaryObjectImpl(Object val)
- Parameters:
val- Value to check.- Returns:
Trueifvalinstance ofBinaryObjectImpl.
-
isBinaryArray
public static boolean isBinaryArray(Object val)
- Parameters:
val- Value to check.- Returns:
Trueifvalinstance ofBinaryArray.
-
useBinaryArrays
public static boolean useBinaryArrays()
- Returns:
Trueif typed arrays should be used,falseotherwise.
-
initUseBinaryArrays
public static void initUseBinaryArrays()
InitializeUSE_BINARY_ARRAYSvalue withIgniteCommonsSystemProperties.IGNITE_USE_BINARY_ARRAYSsystem property value. This method invoked using reflection in tests.
-
field
public static Object field(Object obj, int fieldId)
- Parameters:
fieldId- Field id.- Returns:
BinaryObjectExImpl.field(int)value ornullif object not instance ofBinaryObjectExImpl.
-
arrayEq
public static boolean arrayEq(Object a1, Object a2)
Check for arrays equality.- Parameters:
a1- Value 1.a2- Value 2.- Returns:
Trueif arrays equal.
-
detach
public static BinaryObjectEx detach(Object o)
- Parameters:
o- Object to detach.- Returns:
- Detached object.
-
detachAllowedIfPossible
public static void detachAllowedIfPossible(Object o)
- Parameters:
o-
-
schemasAndFieldsIds
public static Collection<T2<Integer,int[]>> schemasAndFieldsIds(BinaryMetadata meta)
- Parameters:
meta- Binary metadata.- Returns:
- Schemas identifiers of the specified
BinaryMetadata.
-
fieldId
public static int fieldId(BinaryReaderEx reader, int order)
Gets field by its order.- Parameters:
reader- Reader.order- Order.
-
immutable
public static boolean immutable(Object obj)
- Parameters:
obj- Value.- Returns:
Trueif object is of known immutable type.
-
binaryMetadata
public static BinaryMetadata binaryMetadata(int typeId, String typeName, @Nullable @Nullable Map<String,BinaryFieldMetadata> fields, @Nullable @Nullable String affKeyFieldName, @Nullable @Nullable Collection<T2<Integer,List<Integer>>> schemasAndFieldIds, boolean isEnum, @Nullable @Nullable Map<String,Integer> enumMap)
- Parameters:
typeId- Type ID.typeName- Type name.fields- Fields map.affKeyFieldName- Affinity key field name.schemasAndFieldIds- Schemas and fields identifiers.isEnum- Enum flag.enumMap- Enum name to ordinal mapping.- Returns:
- New instance of
BinaryMetadata.
-
hashCode
public static int hashCode(byte[] data, int startPos, int endPos)
-
-