Class MarshallUtil
- Since:
- 4.0
- Author:
- Galder ZamarreƱo
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
static interface
MarshallUtil.CollectionBuilder<E,
T extends Collection<E>> static interface
static interface
static interface
MarshallUtil.EnumBuilder<E extends Enum<E>>
static interface
static interface
MarshallUtil.UnboundedCollectionBuilder<E,
T extends Collection<E>> -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic boolean
isSafeClass
(String className, List<String> allowList) 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
(DataOutput 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 byte[]
toByteArray
(ByteBuffer buf) static <E> E[]
unmarshallArray
(ObjectInput in, MarshallUtil.ArrayBuilder<E> builder) Unmarshall arrays.static byte[]
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
Unmarshall an integer.static String
Unmarshall aString
.static UUID
unmarshallUUID
(ObjectInput in, boolean checkNull) UnmarshallUUID
.
-
Constructor Details
-
MarshallUtil
public MarshallUtil()
-
-
Method Details
-
toByteArray
-
marshallMap
public static <K,V, void marshallMapT extends Map<K, V>> (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 unmarshallMapT extends Map<K, V>> (ObjectInput in, MarshallUtil.MapBuilder<K, V, throws IOException, ClassNotFoundExceptionT> builder) 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
public static <K,V, void marshallMapT extends Map<K, V>> (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 unmarshallMapT extends Map<K, V>> (ObjectInput in, MarshallUtil.ElementReader<K> keyReader, MarshallUtil.ElementReader<V> valueReader, MarshallUtil.MapBuilder<K, V, throws IOException, ClassNotFoundExceptionT> builder) 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:
-
marshallUUID
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
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
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, ClassNotFoundExceptionUnmarshal 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, ClassNotFoundExceptionUnmarshal 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, ClassNotFoundExceptionSame asunmarshallCollection(ObjectInput, CollectionBuilder)
.Used when the size of the
Collection
is not needed for it construction.- Throws:
IOException
ClassNotFoundException
-
marshallString
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
Unmarshall aString
.- Parameters:
in
-ObjectInput
to read.- Returns:
- The
String
ornull
. - Throws:
IOException
- If any of the usual Input/Output related exceptions occur.
-
marshallByteArray
Same asmarshallArray(Object[], ObjectOutput)
but specialized for byte arrays.- Throws:
IOException
-
unmarshallByteArray
Same asunmarshallArray(ObjectInput, ArrayBuilder)
but specialized for byte array.No
MarshallUtil.ArrayBuilder
is necessary.- Throws:
IOException
-
marshallSize
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(DataInput)
.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
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
- 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 IOExceptionUnmarshalls 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
Checks whether class name is matched by the class name white list regular expressions provided.- Parameters:
className
- class to verifyallowList
- list of regular expressions to match class name against- Returns:
- true if the class matched at least one of the regular expressions, false otherwise
-