/** * Verifies deserialization failure cases when reading a serializer from bytes, in the * case of a {@link InvalidClassException}. */ @Test public void testSerializerSerializationWithInvalidClass() throws Exception { TypeSerializer<?> serializer = IntSerializer.INSTANCE; byte[] serialized; try (ByteArrayOutputStreamWithPos out = new ByteArrayOutputStreamWithPos()) { TypeSerializerSerializationUtil.writeSerializer(new DataOutputViewStreamWrapper(out), serializer); serialized = out.toByteArray(); } TypeSerializer<?> deserializedSerializer; try (ByteArrayInputStreamWithPos in = new ByteArrayInputStreamWithPos(serialized)) { deserializedSerializer = TypeSerializerSerializationUtil.tryReadSerializer( new DataInputViewStreamWrapper(in), new ArtificialCNFExceptionThrowingClassLoader( Thread.currentThread().getContextClassLoader(), Collections.singleton(IntSerializer.class.getName())), true); } Assert.assertTrue(deserializedSerializer instanceof UnloadableDummyTypeSerializer); }
/** * Verifies that reading and writing serializers work correctly. */ @Test public void testSerializerSerialization() throws Exception { TypeSerializer<?> serializer = IntSerializer.INSTANCE; byte[] serialized; try (ByteArrayOutputStreamWithPos out = new ByteArrayOutputStreamWithPos()) { TypeSerializerSerializationUtil.writeSerializer(new DataOutputViewStreamWrapper(out), serializer); serialized = out.toByteArray(); } TypeSerializer<?> deserializedSerializer; try (ByteArrayInputStreamWithPos in = new ByteArrayInputStreamWithPos(serialized)) { deserializedSerializer = TypeSerializerSerializationUtil.tryReadSerializer( new DataInputViewStreamWrapper(in), Thread.currentThread().getContextClassLoader()); } Assert.assertEquals(serializer, deserializedSerializer); }
/** * Verifies deserialization failure cases when reading a serializer from bytes, in the * case of a {@link ClassNotFoundException}. */ @Test public void testSerializerSerializationWithClassNotFound() throws Exception { TypeSerializer<?> serializer = IntSerializer.INSTANCE; byte[] serialized; try (ByteArrayOutputStreamWithPos out = new ByteArrayOutputStreamWithPos()) { TypeSerializerSerializationUtil.writeSerializer(new DataOutputViewStreamWrapper(out), serializer); serialized = out.toByteArray(); } TypeSerializer<?> deserializedSerializer; try (ByteArrayInputStreamWithPos in = new ByteArrayInputStreamWithPos(serialized)) { deserializedSerializer = TypeSerializerSerializationUtil.tryReadSerializer( new DataInputViewStreamWrapper(in), new ArtificialCNFExceptionThrowingClassLoader( Thread.currentThread().getContextClassLoader(), Collections.singleton(IntSerializer.class.getName())), true); } Assert.assertTrue(deserializedSerializer instanceof UnloadableDummyTypeSerializer); Assert.assertArrayEquals( InstantiationUtil.serializeObject(serializer), ((UnloadableDummyTypeSerializer<?>) deserializedSerializer).getActualBytes()); }
@Test public void testReadNonVersioned() throws IOException { int preVersionedPayload = 563; TestNonVersionedReadableWritable nonVersionedReadableWritable = new TestNonVersionedReadableWritable(preVersionedPayload); byte[] serialized; try (ByteArrayOutputStreamWithPos out = new ByteArrayOutputStreamWithPos()) { nonVersionedReadableWritable.write(new DataOutputViewStreamWrapper(out)); serialized = out.toByteArray(); } TestPostVersionedReadableWritable restoredVersionedReadableWritable = new TestPostVersionedReadableWritable(); try(ByteArrayInputStreamWithPos in = new ByteArrayInputStreamWithPos(serialized)) { restoredVersionedReadableWritable.read(in); } Assert.assertEquals(String.valueOf(preVersionedPayload), restoredVersionedReadableWritable.getData()); }
@Test public void testReadVersioned() throws IOException { String payload = "test-data"; TestPostVersionedReadableWritable versionedReadableWritable = new TestPostVersionedReadableWritable(payload); byte[] serialized; try (ByteArrayOutputStreamWithPos out = new ByteArrayOutputStreamWithPos()) { versionedReadableWritable.write(new DataOutputViewStreamWrapper(out)); serialized = out.toByteArray(); } TestPostVersionedReadableWritable restoredVersionedReadableWritable = new TestPostVersionedReadableWritable(); try(ByteArrayInputStreamWithPos in = new ByteArrayInputStreamWithPos(serialized)) { restoredVersionedReadableWritable.read(in); } Assert.assertEquals(payload, restoredVersionedReadableWritable.getData()); }
@Test public void testReadSameVersion() throws Exception { String payload = "test"; TestWriteable testWriteable = new TestWriteable(1, payload); byte[] serialized; try (ByteArrayOutputStreamWithPos out = new ByteArrayOutputStreamWithPos()) { testWriteable.write(new DataOutputViewStreamWrapper(out)); serialized = out.toByteArray(); } testWriteable = new TestWriteable(1); try (ByteArrayInputStreamWithPos in = new ByteArrayInputStreamWithPos(serialized)) { testWriteable.read(new DataInputViewStreamWrapper(in)); } Assert.assertEquals(payload, testWriteable.getData()); }
@Test public void testReadCompatibleVersion() throws Exception { String payload = "test"; TestWriteable testWriteable = new TestWriteable(1, payload); byte[] serialized; try (ByteArrayOutputStreamWithPos out = new ByteArrayOutputStreamWithPos()) { testWriteable.write(new DataOutputViewStreamWrapper(out)); serialized = out.toByteArray(); } testWriteable = new TestWriteable(2) { @Override public int[] getCompatibleVersions() { return new int[] {1, 2}; } }; try (ByteArrayInputStreamWithPos in = new ByteArrayInputStreamWithPos(serialized)) { testWriteable.read(new DataInputViewStreamWrapper(in)); } Assert.assertEquals(payload, testWriteable.getData()); }
@Test public void testReadMismatchVersion() throws Exception { String payload = "test"; TestWriteable testWriteable = new TestWriteable(1, payload); byte[] serialized; try (ByteArrayOutputStreamWithPos out = new ByteArrayOutputStreamWithPos()) { testWriteable.write(new DataOutputViewStreamWrapper(out)); serialized = out.toByteArray(); } testWriteable = new TestWriteable(2); try (ByteArrayInputStreamWithPos in = new ByteArrayInputStreamWithPos(serialized)) { testWriteable.read(new DataInputViewStreamWrapper(in)); Assert.fail("Version mismatch expected."); } catch (VersionMismatchException ignored) { } Assert.assertEquals(null, testWriteable.getData()); }
new KeyGroupsStateHandle(offsets, new ByteStateHandleCloseChecking("kg-" + i, out.toByteArray())); new OperatorStateHandle.StateMetaInfo(offsets.toArray(), OperatorStateHandle.Mode.SPLIT_DISTRIBUTE)); OperatorStateHandle operatorStateHandle = new OperatorStreamStateHandle(offsetsMap, new ByteStateHandleCloseChecking("os-" + i, out.toByteArray())); operatorStateHandles.add(operatorStateHandle);
public static <K> byte[] getSerializedKeyForKeyedListState( ByteArrayOutputStreamWithPos outputStream, DataOutputView outputView, K key, TypeSerializer<K> keySerializer, int group, byte[] stateNameByte) throws IOException { serializeSingleKey(outputView, key, keySerializer, group, stateNameByte); return outputStream.toByteArray(); }
public static <K, N> byte[] getSerializedKeyForSubKeyedListState( ByteArrayOutputStreamWithPos outputStream, DataOutputView outputView, K key, TypeSerializer<K> keySerializer, N namespace, TypeSerializer<N> namespaceSerializer, int group, byte[] stateNameByte) throws IOException { serializeSingleKeyWithNamespace(outputView, key, keySerializer, namespace, namespaceSerializer, group, stateNameByte); return outputStream.toByteArray(); }
public static <K, MK> byte[] getSerializedKeyForKeyedMapState( ByteArrayOutputStreamWithPos outputStream, DataOutputView outputView, K key, TypeSerializer<K> keySerializer, MK mapKey, TypeSerializer<MK> mapKeySerializer, int group, byte[] stateNameByte) throws IOException { serializeDoubleKey(outputView, key, keySerializer, mapKey, mapKeySerializer, group, stateNameByte); return outputStream.toByteArray(); }
public static <K> byte[] getSerializedPrefixKeyForKeyedMapState( ByteArrayOutputStreamWithPos outputStream, DataOutputView outputView, K key, TypeSerializer<K> keySerializer, int group, byte[] stateNameByte) throws IOException { serializeSingleKey(outputView, key, keySerializer, group, stateNameByte); return outputStream.toByteArray(); }
public static <K, N> byte[] getSerializedKeyForSubKeyedValueState( ByteArrayOutputStreamWithPos outputStream, DataOutputView outputView, K key, TypeSerializer<K> keySerializer, N namespace, TypeSerializer<N> namespaceSerializer, int group, byte[] stateNameByte) throws IOException { serializeSingleKeyWithNamespace(outputView, key, keySerializer, namespace, namespaceSerializer, group, stateNameByte); return outputStream.toByteArray(); }
public static <K> byte[] getSerializedKeyForKeyedValueState( ByteArrayOutputStreamWithPos outputStream, DataOutputView outputView, K key, TypeSerializer<K> keySerializer, int group, byte[] stateNameByte) throws IOException { serializeSingleKey(outputView, key, keySerializer, group, stateNameByte); return outputStream.toByteArray(); }
public static <V> byte[] getSerializeSingleValue( ByteArrayOutputStreamWithPos outputStream, DataOutputView outputView, V value, TypeSerializer<V> valueSerializer) throws IOException { valueSerializer.serialize(value, outputView); return outputStream.toByteArray(); }
@Override public void clear() { try { writeCurrentKeyWithGroupAndNamespace(); byte[] key = keySerializationStream.toByteArray(); backend.db.remove(columnFamily, writeOptions, key); } catch (IOException|RocksDBException e) { throw new RuntimeException("Error while removing entry from RocksDB", e); } }
private byte[] serializeUserKeyWithCurrentKeyAndNamespace(UK userKey) throws IOException { writeCurrentKeyWithGroupAndNamespace(); userKeySerializer.serialize(userKey, keySerializationDataOutputView); return keySerializationStream.toByteArray(); }
@Override public byte[] deserialize(DataInputView source) throws IOException { ByteArrayOutputStreamWithPos output = new ByteArrayOutputStreamWithPos(); byte[] buffer = new byte[1024]; int numBytes; source.skipBytesToRead(1); while ((numBytes = source.read(buffer)) != -1) { output.write(buffer, 0, numBytes); } return output.toByteArray(); }
private byte[] serializeUserValue(UV userValue) throws IOException { keySerializationStream.reset(); if (userValue == null) { keySerializationDataOutputView.writeBoolean(true); } else { keySerializationDataOutputView.writeBoolean(false); userValueSerializer.serialize(userValue, keySerializationDataOutputView); } return keySerializationStream.toByteArray(); }