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 interface
MarshallUtil.ArrayBuilder<E>
static interface
MarshallUtil.CollectionBuilder<E,T extends Collection<E>>
static interface
MarshallUtil.ElementReader<E>
static interface
MarshallUtil.ElementWriter<E>
static interface
MarshallUtil.EnumBuilder<E extends Enum<E>>
static interface
MarshallUtil.MapBuilder<K,V,T extends Map<K,V>>
static interface
MarshallUtil.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 boolean
isSafeClass(String className, List<String> whitelist)
Checks whether class name is matched by the class name white list regular expressions provided.static <E> void
marshallArray(E[] array, ObjectOutput out)
Marshall arrays.static void
marshallByteArray(byte[] array, ObjectOutput out)
Same asmarshallArray(Object[], ObjectOutput)
but specialized for byte arrays.static <E> void
marshallCollection(Collection<E> collection, ObjectOutput out)
Marshall aCollection
.static <E> void
marshallCollection(Collection<E> collection, ObjectOutput out, MarshallUtil.ElementWriter<E> writer)
Marshall aCollection
.static <E extends Enum<E>>
voidmarshallEnum(E e, ObjectOutput output)
static void
marshallIntCollection(Collection<Integer> collection, ObjectOutput out)
Marshalls a collection of integers.static <K,V,T extends Map<K,V>>
voidmarshallMap(T map, ObjectOutput out)
Marshall themap
to theObjectOutput
.static <K,V,T extends Map<K,V>>
voidmarshallMap(T map, MarshallUtil.ElementWriter<K> keyWriter, MarshallUtil.ElementWriter<V> valueWrite, ObjectOutput out)
Marshall themap
to theObjectOutput
.static void
marshallSize(ObjectOutput out, int value)
A special marshall implementation for integer.static void
marshallString(String string, ObjectOutput out)
Marshall theString
.static void
marshallUUID(UUID uuid, ObjectOutput out, boolean checkNull)
Marshall theUUID
by sending the most and lest significant bits.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 int
unmarshallSize(ObjectInput in)
Unmarshall an integer.static String
unmarshallString(ObjectInput in)
Unmarshall aString
.static UUID
unmarshallUUID(ObjectInput in, boolean checkNull)
UnmarshallUUID
.
-
-
-
Method Detail
-
marshallMap
public static <K,V,T extends Map<K,V>> void marshallMap(T map, ObjectOutput out) throws IOException
Marshall themap
to theObjectOutput
.null
maps are supported.- Type Parameters:
K
- Key type of the map.V
- Value type of the map.T
- Type of theMap
.- Parameters:
map
-Map
to marshall.out
-ObjectOutput
to 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.MapBuilder
is not invoked.- Parameters:
in
-ObjectInput
to read.builder
-MarshallUtil.MapBuilder
to create the concreteMap
implementation.- Returns:
- The populated
Map
created by theMarshallUtil.MapBuilder
ornull
. - 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 themap
to theObjectOutput
.null
maps are supported.- Type Parameters:
K
- Key type of the map.V
- Value type of the map.T
- Type of theMap
.- Parameters:
map
-Map
to marshall.out
-ObjectOutput
to 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.MapBuilder
is not invoked.- Parameters:
in
-ObjectInput
to read.builder
-MarshallUtil.MapBuilder
to create the concreteMap
implementation.- Returns:
- The populated
Map
created by theMarshallUtil.MapBuilder
ornull
. - 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 theUUID
by sending the most and lest significant bits.This method supports
null
ifcheckNull
is set totrue
.- Parameters:
uuid
-UUID
to marshall.out
-ObjectOutput
to write.checkNull
- Iftrue
, it checks ifuuid
isnull
.- 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
-ObjectInput
to read.checkNull
- Iftrue
, it checks if theUUID
marshalled wasnull
.- Returns:
UUID
marshalled.- Throws:
IOException
- If any of the usual Input/Output related exceptions occur.
-
marshallArray
public static <E> void marshallArray(E[] array, ObjectOutput out) throws IOException
Marshall arrays.This method supports
null
array
.- Type Parameters:
E
- Array type.- Parameters:
array
- Array to marshall.out
-ObjectOutput
to 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
-ObjectInput
to read.builder
-MarshallUtil.ArrayBuilder
to 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
null
collection
.- Type Parameters:
E
- Collection's element type.- Parameters:
collection
-Collection
to marshal.out
-ObjectOutput
to 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
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:
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
-Collection
implementation.- Parameters:
in
-ObjectInput
to read.builder
-MarshallUtil.CollectionBuilder
builds the concreteCollection
based on size.reader
-MarshallUtil.ElementReader
reads one element from the input.- Returns:
- The concrete
Collection
implementation. - 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
-Collection
implementation.- Parameters:
in
-ObjectInput
to read.builder
-MarshallUtil.CollectionBuilder
builds the concreteCollection
based on size.- Returns:
- The concrete
Collection
implementation. - 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
Collection
is not needed for it construction.- Throws:
IOException
ClassNotFoundException
-
marshallString
public static void marshallString(String string, ObjectOutput out) throws IOException
Marshall theString
.Same behavior as
DataOutput.writeUTF(String)
but it checks fornull
. If thestring
is nevernull
, it is better to useDataOutput.writeUTF(String)
.- Parameters:
string
-String
to marshall.out
-ObjectOutput
to 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
-ObjectInput
to read.- Returns:
- The
String
ornull
. - Throws:
IOException
- If any of the usual Input/Output related exceptions occur.
-
marshallByteArray
public static void marshallByteArray(byte[] array, ObjectOutput out) throws IOException
Same 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.ArrayBuilder
is 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_VALUE
is 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
-ObjectOutput
to 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
-ObjectInput
to read.- Returns:
- The integer value or
NULL_VALUE
if 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
- theObjectOutput
to 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
- theObjectInput
to read from.builder
- theMarshallUtil.CollectionBuilder
to 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
-
-