This class defines methods for complex object to read from and write to object streams. Any method that starts with
readExternal
or writeExternal
implement functionality that read or write the argument
object(s) to a specified stream in a given format. The format of the serialization is unified among the externalizing
functions:
- For collections:
- For maps:
- For iterables:
- If the iterable is
null
, an implementation dependent sentinel enum object is written to the stream to signal that. - An iterator is created, and every element encountered is written out to the output.
- An end-of-iterable sentinel enum object is written to the output, to signal that no more elements are present.
- If the iterable is
- For array:
- Same as for collections.
The elements of collections/maps are written out in the order they are encountered by their iterators.
If the externalized collections are sorted by a comparator, such as SortedSet or SortedMap instances, the utility functions will not write the comparator of the collections. Callers should manually write the comparators of such collections to the stream if necessary. When appropriate, consider sorting these collections by their natural order, so there will be no such problems.
If the collections are concurrently modified during the serialization, meaning that if the externalizing methods fail to enumerate enough elements in respect to the queried size, or there are more elements after size amount of them have been written out, a SerializationConcurrentModificationException will be thrown. The difference count of the written elements can be queried from the exception.
Callers may optionally handle such scenario, but they are recommended to ensure that this never occurs, and fail the serialization completely instead. If this happens, make sure to cease all modifications to the externalized collections by employing some sort of synchronization.
Some functions in this class define themselves to read a specific type of collection from an input, or read them as immutable collections. Using this can greatly reduce the memory footprint of the read objects, and greatly improve the reading performance of deserialization. However, users should use great care when calling these functions.
Important: If an external reading method name starts with readExternalSorted
, then that method
expects the underlying deserialized elements to be sorted in a given order to work. If the underlying elements are
not sorted for the associated comparator, then the returned collection will not work correctly. When using
such methods, make sure that the elements were externalized in appropriate order, or use a deserializing function
that doesn't have this requirement. The reading methods will not throw any exception if the elements are
encountered out of order.
Important: The deserializing methods auto-cast the objects to the required element type. These casts are unchecked, therefore callers may run into runtime errors if the deserialized objects have different type. If callers are unsure about the underlying type of the objects, they should either check the types of the elements, or not use serialization methods provided by this class.
The serializing and deserializing methods are interchangeable, meaning that if a collection of a type was written out
using one of the externalizing methods, then it may be deserialized by any collection deserializing methods in this
class. E.g. A Set is written out using writeExternalCollection(
Methods that use custom serialization for objects are only interchangeable with other functions that use the same
custom serialization functions. (Such as
writeExternalCollection(
In the documentation of the externalizing methods, there will be an identifier for the format of the serialized data. Check these identifiers in the methods to help identify the interchangeable methods.
If you're using methods in this class, make sure that you understand the inner workings, and implications of them.
public static byte | readByteFromBuffer( Reads a byte from the argument buffer at the given offset. |
public static char | readCharFromBuffer( Reads a char from the argument buffer at the given offset. |
public static < | readExternalArray( Reads an array from the object input using a custom deserializer for its elements. |
public static < | readExternalArray( Reads an array from the object input. |
public static < | Reads a ArrayList from the object input. |
public static byte[] | Reads a byte array from the object input. |
public static < | readExternalCollection( Reads a collection from the object input using a custom deserializer and adds its elements to the argument
collection. |
public static < | readExternalCollection( Reads a collection from the object input and adds its elements to the argument collection. |
public static < | readExternalCollectionSize( Reads a collection from the object input, that is instantiated by the argument supplier. |
public static < | readExternalEnumSetCollection( Reads an EnumSet from the object input. |
public static < | readExternalExternalizableCollection( Reads a collection of externalizable elements and adds its elements to the argument collection. |
public static < | readExternalExternalizableMap( Reads a map containing Externalizable keys and values from the object input. |
public static < | Reads a HashMap from the object input. |
public static < | Reads a HashSet from the object input. |
public static < | Reads an immutable hash map from the object input. |
public static < | Reads an immutable hash set from the object input. |
public static < | Reads an immutable identity hash map from the object input. |
public static < | Reads an immutable linked hash map from the object input. |
public static < | Reads an immutable linked hash set from the object input. |
public static < | readExternalImmutableList( Reads an immutable list from the object input using a custom deserializer for its elements. |
public static < | Reads an immutable list from the object input. |
public static < | Reads an immutable navigable map from the object input. |
public static < | readExternalImmutableNavigableMap( Reads an immutable navigable map from the object input. |
public static < | Reads an immutable navigable set from the object input. |
public static < | readExternalImmutableNavigableSet( Reads an immutable navigable set with the given sorting order from the object input. |
public static < | Reads an iterable from the object input. |
public static < | readExternalIterable( Reads an iterable from the object input and adds the elements to the argument collection. |
public static < | readExternalKeyExternalizableMap( Reads a map containing Externalizable keys from the object input. |
public static < | Reads a LinkedHashMap from the object input. |
public static < | Reads a LinkedHashSet from the object input. |
public static < | readExternalMap( Reads a map from the object input using a custom deserializer for its keys and values, and adds its entries to
the argument map. |
public static < | readExternalMap( Reads a map from the object input and puts its entries to the argument map. |
public static < | readExternalMapSize( Reads a map from the object input, that is instantiated by the argument supplier. |
public static < | Reads an objects from the object input. |
public static < | readExternalSortedImmutableNavigableMap( Reads an immutable navigable map from the object input using a custom deserializer for its keys and values, given
that the deserialized entries are already in order for the argument comparator. |
public static < | readExternalSortedImmutableNavigableMap( Reads an immutable navigable map from the object input using a custom deserializer for its keys and values, given
that the deserialized entries are already in order. |
public static < | Reads an immutable navigable map from the object input given that the deserialized entries are already in order. |
public static < | readExternalSortedImmutableNavigableMap( Reads an immutable navigable map from the object input given that the deserialized entries are already in order
for the argument comparator. |
public static < | Reads an immutable navigable set from the object input given that the deserialized objects are already in order. |
public static < | readExternalSortedImmutableNavigableSet( Reads an immutable navigable set from the object input given that the deserialized objects are already in order
for the argument comparator. |
public static < | readExternalSortedTreeMap( Reads a tree map map from the object input using a custom deserializer for its keys and values, given that the
deserialized entries are already in order for the argument comparator. |
public static < | readExternalSortedTreeMap( Reads a tree map from the object input using a custom deserializer for its keys and values, given that the
deserialized entries are already in order. |
public static < | Reads a tree map from the object input given that the deserialized entries are already in order. |
public static < | readExternalSortedTreeMap( Reads a tree map from the object input given that the deserialized entries are already in order for the argument
comparator. |
public static < | Reads a tree set from the object input given that the deserialized objects are already in order. |
public static < | readExternalSortedTreeSet( Reads a tree set from the object input given that the deserialized objects are already in order for the argument
comparator. |
public static StackTraceElement | Reads an externalized stack trace element from the object input. |
public static < | readExternalUTFCollection( Reads a collection of string elements from the object input. |
public static < | readExternalValueExternalizableMap( Reads a map containing Externalizable values from the object input. |
public static int | readIntFromBuffer( Reads a int from the argument buffer at the given offset. |
public static long | readLongFromBuffer( Reads a long from the argument buffer at the given offset. |
public static short | readShortFromBuffer( Reads a short from the argument buffer at the given offset. |
public static void | writeByteToBuffer( Writes a byte to the argument buffer at the given offset. |
public static void | writeCharToBuffer( Writes a char to the argument buffer at the given offset. |
public static < | writeExternalArray( Writes an array to the object output using the custom element serializer. |
public static void | writeExternalArray( Writes an array to the object output. |
public static void | writeExternalArray( Writes a specific range of an array to the object output. |
public static void | writeExternalByteArray( Writes a byte array to the object output. |
public static void | writeExternalByteArray( Writes the given range of the argument byte array to the object output. |
public static < | writeExternalCollection( Writes a collection to the object output using a custom serializer. |
public static void | writeExternalCollection( Writes a collection to the object output. |
public static void | writeExternalExternalizableCollection( Writes a collection of externalizable elements to the object output. |
public static < | writeExternalExternalizableMap( Writes a map containing Externalizable keys and values to the object output. |
public static void | writeExternalIterable( Writes an iterable to the object output. |
public static < | writeExternalKeyExternalizableMap( Writes a map containing Externalizable keys to the object output. |
public static < | writeExternalMap( Writes a map to the object output using the custom key and value serializers. |
public static void | writeExternalMap( Writes a map to the object output. |
public static void | writeExternalObject( Writes an object to the object output. |
public static void | Writes a stack trace element externally to the object output. |
public static void | writeExternalUTFCollection( Writes a collection of string elements to the object output. |
public static < | writeExternalValueExternalizableMap( Writes a map containing Externalizable values to the object output. |
public static void | writeIntToBuffer( Writes a int to the argument buffer at the given offset. |
public static void | writeLongToBuffer( Writes a long to the argument buffer at the given offset. |
public static void | writeShortToBuffer( Writes a short to the argument buffer at the given offset. |
byte
from the argument buffer at the given offset.byte
buffer.byte
from.byte
read from the given offset.null
.char
from the argument buffer at the given offset.
The char
is read in big-endian order.
byte
buffer.char
from.char
read from the given offset.null
.
Format identifier: collection-object
null
if a null
collection was deserialized.null
.
Format identifier: collection-object
null
if a null
collection was deserialized.null
.
Format identifier: collection-object
The elements of the returned array list is uncheckedly casted to the result element type.
Same as:
readExternalCollectionSize(in, ArrayList::new);
null
if a null
collection was deserialized.null
.
Format identifier: collection-byte
null
if a null
collection was deserialized.null
.
Format identifier: collection-custom
The caller is responsible for passing the reader function that reads the object in the same format as it was written out.
null
if a null
collection was deserialized.null
.
Format identifier: collection-object
The element type of the deserialized objects are uncheckedly casted.
null
if a null
collection was deserialized.null
.
Format identifier: collection-object
The argument collection supplier can be used to construct an appropriate collection for a given number of
elements.
Example use-case:
readExternalCollectionSize(ArrayList::new);
The element type of the deserialized objects are uncheckedly casted.
null
or the same collection returned by the
supplier.null
.
Format identifier: collection-object
null
if a null
collection was deserialized.null
.
Format identifier: collection-externalizable
null
if a null
collection was deserialized.null
.
Format identifier: map-externalizable-externalizable
null
if a null
map was deserialized.null
.
Format identifier: map-object-object
The key and value types of the deserialized entries are uncheckedly casted.
Similar to:
readExternalMap(new HashMap<>(), in);
null
if a null
map was deserialized.null
.
Format identifier: collection-object
The elements of the returned hash set is uncheckedly casted to the result element type.
Similar to:
readExternalCollection(new HashSet<>(), in);
null
if a null
collection was deserialized.null
.
Format identifier: map-object-object
The key and value types of the deserialized entries are uncheckedly casted.
The iteration order in the returned map might not be the same as order of the deserialized entries.
null
if a null
map was deserialized.null
.
Format identifier: collection-object
The element type of the deserialized objects are uncheckedly casted.
The iteration order in the returned set might not be the same as order of the deserialized objects.
null
if a null
collection was deserialized.null
.
Format identifier: map-object-object
The key and value types of the deserialized entries are uncheckedly casted.
The iteration order in the returned map might not be the same as order of the deserialized entries.
null
if a null
map was deserialized.null
.
Format identifier: map-object-object
The key and value types of the deserialized entries are uncheckedly casted.
The iteration order in the returned map is the same as order of the deserialized entries.
null
if a null
map was deserialized.null
.
Format identifier: collection-object
The element type of the deserialized objects are uncheckedly casted.
The iteration order in the returned set is the same as order of the deserialized objects.
null
if a null
collection was deserialized.null
.
Format identifier: collection-custom
null
if a null
collection was deserialized.null
.
Format identifier: collection-object
The element type of the deserialized objects are uncheckedly casted.
null
if a null
collection was deserialized.null
.
Format identifier: iterable-object
The elements of the returned iterable is uncheckedly casted to the result element type.
null
.
Format identifier: iterable-object
The element type of the deserialized objects are uncheckedly casted.
null
if a null
iterable was deserialized.null
.
Format identifier: map-externalizable-object
The key types of the deserialized entries are uncheckedly casted.
null
if a null
map was deserialized.null
.
Format identifier: map-object-object
The key and value types of the deserialized entries are uncheckedly casted.
Similar to:
readExternalMap(new LinkedHashMap<>(), in);
null
if a null
map was deserialized.null
.
Format identifier: collection-object
The elements of the returned linked hash set is uncheckedly casted to the result element type.
Similar to:
readExternalCollection(new LinkedHashSet<>(), in);
null
if a null
collection was deserialized.null
.
Format identifier: map-custom-custom
The caller is responsible for passing the reader functions that reads the objects in the same format as it was written out.
null
if a null
map was deserialized.null
.
Format identifier: map-object-object
The key and value types of the deserialized entries are uncheckedly casted.
null
if a null
map was deserialized.null
.
Format identifier: map-object-object
The argument map supplier can be used to construct an appropriate map for a given number of entries.
Example use-case:
readExternalMapSize(size -> new HashMap<>(size * 4 / 3 + 1));
The key and value types of the deserialized entries are uncheckedly casted.
null
or the same map returned by the
supplier.null
.
Format identifer: object
This method simply calls ObjectInput.readObject(), and uncheckedly casts it to the return type.
null
.Important: This method relies on that the deserialized entries are ordered by the specified comparator. If this requirement is violated, the returned map will not work correctly.
Format identifier: map-custom-custom
If you're unsure whether the serialized entries are in appropriate order, use:
readExternalMap(new TreeMap<>(comparator), in, keyreader, valuereader);
null
means the natural order.null
if a null
map was deserialized.null
.Important: This method relies on that the deserialized entries are ordered by their natural order. If this requirement is violated, the returned map will not work correctly.
Format identifier: map-custom-custom
Same as:
readExternalSortedTreeMap(in, null, keyreader, valuereader);
If you're unsure whether the serialized entries are in appropriate order, use:
readExternalMap(new TreeMap<>(), in, keyreader, valuereader);
null
if a null
map was deserialized.null
.Important: This method relies on that the deserialized entries are ordered by their natural order. If this requirement is violated, the returned map will not work correctly.
Format identifier: map-object-object
The key and value types of the deserialized entries are uncheckedly casted.
Same as:
readExternalSortedTreeMap(in, null);If you're unsure whether the serialized entries are in appropriate order, use:
readExternalMap(new TreeMap<>(), in);
null
if a null
map was deserialized.null
.Important: This method relies on that the deserialized entries are ordered by the specified comparator. If this requirement is violated, the returned map will not work correctly.
Format identifier: map-object-object
The key and value types of the deserialized entries are uncheckedly casted.
If you're unsure whether the serialized entries are in appropriate order, use:
readExternalMap(new TreeMap<>(comparator), in);
null
means the natural order.null
if a null
map was deserialized.null
.Important: This method relies on that the deserialized objects are ordered by their natural order. If this requirement is violated, the returned set will not work correctly.
Format identifier: collection-object
The element type of the deserialized objects are uncheckedly casted.
Same as:
readExternalSortedTreeSet(in, null);If you're unsure whether the serialized entries are in appropriate order, use:
readExternalCollection(new TreeSet<>(), in);
null
if a null
collection was deserialized.null
.Important: This method relies on that the deserialized objects are ordered by the specified comparator. If this requirement is violated, the returned set will not work correctly.
Format identifier: collection-object
The element type of the deserialized objects are uncheckedly casted.
If you're unsure whether the serialized entries are in appropriate order, use:
readExternalCollection(new TreeSet<>(comparator), in);
null
means the natural order.null
if a null
collection was deserialized.null
.
Format identifier: stack_trace_element
See writeExternalStackTraceElement(
null
.
Format identifier: collection-utf
null
if a null
collection was deserialized.null
.
Format identifier: map-object-externalizable
The value types of the deserialized entries are uncheckedly casted.
null
if a null
map was deserialized.null
.int
from the argument buffer at the given offset.
The int
is read in big-endian order.
byte
buffer.itn
from.int
read from the given offset.null
.long
from the argument buffer at the given offset.
The long
is read in big-endian order.
byte
buffer.long
from.long
read from the given offset.null
.short
from the argument buffer at the given offset.
The short
is read in big-endian order.
byte
buffer.short
from.short
read from the given offset.null
.byte
to the argument buffer at the given offset.byte
buffer.byte
to.null
.char
to the argument buffer at the given offset.
The char
is written in big-endian order.
byte
buffer.char
to.null
.
Format identifier: collection-custom
null
.
Format identifier: collection-object
null
.
Format identifier: collection-object
null
.
Format identifier: collection-byte
null
.
Format identifier: collection-byte
null
.
Format identifier: collection-custom
null
.
Format identifier: collection-object
null
.
Format identifier: collection-externalizable
The collection may not contain null
elements.
null
.
Format identifier: map-externalizable-externalizable
null
.
Format identifier: iterable-object
null
.
Format identifier: map-externalizable-object
null
.
Format identifier: map-custom-custom
null
.
Format identifier: map-object-object
null
.
Format identifer: object
This method calls ObjectOutput.writeObject(
null
.
Format identifier: stack_trace_element
The format of an externalized stack trace element is the following:
- Class name. (
utf
) - Method name. (
utf
) - File name. (
object
) - Line number. (
int
)
null
.
Format identifier: collection-utf
null
. If the object output doesn't allow null
strings to
be written out, and any of the elements are null
.
Format identifier: map-object-externalizable
null
.int
to the argument buffer at the given offset.
The int
is written in big-endian order.
byte
buffer.int
to.null
.long
to the argument buffer at the given offset.
The long
is written in big-endian order.
byte
buffer.long
to.null
.short
to the argument buffer at the given offset.
The short
is written in big-endian order.
byte
buffer.short
to.null
.