Class BinaryUtils


  • public class BinaryUtils
    extends Object
    Binary utils.
    • Field Detail

      • MAPPING_FILE_EXTENSION

        public static final String MAPPING_FILE_EXTENSION
        Actual file name "{type_id}.classname{platform_id}". Where type_id is integer type id and platform_id is byte from PlatformType
        See Also:
        Constant Field Values
      • PLAIN_CLASS_TO_FLAG

        public static final Map<Class<?>,​Byte> PLAIN_CLASS_TO_FLAG
      • FLAG_TO_CLASS

        public static final Map<Byte,​Class<?>> FLAG_TO_CLASS
      • 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).
    • Constructor Detail

      • BinaryUtils

        public BinaryUtils()
    • Method Detail

      • hasSchema

        public static boolean hasSchema​(short flags)
        Check if raw-only flag is set.
        Parameters:
        flags - Flags.
        Returns:
        True if set.
      • fieldTypeName

        public static String fieldTypeName​(int typeId)
        Parameters:
        typeId - Field type ID.
        Returns:
        Field type name or null if 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 - W
        val - Value.
      • unwrapTemporary

        public static Object unwrapTemporary​(Object obj)
        Parameters:
        obj - Value to unwrap.
        Returns:
        Unwrapped value.
      • isPlainType

        public static boolean isPlainType​(int type)
        Returns:
        true if 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:
        true if 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:
        True if 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 as map has. Otherwise returns new HashMap instance.
        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:
        true if 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.
      • 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:
        True if 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:
        True if this is a special map class.
      • 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.
      • readMapping

        public static String readMapping​(File file)
        Parameters:
        file - File.
      • mappingFileName

        public static String mappingFileName​(byte platformId,
                                             int typeId)
        Parameters:
        platformId - Platform id.
        typeId - Type id.
      • rawArrayFromBinary

        public static Object[] rawArrayFromBinary​(Object obj)
        Parameters:
        obj - BinaryArray or Object[].
        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 BinaryObject was 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:
        True if val instance of BinaryEnumArray.
      • isBinaryEnumObject

        public static boolean isBinaryEnumObject​(Object val)
        Parameters:
        val - Value to check.
        Returns:
        True if val instance 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 - True if reader is needed 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 - True if 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,
                                            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:
        True if val instance of BinaryObjectExImpl.
      • isBinaryObjectImpl

        public static boolean isBinaryObjectImpl​(Object val)
        Parameters:
        val - Value to check.
        Returns:
        True if val instance of BinaryObjectImpl.
      • isBinaryArray

        public static boolean isBinaryArray​(Object val)
        Parameters:
        val - Value to check.
        Returns:
        True if val instance of BinaryArray.
      • useBinaryArrays

        public static boolean useBinaryArrays()
        Returns:
        True if typed arrays should be used, false otherwise.
      • field

        public static Object field​(Object obj,
                                   int fieldId)
        Parameters:
        fieldId - Field id.
        Returns:
        BinaryObjectExImpl.field(int) value or null if object not instance of BinaryObjectExImpl.
      • arrayEq

        public static boolean arrayEq​(Object a1,
                                      Object a2)
        Check for arrays equality.
        Parameters:
        a1 - Value 1.
        a2 - Value 2.
        Returns:
        True if 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 -
      • 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:
        True if 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)