Interface RevisionDataOutput
-
- All Superinterfaces:
java.io.DataOutput,DirectDataOutput
public interface RevisionDataOutput extends java.io.DataOutput, DirectDataOutput
Extension to DataOutput that adds support for a few new constructs and supports formatting a Serialization Revision. An instance of a RevisionDataOutput is created for each Serialization Revision and closed when that Revision's serialization is done - it is not shared between multiple revisions. This interface is designed to serialize data that can be consumed usingRevisionDataInput.
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static interfaceRevisionDataOutput.ElementSerializer<T>Defines a Function signature that can serialize an element to aRevisionDataOutput.
-
Field Summary
Fields Modifier and Type Field Description static intCOMPACT_INT_MAXMaximum value that can be encoded usingwriteCompactInt(int).static intCOMPACT_INT_MINMinimum value that can be encoded usingwriteCompactInt(int).static longCOMPACT_LONG_MAXMaximum value that can be encoded usingwriteCompactLong(long).static longCOMPACT_LONG_MINMinimum value that can be encoded usingwriteCompactLong(long).static longCOMPACT_SIGNED_LONG_MAXMaximum value that can be encoded usingwriteCompactSignedLong(long).static longCOMPACT_SIGNED_LONG_MINMinimum value that can be encoded usingwriteCompactSignedLong(long).static intUUID_BYTESThe number of bytes required to encode a UUID.
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description java.io.OutputStreamgetBaseStream()Gets a pointer to the OutputStream that this RevisionDataOutput writes to.intgetCollectionLength(int elementCount, int elementLength)Calculates the number of bytes required to serialize a Collection or array.<T> intgetCollectionLength(java.util.Collection<T> collection, java.util.function.ToIntFunction<T> elementLengthProvider)Calculates the number of bytes required to serialize a Collection.<T> intgetCollectionLength(T[] array, java.util.function.ToIntFunction<T> elementLengthProvider)Calculates the number of bytes required to serialize an array.intgetCompactIntLength(int value)Calculates the length, in bytes, of the given Integer as serialized by usingwriteCompactInt(int).intgetCompactLongLength(long value)Calculates the length, in bytes, of the given Long as serialized by usingwriteCompactLong(long).intgetCompactSignedLongLength(long value)Calculates the length, in bytes, of the given signed Long as serialized by usingwriteCompactSignedLong(long).intgetMapLength(int elementCount, int keyLength, int valueLength)Calculates the number of bytes required to serialize a Map.<K,V>
intgetMapLength(java.util.Map<K,V> map, java.util.function.ToIntFunction<K> keyLengthProvider, java.util.function.ToIntFunction<V> valueLengthProvider)Calculates the number of bytes required to serialize a Map.intgetUTFLength(java.lang.String s)Calculates the length, in bytes, of the given String as serialized by usingDataOutput.writeUTF(java.lang.String).voidlength(int length)IfrequiresExplicitLength()== true, this method will write 4 bytes at the current position representing the expected serialization length (via the argument).booleanrequiresExplicitLength()Gets a value indicating whether this instance of a RevisionDataOutput requireslength(int)to be called prior to writing anything to it.default voidwriteArray(byte[] array)Serializes the given byte array.voidwriteArray(byte[] array, int offset, int length)Serializes the given byte array.<T> voidwriteArray(T[] array, RevisionDataOutput.ElementSerializer<T> elementSerializer)Serializes the given array using the givenRevisionDataOutput.ElementSerializer.voidwriteBuffer(BufferView buffer)Serializes the givenBufferView.<T> voidwriteCollection(java.util.Collection<T> collection, RevisionDataOutput.ElementSerializer<T> elementSerializer)Serializes the given Collection using the givenRevisionDataOutput.ElementSerializer.voidwriteCompactInt(int value)Encodes the given Integer into a compact serialization of 1, 2, 3 or 4 bytes.voidwriteCompactLong(long value)Encodes the given Long into a compact serialization of 1, 2, 4 or 8 bytes.voidwriteCompactSignedLong(long value)Encodes the given signed Long into a compact serialization of 1, 2, 4 or 8 bytes.<K,V>
voidwriteMap(java.util.Map<K,V> map, RevisionDataOutput.ElementSerializer<K> keySerializer, RevisionDataOutput.ElementSerializer<V> valueSerializer)Serializes the given Map using the givenRevisionDataOutput.ElementSerializers (one for Key and one for Value).voidwriteUUID(java.util.UUID uuid)Serializes the given UUID as two consecutive Long values.-
Methods inherited from interface java.io.DataOutput
write, write, write, writeBoolean, writeByte, writeBytes, writeChar, writeChars, writeDouble, writeFloat, writeInt, writeLong, writeShort, writeUTF
-
Methods inherited from interface io.pravega.common.io.DirectDataOutput
writeInt, writeLong, writeShort
-
-
-
-
Field Detail
-
COMPACT_LONG_MAX
static final long COMPACT_LONG_MAX
Maximum value that can be encoded usingwriteCompactLong(long).- See Also:
- Constant Field Values
-
COMPACT_LONG_MIN
static final long COMPACT_LONG_MIN
Minimum value that can be encoded usingwriteCompactLong(long).- See Also:
- Constant Field Values
-
COMPACT_SIGNED_LONG_MAX
static final long COMPACT_SIGNED_LONG_MAX
Maximum value that can be encoded usingwriteCompactSignedLong(long).- See Also:
- Constant Field Values
-
COMPACT_SIGNED_LONG_MIN
static final long COMPACT_SIGNED_LONG_MIN
Minimum value that can be encoded usingwriteCompactSignedLong(long).- See Also:
- Constant Field Values
-
COMPACT_INT_MAX
static final int COMPACT_INT_MAX
Maximum value that can be encoded usingwriteCompactInt(int).- See Also:
- Constant Field Values
-
COMPACT_INT_MIN
static final int COMPACT_INT_MIN
Minimum value that can be encoded usingwriteCompactInt(int).- See Also:
- Constant Field Values
-
UUID_BYTES
static final int UUID_BYTES
The number of bytes required to encode a UUID.- See Also:
- Constant Field Values
-
-
Method Detail
-
requiresExplicitLength
boolean requiresExplicitLength()
Gets a value indicating whether this instance of a RevisionDataOutput requireslength(int)to be called prior to writing anything to it.- Returns:
- True if Length must be declared beforehand (by invoking length()) or not.
-
length
void length(int length) throws java.io.IOExceptionIfrequiresExplicitLength()== true, this method will write 4 bytes at the current position representing the expected serialization length (via the argument). In this case, this method must be called prior to invoking any write method on this object. IfrequiresExplicitLength()== false, this method will have no effect, since the length can be auto-calculated when the RevisionDataOutput is closed.- Parameters:
length- The length to declare.- Throws:
java.io.IOException- If an IO Exception occurred.
-
getBaseStream
java.io.OutputStream getBaseStream()
Gets a pointer to the OutputStream that this RevisionDataOutput writes to.- Returns:
- The OutputStream.
-
getUTFLength
int getUTFLength(java.lang.String s)
Calculates the length, in bytes, of the given String as serialized by usingDataOutput.writeUTF(java.lang.String). Invoking this method will not actually write the String.- Parameters:
s- The string to measure.- Returns:
- The
DataOutput.writeUTF(java.lang.String)length of the String. Note that this may be different from s.length().
-
getCompactLongLength
int getCompactLongLength(long value)
Calculates the length, in bytes, of the given Long as serialized by usingwriteCompactLong(long). Invoking this method will not actually write the value.- Parameters:
value- The value to measure.- Returns:
- The
writeCompactLong(long)length of the value. This is a value between 1 andLong.BYTES(inclusive). - Throws:
java.lang.IllegalArgumentException- If value is not in the interval [0, 2^62).
-
writeCompactLong
void writeCompactLong(long value) throws java.io.IOExceptionEncodes the given Long into a compact serialization of 1, 2, 4 or 8 bytes. The actual number of bytes can be calculated usinggetCompactLongLength(long). The first two bits of the given value are ignored and will be reserved for serialization, hence this can only serialize values in the interval [0, 2^62). This value must be read usingRevisionDataInput.readCompactLong(). It cannot be read usingDataInput.readLong()or usingRevisionDataInput.readCompactSignedLong().- Parameters:
value- The value to serialize.- Throws:
java.io.IOException- If an IO Exception occurred.java.lang.IllegalArgumentException- If value is not in the interval [0, 2^62).
-
getCompactSignedLongLength
int getCompactSignedLongLength(long value)
Calculates the length, in bytes, of the given signed Long as serialized by usingwriteCompactSignedLong(long). Invoking this method will not actually write the value.- Parameters:
value- The value to measure.- Returns:
- The
writeCompactSignedLong(long)length of the value. This is a value between 1 andLong.BYTES(inclusive). - Throws:
java.lang.IllegalArgumentException- If value is not in the interval [-2^61, 2^61).
-
writeCompactSignedLong
void writeCompactSignedLong(long value) throws java.io.IOExceptionEncodes the given signed Long into a compact serialization of 1, 2, 4 or 8 bytes. The actual number of bytes can be calculated usinggetCompactSignedLongLength(long). The first three bits of the given value are ignored and will be reserved for serialization, hence this can only serialize values in the interval [-2^61, 2^61).This value must be read using
RevisionDataInput.readCompactSignedLong(). It cannot be read usingDataInput.readLong()or usingRevisionDataInput.readCompactLong().- Parameters:
value- The value to serialize.- Throws:
java.io.IOException- If an IO Exception occurred.java.lang.IllegalArgumentException- If value is not in the interval [-2^61, 2^61).
-
getCompactIntLength
int getCompactIntLength(int value)
Calculates the length, in bytes, of the given Integer as serialized by usingwriteCompactInt(int). Invoking this method will not actually write the value.- Parameters:
value- The value to measure.- Returns:
- The
writeCompactInt(int)length of the value. This is a value between 1 andInteger.BYTES(inclusive). - Throws:
java.lang.IllegalArgumentException- If value is not in the interval [0, 2^30).
-
writeCompactInt
void writeCompactInt(int value) throws java.io.IOExceptionEncodes the given Integer into a compact serialization of 1, 2, 3 or 4 bytes. The actual number of bytes can be calculated usinggetCompactIntLength(int). The first two bits of the given value are ignored and will be reserved or serialization, hence this can only serialize values in the interval [0, 2^30). This value must be read usingRevisionDataInput.readCompactInt(). It cannot be read usingDataInput.readInt().- Parameters:
value- The value to serialize.- Throws:
java.io.IOException- If an IO Exception occurred.java.lang.IllegalArgumentException- If value is not in the interval [0, 2^30).
-
writeUUID
void writeUUID(java.util.UUID uuid) throws java.io.IOExceptionSerializes the given UUID as two consecutive Long values. This value must be read usingRevisionDataInput.readUUID().- Parameters:
uuid- The UUID to serialize.- Throws:
java.io.IOException- If an IO Exception occurred.
-
getCollectionLength
int getCollectionLength(int elementCount, int elementLength)Calculates the number of bytes required to serialize a Collection or array. This method can be used to estimate the serialization length of bothwriteCollection(java.util.Collection<T>, io.pravega.common.io.serialization.RevisionDataOutput.ElementSerializer<T>)andwriteArray(T[], io.pravega.common.io.serialization.RevisionDataOutput.ElementSerializer<T>).- Parameters:
elementCount- The size of the collection.elementLength- The size (in bytes) of each element's serialization.- Returns:
- The number of bytes.
-
getCollectionLength
<T> int getCollectionLength(java.util.Collection<T> collection, java.util.function.ToIntFunction<T> elementLengthProvider)Calculates the number of bytes required to serialize a Collection. This method can be used to estimate the serialization length ofwriteCollection(java.util.Collection<T>, io.pravega.common.io.serialization.RevisionDataOutput.ElementSerializer<T>).- Type Parameters:
T- Type of the Collection's Elements.- Parameters:
collection- The Collection to measure.elementLengthProvider- A Function that, given an Element of type T, will return its serialization length.- Returns:
- The number of bytes.
-
getCollectionLength
<T> int getCollectionLength(T[] array, java.util.function.ToIntFunction<T> elementLengthProvider)Calculates the number of bytes required to serialize an array. This method can be used to estimate the serialization length ofwriteArray(T[], io.pravega.common.io.serialization.RevisionDataOutput.ElementSerializer<T>).- Type Parameters:
T- Type of the Array's Elements- Parameters:
array- The array to measure.elementLengthProvider- A Function that, given an Element of type T, will return its serialization length.- Returns:
- The number of bytes.
-
writeCollection
<T> void writeCollection(java.util.Collection<T> collection, RevisionDataOutput.ElementSerializer<T> elementSerializer) throws java.io.IOExceptionSerializes the given Collection using the givenRevisionDataOutput.ElementSerializer. It first writes a Compact Integer representing the number of elements in the collection, followed by each element's serialization, in the same order as returned by the Collection's iterator.- Type Parameters:
T- Type of the elements in the Collection.- Parameters:
collection- The Collection to serialize. Can be null (in which case an Empty Collection will be deserialized byRevisionDataInput.readCollection(io.pravega.common.io.serialization.RevisionDataInput.ElementDeserializer<T>)).elementSerializer- A Function that serializes a single element of the collection to aRevisionDataOutput.- Throws:
java.io.IOException- If an IO Exception occurred.
-
writeArray
<T> void writeArray(T[] array, RevisionDataOutput.ElementSerializer<T> elementSerializer) throws java.io.IOExceptionSerializes the given array using the givenRevisionDataOutput.ElementSerializer. It first writes a Compact Integer representing the number of elements in the array, followed by each element's serialization, in the order in which they appear in the array.- Type Parameters:
T- Type of the elements in the array.- Parameters:
array- The array to serialize. Can be null (in which case an Empty array will be deserialized byRevisionDataInput.readArray(io.pravega.common.io.serialization.RevisionDataInput.ElementDeserializer<T>, java.util.function.IntFunction<T[]>)).elementSerializer- A Function that serializes a single element of the array to aRevisionDataOutput.- Throws:
java.io.IOException- If an IO Exception occurred.
-
writeArray
default void writeArray(byte[] array) throws java.io.IOExceptionSerializes the given byte array. Equivalent to callingwriteArray(T[], io.pravega.common.io.serialization.RevisionDataOutput.ElementSerializer<T>)(array, 0, array.length)}.- Parameters:
array- The array to serialize. Can be null (in which case an Empty array will be deserialized byRevisionDataInput.readArray(io.pravega.common.io.serialization.RevisionDataInput.ElementDeserializer<T>, java.util.function.IntFunction<T[]>)).- Throws:
java.io.IOException- If an IO Exception occurred.
-
writeArray
void writeArray(byte[] array, int offset, int length) throws java.io.IOExceptionSerializes the given byte array. It first writes a Compact Integer representing the length to serialize, followed by the actual array elements being written.- Parameters:
array- The array to serialize. Can be null (in which case an Empty array will be deserialized byRevisionDataInput.readArray(io.pravega.common.io.serialization.RevisionDataInput.ElementDeserializer<T>, java.util.function.IntFunction<T[]>))).offset- The offset within the array to begin serializing at. This is ignored if array == null.length- The number of elements to serialize. This is ignored if array == null.- Throws:
java.io.IOException- If an IO Exception occurred.
-
writeBuffer
void writeBuffer(BufferView buffer) throws java.io.IOException
Serializes the givenBufferView. It first writes a Compact Integer representingBufferView.getLength(), followed byBufferView.getLength()bytes representing the data from theBufferView. This can be read back usingRevisionDataInput.readArray()as this method serializes the data in the same ways aswriteArray(T[], io.pravega.common.io.serialization.RevisionDataOutput.ElementSerializer<T>).- Specified by:
writeBufferin interfaceDirectDataOutput- Parameters:
buffer- TheBufferViewto serialize. Can be null (in which case an Empty array will be deserialized byRevisionDataInput.readArray(io.pravega.common.io.serialization.RevisionDataInput.ElementDeserializer<T>, java.util.function.IntFunction<T[]>))).- Throws:
java.io.IOException- If an IO Exception occurred.
-
getMapLength
int getMapLength(int elementCount, int keyLength, int valueLength)Calculates the number of bytes required to serialize a Map.- Parameters:
elementCount- The size of the Map.keyLength- The size (in bytes) of each key's serialization.valueLength- The size (in bytes) of each value's serialization.- Returns:
- The number of bytes.
-
getMapLength
<K,V> int getMapLength(java.util.Map<K,V> map, java.util.function.ToIntFunction<K> keyLengthProvider, java.util.function.ToIntFunction<V> valueLengthProvider)Calculates the number of bytes required to serialize a Map.- Type Parameters:
K- Type of the Map's Keys.V- Type of the Map's Values.- Parameters:
map- The Map to measure.keyLengthProvider- A Function that, given a Key of type K, will return its serialization length.valueLengthProvider- A Function that, given a Value of type V, will return its serialization length.- Returns:
- The number of bytes.
-
writeMap
<K,V> void writeMap(java.util.Map<K,V> map, RevisionDataOutput.ElementSerializer<K> keySerializer, RevisionDataOutput.ElementSerializer<V> valueSerializer) throws java.io.IOExceptionSerializes the given Map using the givenRevisionDataOutput.ElementSerializers (one for Key and one for Value). It first writes a Compact Integer representing the number of elements in the Map, followed by each pair's serialization (first the key, then the value), in the same order as returned by the Map's iterator.- Type Parameters:
K- Type of the Map's Keys.V- Type of the Map's Values.- Parameters:
map- The Map to serialize. Can be null (in which case an Empty Map will be deserialized byRevisionDataInput.readMap(io.pravega.common.io.serialization.RevisionDataInput.ElementDeserializer<K>, io.pravega.common.io.serialization.RevisionDataInput.ElementDeserializer<V>)).keySerializer- A Function that serializes a single Key of the Map to aRevisionDataOutput.valueSerializer- A Function that serializes a single Value of the Map to aRevisionDataOutput.- Throws:
java.io.IOException- If an IO Exception occurred.
-
-