@SuppressWarnings("unchecked") public static <T> T deserializeObject(InputStream in, ClassLoader cl) throws IOException, ClassNotFoundException { return deserializeObject(in, cl, false); }
@SuppressWarnings("unchecked") public static <T> T deserializeObject(byte[] bytes, ClassLoader cl) throws IOException, ClassNotFoundException { return deserializeObject(bytes, cl, false); }
@SuppressWarnings("unchecked") public static <T> T deserializeObject(byte[] bytes, ClassLoader cl, boolean isFailureTolerant) throws IOException, ClassNotFoundException { return deserializeObject(new ByteArrayInputStream(bytes), cl, isFailureTolerant); }
public Throwable deserializeError(ClassLoader classloader) { if (serializedException == null) { // failed to serialize the original exception // return this SerializedThrowable as a stand in return this; } Throwable cached = cachedException == null ? null : cachedException.get(); if (cached == null) { try { cached = InstantiationUtil.deserializeObject(serializedException, classloader); cachedException = new WeakReference<>(cached); } catch (Throwable t) { // something went wrong // return this SerializedThrowable as a stand in return this; } } return cached; }
@SuppressWarnings("unchecked") public T deserializeValue(ClassLoader loader) throws IOException, ClassNotFoundException { Preconditions.checkNotNull(loader, "No classloader has been passed"); return serializedData == null ? null : (T) InstantiationUtil.deserializeObject(serializedData, loader); }
public static <T> T readObjectFromConfig(Configuration config, String key, ClassLoader cl) throws IOException, ClassNotFoundException { byte[] bytes = config.getBytes(key, null); if (bytes == null) { return null; } return deserializeObject(bytes, cl); }
@Override public ComplexPayload copy(ComplexPayload from) { try { Thread currentThread = Thread.currentThread(); if (currentOwnerThread.compareAndSet(null, currentThread)) { return InstantiationUtil.deserializeObject( InstantiationUtil.serializeObject(from), currentThread.getContextClassLoader()); } else { throw new IllegalStateException("Concurrent access to type serializer detected!"); } } catch (Exception e) { throw new RuntimeException(e); } finally { currentOwnerThread.set(null); } }
@Override public ComplexPayload deserialize(DataInputView source) throws IOException { try (final DataInputViewStream inViewWrapper = new DataInputViewStream(source)) { Thread currentThread = Thread.currentThread(); if (currentOwnerThread.compareAndSet(null, currentThread)) { return InstantiationUtil.deserializeObject( inViewWrapper, currentThread.getContextClassLoader()); } else { throw new IllegalStateException("Concurrent access to type serializer detected!"); } } catch (ClassNotFoundException e) { throw new IOException("Could not deserialize object.", e); } finally { currentOwnerThread.set(null); } }
public static <T extends Serializable> T decodeStringToObject(String base64String, Class<T> baseClass, ClassLoader classLoader) { try { final byte[] bytes = BASE64_DECODER.decode(base64String.getBytes(UTF_8)); final T instance = InstantiationUtil.deserializeObject(bytes, classLoader); if (instance != null && !baseClass.isAssignableFrom(instance.getClass())) { throw new ValidationException( "Object '" + instance + "' does not match expected base class '" + baseClass + "' but is of class '" + instance.getClass() + "'."); } return instance; } catch (Exception e) { throw new ValidationException( "Unable to deserialize string '" + base64String + "' of base class '" + baseClass.getName() + "'.", e); } }
@SuppressWarnings("unchecked") @Override protected void restoreKeyAndNamespaceSerializers( InternalTimersSnapshot<K, N> restoredTimersSnapshot, DataInputView in) throws IOException { DataInputViewStream dis = new DataInputViewStream(in); try { restoredTimersSnapshot.setKeySerializer(InstantiationUtil.deserializeObject(dis, userCodeClassLoader, true)); restoredTimersSnapshot.setNamespaceSerializer(InstantiationUtil.deserializeObject(dis, userCodeClassLoader, true)); } catch (ClassNotFoundException exception) { throw new IOException(exception); } } }
private void readV1(DataInputView in, ClassLoader classLoader) throws IOException { nestedSnapshot = NestedSerializersSnapshotDelegate.legacyReadNestedSerializerSnapshots(in, classLoader); try (DataInputViewStream inViewWrapper = new DataInputViewStream(in)) { componentClass = InstantiationUtil.deserializeObject(inViewWrapper, classLoader); } catch (ClassNotFoundException e) { throw new IOException("Could not find requested element class in classpath.", e); } }
@SuppressWarnings("unchecked") private PojoComparator(PojoComparator<T> toClone) { this.keyFields = toClone.keyFields; this.comparators = new TypeComparator[toClone.comparators.length]; for (int i = 0; i < toClone.comparators.length; i++) { this.comparators[i] = toClone.comparators[i].duplicate(); } this.normalizedKeyLengths = toClone.normalizedKeyLengths; this.numLeadingNormalizableKeys = toClone.numLeadingNormalizableKeys; this.normalizableKeyPrefixLen = toClone.normalizableKeyPrefixLen; this.invertNormKey = toClone.invertNormKey; this.type = toClone.type; try { this.serializer = (TypeSerializer<T>) InstantiationUtil.deserializeObject( InstantiationUtil.serializeObject(toClone.serializer), Thread.currentThread().getContextClassLoader()); } catch (IOException | ClassNotFoundException e) { throw new RuntimeException("Cannot copy serializer", e); } }
@Override public void read(DataInputView in) throws IOException { super.read(in); try (final DataInputViewStream inViewWrapper = new DataInputViewStream(in)) { tupleClass = InstantiationUtil.deserializeObject(inViewWrapper, getUserCodeClassLoader(), true); } catch (ClassNotFoundException e) { throw new IOException("Could not find requested tuple class in classpath.", e); } }
@Override public void collect(StreamRecord<T> record) { try { ClassLoader cl = record.getClass().getClassLoader(); T copied = InstantiationUtil.deserializeObject(InstantiationUtil.serializeObject(record.getValue()), cl); outputs.add(copied); } catch (IOException | ClassNotFoundException ex) { throw new RuntimeException("Unable to deserialize record: " + record, ex); } }
@Override public void collect(StreamRecord<T> record) { try { ClassLoader cl = record.getClass().getClassLoader(); T copied = InstantiationUtil.deserializeObject(InstantiationUtil.serializeObject(record.getValue()), cl); list.add(record.copy(copied)); } catch (IOException | ClassNotFoundException ex) { throw new RuntimeException("Unable to deserialize record: " + record, ex); } }
protected void validate(ParameterTool parameter) { ClosureCleaner.ensureSerializable(parameter); internalValidate(parameter); // -------- test behaviour after serialization ------------ try { byte[] b = InstantiationUtil.serializeObject(parameter); final ParameterTool copy = InstantiationUtil.deserializeObject(b, getClass().getClassLoader()); internalValidate(copy); } catch (IOException | ClassNotFoundException e) { throw new RuntimeException(e); } }
@Test public void testResolveProxyClass() throws Exception { final String interfaceName = "UserDefinedInterface"; final String proxyName = "UserProxy"; try (URLClassLoader userClassLoader = createClassLoader(interfaceName, proxyName)) { Class<?> userInterface = Class.forName(interfaceName, false, userClassLoader); InvocationHandler userProxy = (InvocationHandler) Class.forName(proxyName, false, userClassLoader) .newInstance(); Object proxy = Proxy.newProxyInstance(userClassLoader, new Class[]{userInterface}, userProxy); byte[] serializeObject = InstantiationUtil.serializeObject(proxy); Object deserializedProxy = InstantiationUtil.deserializeObject(serializeObject, userClassLoader); assertNotNull(deserializedProxy); } }
private void testSerializability(AvroRowSerializationSchema ser, AvroRowDeserializationSchema deser, Row data) throws Exception { final byte[] serBytes = InstantiationUtil.serializeObject(ser); final byte[] deserBytes = InstantiationUtil.serializeObject(deser); final AvroRowSerializationSchema serCopy = InstantiationUtil.deserializeObject(serBytes, Thread.currentThread().getContextClassLoader()); final AvroRowDeserializationSchema deserCopy = InstantiationUtil.deserializeObject(deserBytes, Thread.currentThread().getContextClassLoader()); final byte[] bytes = serCopy.serialize(data); deserCopy.deserialize(bytes); deserCopy.deserialize(bytes); final Row actual = deserCopy.deserialize(bytes); assertEquals(data, actual); } }
@Test public void testSerializeEnumSerializer() throws Exception { EnumSerializer<PublicEnum> serializer = new EnumSerializer<>(PublicEnum.class); // verify original transient parameters assertEquals(PublicEnum.FOO.ordinal(), serializer.getValueToOrdinal().get(PublicEnum.FOO).intValue()); assertEquals(PublicEnum.BAR.ordinal(), serializer.getValueToOrdinal().get(PublicEnum.BAR).intValue()); assertEquals(PublicEnum.PETER.ordinal(), serializer.getValueToOrdinal().get(PublicEnum.PETER).intValue()); assertEquals(PublicEnum.NATHANIEL.ordinal(), serializer.getValueToOrdinal().get(PublicEnum.NATHANIEL).intValue()); assertEquals(PublicEnum.EMMA.ordinal(), serializer.getValueToOrdinal().get(PublicEnum.EMMA).intValue()); assertEquals(PublicEnum.PAULA.ordinal(), serializer.getValueToOrdinal().get(PublicEnum.PAULA).intValue()); assertTrue(Arrays.equals(PublicEnum.values(), serializer.getValues())); byte[] serializedSerializer = InstantiationUtil.serializeObject(serializer); // deserialize and re-verify transient parameters serializer = InstantiationUtil.deserializeObject(serializedSerializer, Thread.currentThread().getContextClassLoader()); assertEquals(PublicEnum.FOO.ordinal(), serializer.getValueToOrdinal().get(PublicEnum.FOO).intValue()); assertEquals(PublicEnum.BAR.ordinal(), serializer.getValueToOrdinal().get(PublicEnum.BAR).intValue()); assertEquals(PublicEnum.PETER.ordinal(), serializer.getValueToOrdinal().get(PublicEnum.PETER).intValue()); assertEquals(PublicEnum.NATHANIEL.ordinal(), serializer.getValueToOrdinal().get(PublicEnum.NATHANIEL).intValue()); assertEquals(PublicEnum.EMMA.ordinal(), serializer.getValueToOrdinal().get(PublicEnum.EMMA).intValue()); assertEquals(PublicEnum.PAULA.ordinal(), serializer.getValueToOrdinal().get(PublicEnum.PAULA).intValue()); assertTrue(Arrays.equals(PublicEnum.values(), serializer.getValues())); }
@Test public void testSerializable() throws Exception { ResourceSpec rs1 = ResourceSpec.newBuilder(). setCpuCores(1.0). setHeapMemoryInMB(100). setGPUResource(1.1). build(); byte[] buffer = InstantiationUtil.serializeObject(rs1); ResourceSpec rs2 = InstantiationUtil.deserializeObject(buffer, ClassLoader.getSystemClassLoader()); assertEquals(rs1, rs2); } }