use of org.apache.flink.runtime.util.DataInputDeserializer in project flink by apache.
the class KvStateRequestSerializer method deserializeKeyAndNamespace.
/**
* Deserializes the key and namespace into a {@link Tuple2}.
*
* @param serializedKeyAndNamespace Serialized key and namespace
* @param keySerializer Serializer for the key
* @param namespaceSerializer Serializer for the namespace
* @param <K> Key type
* @param <N> Namespace
* @return Tuple2 holding deserialized key and namespace
* @throws IOException if the deserialization fails for any reason
*/
public static <K, N> Tuple2<K, N> deserializeKeyAndNamespace(byte[] serializedKeyAndNamespace, TypeSerializer<K> keySerializer, TypeSerializer<N> namespaceSerializer) throws IOException {
DataInputDeserializer dis = new DataInputDeserializer(serializedKeyAndNamespace, 0, serializedKeyAndNamespace.length);
try {
K key = keySerializer.deserialize(dis);
byte magicNumber = dis.readByte();
if (magicNumber != 42) {
throw new IOException("Unexpected magic number " + magicNumber + ".");
}
N namespace = namespaceSerializer.deserialize(dis);
if (dis.available() > 0) {
throw new IOException("Unconsumed bytes in the serialized key and namespace.");
}
return new Tuple2<>(key, namespace);
} catch (IOException e) {
throw new IOException("Unable to deserialize key " + "and namespace. This indicates a mismatch in the key/namespace " + "serializers used by the KvState instance and this access.", e);
}
}
use of org.apache.flink.runtime.util.DataInputDeserializer in project flink by apache.
the class KvStateRequestSerializer method deserializeValue.
/**
* Deserializes the value with the given serializer.
*
* @param serializedValue Serialized value of type T
* @param serializer Serializer for T
* @param <T> Type of the value
* @return Deserialized value or <code>null</code> if the serialized value
* is <code>null</code>
* @throws IOException On failure during deserialization
*/
public static <T> T deserializeValue(byte[] serializedValue, TypeSerializer<T> serializer) throws IOException {
if (serializedValue == null) {
return null;
} else {
final DataInputDeserializer deser = new DataInputDeserializer(serializedValue, 0, serializedValue.length);
final T value = serializer.deserialize(deser);
if (deser.available() > 0) {
throw new IOException("Unconsumed bytes in the deserialized value. " + "This indicates a mismatch in the value serializers " + "used by the KvState instance and this access.");
}
return value;
}
}
use of org.apache.flink.runtime.util.DataInputDeserializer in project flink by apache.
the class CheckpointBarrierTest method testSerialization.
/**
* Test serialization of the checkpoint barrier.
* The checkpoint barrier does not support its own serialization, in order to be immutable.
*/
@Test
public void testSerialization() throws Exception {
long id = Integer.MAX_VALUE + 123123L;
long timestamp = Integer.MAX_VALUE + 1228L;
CheckpointOptions options = CheckpointOptions.forFullCheckpoint();
CheckpointBarrier barrier = new CheckpointBarrier(id, timestamp, options);
try {
barrier.write(new DataOutputSerializer(1024));
fail("should throw an exception");
} catch (UnsupportedOperationException e) {
// expected
}
try {
barrier.read(new DataInputDeserializer(new byte[32]));
fail("should throw an exception");
} catch (UnsupportedOperationException e) {
// expected
}
}
use of org.apache.flink.runtime.util.DataInputDeserializer in project flink by apache.
the class StreamElementSerializerTest method serializeAndDeserialize.
@SuppressWarnings("unchecked")
private static <T, X extends StreamElement> X serializeAndDeserialize(X record, StreamElementSerializer<T> serializer) throws IOException {
DataOutputSerializer output = new DataOutputSerializer(32);
serializer.serialize(record, output);
// additional binary copy step
DataInputDeserializer copyInput = new DataInputDeserializer(output.getByteArray(), 0, output.length());
DataOutputSerializer copyOutput = new DataOutputSerializer(32);
serializer.copy(copyInput, copyOutput);
DataInputDeserializer input = new DataInputDeserializer(copyOutput.getByteArray(), 0, copyOutput.length());
return (X) serializer.deserialize(input);
}
Aggregations