Class MarshallUtil
- java.lang.Object
-
- org.infinispan.commons.marshall.MarshallUtil
-
@Immutable public class MarshallUtil extends Object
MarshallUtil.- Since:
- 4.0
- Author:
- Galder ZamarreƱo
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceMarshallUtil.ArrayBuilder<E>static interfaceMarshallUtil.CollectionBuilder<E,T extends Collection<E>>static interfaceMarshallUtil.ElementReader<E>static interfaceMarshallUtil.ElementWriter<E>static interfaceMarshallUtil.EnumBuilder<E extends Enum<E>>static interfaceMarshallUtil.MapBuilder<K,V,T extends Map<K,V>>static interfaceMarshallUtil.UnboundedCollectionBuilder<E,T extends Collection<E>>
-
Constructor Summary
Constructors Constructor Description MarshallUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static booleanisSafeClass(String className, List<String> whitelist)Checks whether class name is matched by the class name white list regular expressions provided.static <E> voidmarshallArray(E[] array, ObjectOutput out)Marshall arrays.static voidmarshallByteArray(byte[] array, ObjectOutput out)Same asmarshallArray(Object[], ObjectOutput)but specialized for byte arrays.static <E> voidmarshallCollection(Collection<E> collection, ObjectOutput out)Marshall aCollection.static <E> voidmarshallCollection(Collection<E> collection, ObjectOutput out, MarshallUtil.ElementWriter<E> writer)Marshall aCollection.static <E extends Enum<E>>
voidmarshallEnum(E e, ObjectOutput output)static voidmarshallIntCollection(Collection<Integer> collection, ObjectOutput out)Marshalls a collection of integers.static <K,V,T extends Map<K,V>>
voidmarshallMap(T map, ObjectOutput out)Marshall themapto theObjectOutput.static <K,V,T extends Map<K,V>>
voidmarshallMap(T map, MarshallUtil.ElementWriter<K> keyWriter, MarshallUtil.ElementWriter<V> valueWrite, ObjectOutput out)Marshall themapto theObjectOutput.static voidmarshallSize(ObjectOutput out, int value)A special marshall implementation for integer.static voidmarshallString(String string, ObjectOutput out)Marshall theString.static voidmarshallUUID(UUID uuid, ObjectOutput out, boolean checkNull)Marshall theUUIDby sending the most and lest significant bits.static byte[]toByteArray(ByteBuffer buf)static <E> E[]unmarshallArray(ObjectInput in, MarshallUtil.ArrayBuilder<E> builder)Unmarshall arrays.static byte[]unmarshallByteArray(ObjectInput in)Same asunmarshallArray(ObjectInput, ArrayBuilder)but specialized for byte array.static <E,T extends Collection<E>>
TunmarshallCollection(ObjectInput in, MarshallUtil.CollectionBuilder<E,T> builder)Unmarshal aCollection.static <E,T extends Collection<E>>
TunmarshallCollection(ObjectInput in, MarshallUtil.CollectionBuilder<E,T> builder, MarshallUtil.ElementReader<E> reader)Unmarshal aCollection.static <E,T extends Collection<E>>
TunmarshallCollectionUnbounded(ObjectInput in, MarshallUtil.UnboundedCollectionBuilder<E,T> builder)static <E extends Enum<E>>
EunmarshallEnum(ObjectInput input, MarshallUtil.EnumBuilder<E> builder)static <T extends Collection<Integer>>
TunmarshallIntCollection(ObjectInput in, MarshallUtil.CollectionBuilder<Integer,T> builder)Unmarshalls a collection of integers.static <K,V,T extends Map<K,V>>
TunmarshallMap(ObjectInput in, MarshallUtil.ElementReader<K> keyReader, MarshallUtil.ElementReader<V> valueReader, MarshallUtil.MapBuilder<K,V,T> builder)Unmarshall theMap.static <K,V,T extends Map<K,V>>
TunmarshallMap(ObjectInput in, MarshallUtil.MapBuilder<K,V,T> builder)Unmarshall theMap.static intunmarshallSize(ObjectInput in)Unmarshall an integer.static StringunmarshallString(ObjectInput in)Unmarshall aString.static UUIDunmarshallUUID(ObjectInput in, boolean checkNull)UnmarshallUUID.
-
-
-
Method Detail
-
toByteArray
public static byte[] toByteArray(ByteBuffer buf)
-
marshallMap
public static <K,V,T extends Map<K,V>> void marshallMap(T map, ObjectOutput out) throws IOException
Marshall themapto theObjectOutput.nullmaps are supported.- Type Parameters:
K- Key type of the map.V- Value type of the map.T- Type of theMap.- Parameters:
map-Mapto marshall.out-ObjectOutputto write. It must be non-null.- Throws:
IOException- If any of the usual Input/Output related exceptions occur.
-
unmarshallMap
public static <K,V,T extends Map<K,V>> T unmarshallMap(ObjectInput in, MarshallUtil.MapBuilder<K,V,T> builder) throws IOException, ClassNotFoundException
Unmarshall theMap.If the marshalled map is
null, then theMarshallUtil.MapBuilderis not invoked.- Parameters:
in-ObjectInputto read.builder-MarshallUtil.MapBuilderto create the concreteMapimplementation.- Returns:
- The populated
Mapcreated by theMarshallUtil.MapBuilderornull. - Throws:
IOException- If any of the usual Input/Output related exceptions occur.ClassNotFoundException- If the class of a serialized object cannot be found.- See Also:
marshallMap(Map, ObjectOutput)
-
marshallMap
public static <K,V,T extends Map<K,V>> void marshallMap(T map, MarshallUtil.ElementWriter<K> keyWriter, MarshallUtil.ElementWriter<V> valueWrite, ObjectOutput out) throws IOException
Marshall themapto theObjectOutput.nullmaps are supported.- Type Parameters:
K- Key type of the map.V- Value type of the map.T- Type of theMap.- Parameters:
map-Mapto marshall.out-ObjectOutputto write. It must be non-null.- Throws:
IOException- If any of the usual Input/Output related exceptions occur.
-
unmarshallMap
public static <K,V,T extends Map<K,V>> T unmarshallMap(ObjectInput in, MarshallUtil.ElementReader<K> keyReader, MarshallUtil.ElementReader<V> valueReader, MarshallUtil.MapBuilder<K,V,T> builder) throws IOException, ClassNotFoundException
Unmarshall theMap.If the marshalled map is
null, then theMarshallUtil.MapBuilderis not invoked.- Parameters:
in-ObjectInputto read.builder-MarshallUtil.MapBuilderto create the concreteMapimplementation.- Returns:
- The populated
Mapcreated by theMarshallUtil.MapBuilderornull. - Throws:
IOException- If any of the usual Input/Output related exceptions occur.ClassNotFoundException- If the class of a serialized object cannot be found.- See Also:
marshallMap(Map, ElementWriter, ElementWriter, ObjectOutput)
-
marshallUUID
public static void marshallUUID(UUID uuid, ObjectOutput out, boolean checkNull) throws IOException
Marshall theUUIDby sending the most and lest significant bits.This method supports
nullifcheckNullis set totrue.- Parameters:
uuid-UUIDto marshall.out-ObjectOutputto write.checkNull- Iftrue, it checks ifuuidisnull.- Throws:
IOException- If any of the usual Input/Output related exceptions occur.
-
unmarshallUUID
public static UUID unmarshallUUID(ObjectInput in, boolean checkNull) throws IOException
UnmarshallUUID.- Parameters:
in-ObjectInputto read.checkNull- Iftrue, it checks if theUUIDmarshalled wasnull.- Returns:
UUIDmarshalled.- Throws:
IOException- If any of the usual Input/Output related exceptions occur.
-
marshallArray
public static <E> void marshallArray(E[] array, ObjectOutput out) throws IOExceptionMarshall arrays.This method supports
nullarray.- Type Parameters:
E- Array type.- Parameters:
array- Array to marshall.out-ObjectOutputto write.- Throws:
IOException- If any of the usual Input/Output related exceptions occur.
-
unmarshallArray
public static <E> E[] unmarshallArray(ObjectInput in, MarshallUtil.ArrayBuilder<E> builder) throws IOException, ClassNotFoundException
Unmarshall arrays.- Type Parameters:
E- Array type.- Parameters:
in-ObjectInputto read.builder-MarshallUtil.ArrayBuilderto build the array.- Returns:
- The populated array.
- Throws:
IOException- If any of the usual Input/Output related exceptions occur.ClassNotFoundException- If the class of a serialized object cannot be found.
-
marshallCollection
public static <E> void marshallCollection(Collection<E> collection, ObjectOutput out) throws IOException
Marshall aCollection.This method supports
nullcollection.- Type Parameters:
E- Collection's element type.- Parameters:
collection-Collectionto marshal.out-ObjectOutputto write.- Throws:
IOException- If any of the usual Input/Output related exceptions occur.
-
marshallCollection
public static <E> void marshallCollection(Collection<E> collection, ObjectOutput out, MarshallUtil.ElementWriter<E> writer) throws IOException
Marshall aCollection.This method supports
nullcollection.- Type Parameters:
E- Collection's element type.- Parameters:
collection-Collectionto marshal.out-ObjectOutputto write.writer-MarshallUtil.ElementWriterthat writes single element to the output.- Throws:
IOException- If any of the usual Input/Output related exceptions occur.
-
unmarshallCollection
public static <E,T extends Collection<E>> T unmarshallCollection(ObjectInput in, MarshallUtil.CollectionBuilder<E,T> builder, MarshallUtil.ElementReader<E> reader) throws IOException, ClassNotFoundException
Unmarshal aCollection.- Type Parameters:
E- Collection's element type.T-Collectionimplementation.- Parameters:
in-ObjectInputto read.builder-MarshallUtil.CollectionBuilderbuilds the concreteCollectionbased on size.reader-MarshallUtil.ElementReaderreads one element from the input.- Returns:
- The concrete
Collectionimplementation. - Throws:
IOException- If any of the usual Input/Output related exceptions occur.ClassNotFoundException- If the class of a serialized object cannot be found.
-
unmarshallCollection
public static <E,T extends Collection<E>> T unmarshallCollection(ObjectInput in, MarshallUtil.CollectionBuilder<E,T> builder) throws IOException, ClassNotFoundException
Unmarshal aCollection.- Type Parameters:
E- Collection's element type.T-Collectionimplementation.- Parameters:
in-ObjectInputto read.builder-MarshallUtil.CollectionBuilderbuilds the concreteCollectionbased on size.- Returns:
- The concrete
Collectionimplementation. - Throws:
IOException- If any of the usual Input/Output related exceptions occur.ClassNotFoundException- If the class of a serialized object cannot be found.
-
unmarshallCollectionUnbounded
public static <E,T extends Collection<E>> T unmarshallCollectionUnbounded(ObjectInput in, MarshallUtil.UnboundedCollectionBuilder<E,T> builder) throws IOException, ClassNotFoundException
Same asunmarshallCollection(ObjectInput, CollectionBuilder).Used when the size of the
Collectionis not needed for it construction.- Throws:
IOExceptionClassNotFoundException
-
marshallString
public static void marshallString(String string, ObjectOutput out) throws IOException
Marshall theString.Same behavior as
DataOutput.writeUTF(String)but it checks fornull. If thestringis nevernull, it is better to useDataOutput.writeUTF(String).- Parameters:
string-Stringto marshall.out-ObjectOutputto write.- Throws:
IOException- If any of the usual Input/Output related exceptions occur.
-
unmarshallString
public static String unmarshallString(ObjectInput in) throws IOException
Unmarshall aString.- Parameters:
in-ObjectInputto read.- Returns:
- The
Stringornull. - Throws:
IOException- If any of the usual Input/Output related exceptions occur.
-
marshallByteArray
public static void marshallByteArray(byte[] array, ObjectOutput out) throws IOExceptionSame asmarshallArray(Object[], ObjectOutput)but specialized for byte arrays.- Throws:
IOException
-
unmarshallByteArray
public static byte[] unmarshallByteArray(ObjectInput in) throws IOException
Same asunmarshallArray(ObjectInput, ArrayBuilder)but specialized for byte array.No
MarshallUtil.ArrayBuilderis necessary.- Throws:
IOException
-
marshallSize
public static void marshallSize(ObjectOutput out, int value) throws 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 andNULL_VALUEis returned byunmarshallSize(ObjectInput).The integer is marshalled in a variable length from 1 to 5 bytes. Negatives values are always marshalled in 1 byte.
- Parameters:
out-ObjectOutputto write.value- Integer value to marshall.- Throws:
IOException- If any of the usual Input/Output related exceptions occur.
-
unmarshallSize
public static int unmarshallSize(ObjectInput in) throws IOException
Unmarshall an integer.- Parameters:
in-ObjectInputto read.- Returns:
- The integer value or
NULL_VALUEif the original value was negative. - Throws:
IOException- If any of the usual Input/Output related exceptions occur.
-
marshallEnum
public static <E extends Enum<E>> void marshallEnum(E e, ObjectOutput output) throws IOException
- Throws:
IOException
-
unmarshallEnum
public static <E extends Enum<E>> E unmarshallEnum(ObjectInput input, MarshallUtil.EnumBuilder<E> builder) throws IOException
- Throws:
IOException
-
marshallIntCollection
public static void marshallIntCollection(Collection<Integer> collection, ObjectOutput out) throws IOException
Marshalls a collection of integers.- Parameters:
collection- the collection to marshall.out- theObjectOutputto write to.- Throws:
IOException- if an error occurs.
-
unmarshallIntCollection
public static <T extends Collection<Integer>> T unmarshallIntCollection(ObjectInput in, MarshallUtil.CollectionBuilder<Integer,T> builder) throws IOException
Unmarshalls a collection of integers.- Type Parameters:
T- the concrete type of the collection.- Parameters:
in- theObjectInputto read from.builder- theMarshallUtil.CollectionBuilderto build the collection of integer.- Returns:
- the collection.
- Throws:
IOException- if an error occurs.
-
isSafeClass
public static boolean isSafeClass(String className, List<String> whitelist)
Checks whether class name is matched by the class name white list regular expressions provided.- Parameters:
className- class to verifywhitelist- list of regular expressions to match class name against- Returns:
- true if the class matched at least one of the regular expressions, false otherwise
-
-