Class MarshallUtil
- Since:
- 4.0
- Author:
- Galder ZamarreƱo
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfacestatic interfaceMarshallUtil.CollectionBuilder<E,T extends Collection<E>> static interfacestatic interfacestatic interfaceMarshallUtil.EnumBuilder<E extends Enum<E>>static interfacestatic interfaceMarshallUtil.UnboundedCollectionBuilder<E,T extends Collection<E>> -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic booleanisSafeClass(String className, List<String> allowList) 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(DataOutput 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[]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 intUnmarshall an integer.static StringUnmarshall aString.static UUIDunmarshallUUID(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 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 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.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
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 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 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.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:
-
marshallUUID
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
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
Marshall 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, ClassNotFoundExceptionUnmarshal 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, ClassNotFoundExceptionUnmarshal 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, ClassNotFoundExceptionSame asunmarshallCollection(ObjectInput, CollectionBuilder).Used when the size of the
Collectionis not needed for it construction.- Throws:
IOExceptionClassNotFoundException
-
marshallString
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
Unmarshall aString.- Parameters:
in-ObjectInputto read.- Returns:
- The
Stringornull. - 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.ArrayBuilderis 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_VALUEis 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-ObjectOutputto write.value- Integer value to marshall.- Throws:
IOException- If any of the usual Input/Output related exceptions occur.
-
unmarshallSize
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
- 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 IOExceptionUnmarshalls 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
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
-