Class MarshallUtil


  • @Immutable
    public class MarshallUtil
    extends java.lang.Object
    MarshallUtil.
    Since:
    4.0
    Author:
    Galder ZamarreƱo
    • Constructor Detail

      • MarshallUtil

        public MarshallUtil()
    • Method Detail

      • marshallMap

        public static <K,​V,​T extends java.util.Map<K,​V>> void marshallMap​(T map,
                                                                                            java.io.ObjectOutput out)
                                                                                     throws java.io.IOException
        Marshall the map to the ObjectOutput.

        null maps are supported.

        Type Parameters:
        K - Key type of the map.
        V - Value type of the map.
        T - Type of the Map.
        Parameters:
        map - Map to marshall.
        out - ObjectOutput to write. It must be non-null.
        Throws:
        java.io.IOException - If any of the usual Input/Output related exceptions occur.
      • unmarshallMap

        public static <K,​V,​T extends java.util.Map<K,​V>> T unmarshallMap​(java.io.ObjectInput in,
                                                                                           MarshallUtil.MapBuilder<K,​V,​T> builder)
                                                                                    throws java.io.IOException,
                                                                                           java.lang.ClassNotFoundException
        Unmarshall the Map.

        If the marshalled map is null, then the MarshallUtil.MapBuilder is not invoked.

        Parameters:
        in - ObjectInput to read.
        builder - MarshallUtil.MapBuilder to create the concrete Map implementation.
        Returns:
        The populated Map created by the MarshallUtil.MapBuilder or null.
        Throws:
        java.io.IOException - If any of the usual Input/Output related exceptions occur.
        java.lang.ClassNotFoundException - If the class of a serialized object cannot be found.
        See Also:
        marshallMap(Map, ObjectOutput)
      • marshallMap

        public static <K,​V,​T extends java.util.Map<K,​V>> void marshallMap​(T map,
                                                                                            MarshallUtil.ElementWriter<K> keyWriter,
                                                                                            MarshallUtil.ElementWriter<V> valueWrite,
                                                                                            java.io.ObjectOutput out)
                                                                                     throws java.io.IOException
        Marshall the map to the ObjectOutput.

        null maps are supported.

        Type Parameters:
        K - Key type of the map.
        V - Value type of the map.
        T - Type of the Map.
        Parameters:
        map - Map to marshall.
        out - ObjectOutput to write. It must be non-null.
        Throws:
        java.io.IOException - If any of the usual Input/Output related exceptions occur.
      • marshallUUID

        public static void marshallUUID​(java.util.UUID uuid,
                                        java.io.ObjectOutput out,
                                        boolean checkNull)
                                 throws java.io.IOException
        Marshall the UUID by sending the most and lest significant bits.

        This method supports null if checkNull is set to true.

        Parameters:
        uuid - UUID to marshall.
        out - ObjectOutput to write.
        checkNull - If true, it checks if uuid is null.
        Throws:
        java.io.IOException - If any of the usual Input/Output related exceptions occur.
      • unmarshallUUID

        public static java.util.UUID unmarshallUUID​(java.io.ObjectInput in,
                                                    boolean checkNull)
                                             throws java.io.IOException
        Unmarshall UUID.
        Parameters:
        in - ObjectInput to read.
        checkNull - If true, it checks if the UUID marshalled was null.
        Returns:
        UUID marshalled.
        Throws:
        java.io.IOException - If any of the usual Input/Output related exceptions occur.
      • marshallArray

        public static <E> void marshallArray​(E[] array,
                                             java.io.ObjectOutput out)
                                      throws java.io.IOException
        Marshall arrays.

        This method supports null array.

        Type Parameters:
        E - Array type.
        Parameters:
        array - Array to marshall.
        out - ObjectOutput to write.
        Throws:
        java.io.IOException - If any of the usual Input/Output related exceptions occur.
      • unmarshallArray

        public static <E> E[] unmarshallArray​(java.io.ObjectInput in,
                                              MarshallUtil.ArrayBuilder<E> builder)
                                       throws java.io.IOException,
                                              java.lang.ClassNotFoundException
        Unmarshall arrays.
        Type Parameters:
        E - Array type.
        Parameters:
        in - ObjectInput to read.
        builder - MarshallUtil.ArrayBuilder to build the array.
        Returns:
        The populated array.
        Throws:
        java.io.IOException - If any of the usual Input/Output related exceptions occur.
        java.lang.ClassNotFoundException - If the class of a serialized object cannot be found.
      • marshallCollection

        public static <E> void marshallCollection​(java.util.Collection<E> collection,
                                                  java.io.ObjectOutput out)
                                           throws java.io.IOException
        Marshall a Collection.

        This method supports null collection.

        Type Parameters:
        E - Collection's element type.
        Parameters:
        collection - Collection to marshal.
        out - ObjectOutput to write.
        Throws:
        java.io.IOException - If any of the usual Input/Output related exceptions occur.
      • marshallCollection

        public static <E> void marshallCollection​(java.util.Collection<E> collection,
                                                  java.io.ObjectOutput out,
                                                  MarshallUtil.ElementWriter<E> writer)
                                           throws java.io.IOException
        Marshall a Collection.

        This method supports null collection.

        Type Parameters:
        E - Collection's element type.
        Parameters:
        collection - Collection to marshal.
        out - ObjectOutput to write.
        writer - MarshallUtil.ElementWriter that writes single element to the output.
        Throws:
        java.io.IOException - If any of the usual Input/Output related exceptions occur.
      • unmarshallCollection

        public static <E,​T extends java.util.Collection<E>> T unmarshallCollection​(java.io.ObjectInput in,
                                                                                         MarshallUtil.CollectionBuilder<E,​T> builder,
                                                                                         MarshallUtil.ElementReader<E> reader)
                                                                                  throws java.io.IOException,
                                                                                         java.lang.ClassNotFoundException
        Unmarshal a Collection.
        Type Parameters:
        E - Collection's element type.
        T - Collection implementation.
        Parameters:
        in - ObjectInput to read.
        builder - MarshallUtil.CollectionBuilder builds the concrete Collection based on size.
        reader - MarshallUtil.ElementReader reads one element from the input.
        Returns:
        The concrete Collection implementation.
        Throws:
        java.io.IOException - If any of the usual Input/Output related exceptions occur.
        java.lang.ClassNotFoundException - If the class of a serialized object cannot be found.
      • unmarshallCollection

        public static <E,​T extends java.util.Collection<E>> T unmarshallCollection​(java.io.ObjectInput in,
                                                                                         MarshallUtil.CollectionBuilder<E,​T> builder)
                                                                                  throws java.io.IOException,
                                                                                         java.lang.ClassNotFoundException
        Unmarshal a Collection.
        Type Parameters:
        E - Collection's element type.
        T - Collection implementation.
        Parameters:
        in - ObjectInput to read.
        builder - MarshallUtil.CollectionBuilder builds the concrete Collection based on size.
        Returns:
        The concrete Collection implementation.
        Throws:
        java.io.IOException - If any of the usual Input/Output related exceptions occur.
        java.lang.ClassNotFoundException - If the class of a serialized object cannot be found.
      • unmarshallCollectionUnbounded

        public static <E,​T extends java.util.Collection<E>> T unmarshallCollectionUnbounded​(java.io.ObjectInput in,
                                                                                                  MarshallUtil.UnboundedCollectionBuilder<E,​T> builder)
                                                                                           throws java.io.IOException,
                                                                                                  java.lang.ClassNotFoundException
        Same as unmarshallCollection(ObjectInput, CollectionBuilder).

        Used when the size of the Collection is not needed for it construction.

        Throws:
        java.io.IOException
        java.lang.ClassNotFoundException
      • marshallString

        public static void marshallString​(java.lang.String string,
                                          java.io.ObjectOutput out)
                                   throws java.io.IOException
        Marshall the String.

        Same behavior as DataOutput.writeUTF(String) but it checks for null. If the string is never null, it is better to use DataOutput.writeUTF(String).

        Parameters:
        string - String to marshall.
        out - ObjectOutput to write.
        Throws:
        java.io.IOException - If any of the usual Input/Output related exceptions occur.
      • unmarshallString

        public static java.lang.String unmarshallString​(java.io.ObjectInput in)
                                                 throws java.io.IOException
        Unmarshall a String.
        Parameters:
        in - ObjectInput to read.
        Returns:
        The String or null.
        Throws:
        java.io.IOException - If any of the usual Input/Output related exceptions occur.
      • marshallByteArray

        public static void marshallByteArray​(byte[] array,
                                             java.io.ObjectOutput out)
                                      throws java.io.IOException
        Same as marshallArray(Object[], ObjectOutput) but specialized for byte arrays.
        Throws:
        java.io.IOException
      • marshallSize

        public static void marshallSize​(java.io.ObjectOutput out,
                                        int value)
                                 throws java.io.IOException
        A special marshall implementation for integer.

        This method supports negative values but they are handles as NULL_VALUE. It means that the real value is lost and NULL_VALUE is returned by unmarshallSize(ObjectInput).

        The integer is marshalled in a variable length from 1 to 5 bytes. Negatives values are always marshalled in 1 byte.

        Parameters:
        out - ObjectOutput to write.
        value - Integer value to marshall.
        Throws:
        java.io.IOException - If any of the usual Input/Output related exceptions occur.
      • unmarshallSize

        public static int unmarshallSize​(java.io.ObjectInput in)
                                  throws java.io.IOException
        Unmarshall an integer.
        Parameters:
        in - ObjectInput to read.
        Returns:
        The integer value or NULL_VALUE if the original value was negative.
        Throws:
        java.io.IOException - If any of the usual Input/Output related exceptions occur.
      • marshallEnum

        public static <E extends java.lang.Enum<E>> void marshallEnum​(E e,
                                                                      java.io.ObjectOutput output)
                                                               throws java.io.IOException
        Throws:
        java.io.IOException
      • unmarshallEnum

        public static <E extends java.lang.Enum<E>> E unmarshallEnum​(java.io.ObjectInput input,
                                                                     MarshallUtil.EnumBuilder<E> builder)
                                                              throws java.io.IOException
        Throws:
        java.io.IOException
      • marshallIntCollection

        public static void marshallIntCollection​(java.util.Collection<java.lang.Integer> collection,
                                                 java.io.ObjectOutput out)
                                          throws java.io.IOException
        Marshalls a collection of integers.
        Parameters:
        collection - the collection to marshall.
        out - the ObjectOutput to write to.
        Throws:
        java.io.IOException - if an error occurs.
      • unmarshallIntCollection

        public static <T extends java.util.Collection<java.lang.Integer>> T unmarshallIntCollection​(java.io.ObjectInput in,
                                                                                                    MarshallUtil.CollectionBuilder<java.lang.Integer,​T> builder)
                                                                                             throws java.io.IOException
        Unmarshalls a collection of integers.
        Type Parameters:
        T - the concrete type of the collection.
        Parameters:
        in - the ObjectInput to read from.
        builder - the MarshallUtil.CollectionBuilder to build the collection of integer.
        Returns:
        the collection.
        Throws:
        java.io.IOException - if an error occurs.
      • isSafeClass

        public static boolean isSafeClass​(java.lang.String className,
                                          java.util.List<java.lang.String> whitelist)
        Checks whether class name is matched by the class name white list regular expressions provided.
        Parameters:
        className - class to verify
        whitelist - list of regular expressions to match class name against
        Returns:
        true if the class matched at least one of the regular expressions, false otherwise