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
      • CLS_TO_WRITE_REPLACER

        public static final Map<Class,​BinaryWriteReplacer> CLS_TO_WRITE_REPLACER
        Map from class to associated write replacer.
      • SINGLETON_LIST_CLS

        public static final Class<? extends Collection> SINGLETON_LIST_CLS
        Class for SingletonList obtained at runtime.
      • FLAG_HAS_SCHEMA

        public static final short FLAG_HAS_SCHEMA
        Flag: only raw data exists.
        See Also:
        Constant Field Values
      • FLAG_HAS_RAW

        public static final short FLAG_HAS_RAW
        Flag indicating that object has raw data.
        See Also:
        Constant Field Values
      • FLAG_OFFSET_ONE_BYTE

        public static final short FLAG_OFFSET_ONE_BYTE
        Flag: offsets take 1 byte.
        See Also:
        Constant Field Values
      • FLAG_OFFSET_TWO_BYTES

        public static final short FLAG_OFFSET_TWO_BYTES
        Flag: offsets take 2 bytes.
        See Also:
        Constant Field Values
      • 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
      • OFFSET_1

        public static final int OFFSET_1
        Offset which fits into 1 byte.
        See Also:
        Constant Field Values
      • OFFSET_2

        public static final int OFFSET_2
        Offset which fits into 2 bytes.
        See Also:
        Constant Field Values
      • OFFSET_4

        public static final int OFFSET_4
        Offset which fits into 4 bytes.
        See Also:
        Constant Field Values
      • WRAP_TREES

        public static final boolean WRAP_TREES
        Whether to skip TreeMap/TreeSet wrapping.
      • 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

      • isUserType

        public static boolean isUserType​(short flags)
        Check if user type flag is set.
        Parameters:
        flags - Flags.
        Returns:
        True if set.
      • hasSchema

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

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

        public static boolean isCompactFooter​(short flags)
        Check if "no-field-ids" flag is set.
        Parameters:
        flags - Flags.
        Returns:
        True if set.
      • isFlagSet

        public static boolean isFlagSet​(short flags,
                                        short flag)
        Check whether particular flag is set.
        Parameters:
        flags - Flags.
        flag - Flag.
        Returns:
        True if flag is set in flags.
      • schemaInitialId

        public static int schemaInitialId()
        Schema initial ID.
        Returns:
        ID.
      • updateSchemaId

        public static int updateSchemaId​(int schemaId,
                                         int fieldId)
        Update schema ID when new field is added.
        Parameters:
        schemaId - Current schema ID.
        fieldId - Field ID.
        Returns:
        New schema ID.
      • 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​(BinaryWriterExImpl writer,
                                            Object val)
        Write value with flag. e.g. writePlainObject(writer, (byte)77) will write two byte: {BYTE, 77}.
        Parameters:
        writer - W
        val - Value.
      • unwrapLazy

        public static Object unwrapLazy​(@Nullable
                                        @Nullable Object obj)
        Parameters:
        obj - Value to unwrap.
        Returns:
        Unwrapped value.
      • unwrapLazyIterator

        public static Iterator<Object> unwrapLazyIterator​(Iterator<Object> delegate)
        Parameters:
        delegate - Iterator to delegate.
        Returns:
        New iterator.
      • 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.
      • wrapTrees

        public static boolean wrapTrees()
        Returns:
        Whether tree structures should be wrapped.
      • 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.
      • knownCacheObject

        public static boolean knownCacheObject​(Object obj)
        Parameters:
        obj - Object to check.
        Returns:
        True if this is an object 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.
      • newSet

        public static <V> Set<V> newSet​(Set<V> set)
        Attempts to create a new set of the same type as set has. Otherwise returns new HashSet instance.
        Parameters:
        set - Original set.
        Returns:
        New set.
      • 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.
      • footerStartRelative

        public static int footerStartRelative​(BinaryPositionReadable in,
                                              int start)
        Get footer start of the object.
        Parameters:
        in - Input stream.
        start - Object start position inside the stream.
        Returns:
        Footer start.
      • footerStartAbsolute

        public static int footerStartAbsolute​(BinaryPositionReadable in,
                                              int start)
        Get object's footer.
        Parameters:
        in - Input stream.
        start - Start position.
        Returns:
        Footer start.
      • rawOffsetRelative

        public static int rawOffsetRelative​(BinaryPositionReadable in,
                                            int start)
        Get relative raw offset of the object.
        Parameters:
        in - Input stream.
        start - Object start position inside the stream.
        Returns:
        Raw offset.
      • 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.
      • mode

        public static BinaryWriteMode mode​(Class<?> cls)
        Parameters:
        cls - Class.
        Returns:
        Mode.
      • 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.
      • doReadByteArray

        public static byte[] doReadByteArray​(BinaryInputStream in)
        Returns:
        Value.
      • doReadBooleanArray

        public static boolean[] doReadBooleanArray​(BinaryInputStream in)
        Returns:
        Value.
      • doReadShortArray

        public static short[] doReadShortArray​(BinaryInputStream in)
        Returns:
        Value.
      • doReadCharArray

        public static char[] doReadCharArray​(BinaryInputStream in)
        Returns:
        Value.
      • doReadIntArray

        public static int[] doReadIntArray​(BinaryInputStream in)
        Returns:
        Value.
      • doReadLongArray

        public static long[] doReadLongArray​(BinaryInputStream in)
        Returns:
        Value.
      • doReadFloatArray

        public static float[] doReadFloatArray​(BinaryInputStream in)
        Returns:
        Value.
      • doReadDoubleArray

        public static double[] doReadDoubleArray​(BinaryInputStream in)
        Returns:
        Value.
      • doReadClass

        public static Class doReadClass​(BinaryInputStream in,
                                        BinaryContext ctx,
                                        ClassLoader ldr,
                                        boolean deserialize)
                                 throws BinaryObjectException
        Parameters:
        in - Binary input stream.
        ctx - Binary context.
        ldr - Class loader.
        deserialize - Doesn't load the class when the flag is false. Class information is skipped.
        Returns:
        Class object specified at the input stream if deserialize == true. Otherwise returns null
        Throws:
        BinaryObjectException - If failed.
      • doReadClassName

        public static String doReadClassName​(BinaryInputStream in)
        Parameters:
        in - Input stream.
        Returns:
        Class name.
      • resolveClass

        public static Class resolveClass​(BinaryContext ctx,
                                         int typeId,
                                         @Nullable
                                         @Nullable String clsName,
                                         @Nullable
                                         @Nullable ClassLoader ldr,
                                         boolean registerMeta)
        Resolve the class.
        Parameters:
        ctx - Binary context.
        typeId - Type ID.
        clsName - Class name.
        ldr - Class loaded.
        Returns:
        Resovled class.
      • doReadEnum

        public static Enum<?> doReadEnum​(BinaryInputStream in,
                                         Class<?> cls,
                                         boolean useCache)
                                  throws BinaryObjectException
        Having target class in place we simply read ordinal and create final representation.
        Parameters:
        cls - Enum class.
        useCache - True if class loader cache will be used, false otherwise.
        Returns:
        Value.
        Throws:
        BinaryObjectException
      • positionForHandle

        public static int positionForHandle​(BinaryInputStream in)
        Get position to be used for handle. We assume here that the hdr byte was read, hence subtract -1.
        Returns:
        Position for handle.
      • isBinarylizable

        public static boolean isBinarylizable​(Class cls)
        Check if class is binarylizable.
        Parameters:
        cls - Class.
        Returns:
        True if binarylizable.
      • isCustomJavaSerialization

        public static boolean isCustomJavaSerialization​(Class cls)
        Determines whether class contains custom Java serialization logic.
        Parameters:
        cls - Class.
        Returns:
        true if custom Java serialization logic exists, false otherwise.
      • qualifiedFieldName

        public static String qualifiedFieldName​(Class cls,
                                                String fieldName)
        Create qualified field name.
        Parameters:
        cls - Class.
        fieldName - Field name.
        Returns:
        Qualified field name.
      • writeIgniteUuid

        public static void writeIgniteUuid​(BinaryRawWriter out,
                                           @Nullable
                                           @Nullable IgniteUuid val)
        Write IgniteUuid instance.
        Parameters:
        out - Writer.
        val - Value.
      • readIgniteUuid

        @Nullable
        public static @Nullable IgniteUuid readIgniteUuid​(BinaryRawReader in)
        Read IgniteUuid instance.
        Parameters:
        in - Reader.
        Returns:
        Value.
      • utf8BytesToStr

        public static String utf8BytesToStr​(byte[] arr,
                                            int off,
                                            int len)
        Reconstructs string from UTF-8 bytes.
        Parameters:
        arr - array Byte array.
        off - offset Offset in the array.
        len - length Byte array lenght.
        Returns:
        string Resulting string.
      • strToUtf8Bytes

        public static byte[] strToUtf8Bytes​(String val)
        Converts the string into UTF-8 byte array considering special symbols like the surrogates.
        Parameters:
        val - String to convert.
        Returns:
        Resulting byte array.
      • typeProxy

        public static BinaryType typeProxy​(BinaryContext ctx,
                                           BinaryObjectEx obj)
        Create binary type proxy.
        Parameters:
        ctx - Context.
        obj - Binary object.
        Returns:
        Binary type proxy.
      • type

        public static BinaryType type​(BinaryContext ctx,
                                      BinaryObjectEx obj)
        Create binary type which is used by users.
        Parameters:
        ctx - Context.
        obj - Binary object.
        Returns:
        Binary type.
      • binaryMetaFileName

        public static String binaryMetaFileName​(int typeId)
        Parameters:
        typeId - Type id.
        Returns:
        Binary metadata file name.
      • 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.
      • notTmpFile

        public static boolean notTmpFile​(File f)
        Parameters:
        f - File.
        Returns:
        True if file is regular(not temporary).
      • writeReplacer

        public static BinaryWriteReplacer writeReplacer​(Class cls)
        Get predefined write-replacer associated with class.
        Parameters:
        cls - Class.
        Returns:
        Write replacer.
      • mergeEnumValues

        public static Map<String,​Integer> mergeEnumValues​(String typeName,
                                                                @Nullable
                                                                @Nullable Map<String,​Integer> oldValues,
                                                                Map<String,​Integer> newValues)
                                                         throws BinaryObjectException
        Merges enum value mappings and checks for conflicts. Possible conflicts: - same name is used for different ordinal values. - ordinal value is used more than once.
        Parameters:
        typeName - Name of the type.
        oldValues - Old enum value mapping.
        newValues - New enum value mapping.
        Throws:
        BinaryObjectException - in case of name or value conflict.
      • 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.