public static boolean isSerializable(Object o) { try { serializeObject(o); } catch (IOException e) { return false; } return true; }
public SerializedValue(T value) throws IOException { this.serializedData = value == null ? null : InstantiationUtil.serializeObject(value); }
@Override public void serialize(ComplexPayload record, DataOutputView target) throws IOException { try { if (currentOwnerThread.compareAndSet(null, Thread.currentThread())) { target.write(InstantiationUtil.serializeObject(record)); } else { throw new IllegalStateException("Concurrent access to type serializer detected!"); } } finally { currentOwnerThread.set(null); } }
public static String encodeObjectToString(Serializable obj) { try { final byte[] bytes = InstantiationUtil.serializeObject(obj); return new String(BASE64_ENCODER.encode(bytes), UTF_8); } catch (Exception e) { throw new ValidationException( "Unable to serialize object '" + obj.toString() + "' of class '" + obj.getClass().getName() + "'."); } }
public static void ensureSerializable(Object obj) { try { InstantiationUtil.serializeObject(obj); } catch (Exception e) { throw new InvalidProgramException("Object " + obj + " is not serializable", e); } }
public static void writeObjectToConfig(Object o, Configuration config, String key) throws IOException { byte[] bytes = serializeObject(o); config.setBytes(key, bytes); }
@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); } }
/** * Clones the given serializable object using Java serialization, using the given classloader to * resolve the cloned classes. * * @param obj Object to clone * @param classLoader The classloader to resolve the classes during deserialization. * @param <T> Type of the object to clone * * @return Cloned object * * @throws IOException Thrown if the serialization or deserialization process fails. * @throws ClassNotFoundException Thrown if any of the classes referenced by the object * cannot be resolved during deserialization. */ public static <T extends Serializable> T clone(T obj, ClassLoader classLoader) throws IOException, ClassNotFoundException { if (obj == null) { return null; } else { final byte[] serializedObject = serializeObject(obj); return deserializeObject(serializedObject, classLoader); } }
@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 write(DataOutputView out) throws IOException { super.write(out); try (final DataOutputViewStream outViewWrapper = new DataOutputViewStream(out)) { InstantiationUtil.serializeObject(outViewWrapper, tupleClass); } }
@Override protected void writeKeyAndNamespaceSerializers(DataOutputView out) throws IOException { // the pre-versioned format only serializes the serializers, without their configuration snapshots try (ByteArrayOutputStreamWithPos stream = new ByteArrayOutputStreamWithPos()) { InstantiationUtil.serializeObject(stream, timersSnapshot.getKeySerializer()); InstantiationUtil.serializeObject(stream, timersSnapshot.getNamespaceSerializer()); out.write(stream.getBuf(), 0, stream.getPosition()); } } }
@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 testSerialization() { final T[] testData = getTestData(); for (T typeInfo : testData) { final byte[] serialized; try { serialized = InstantiationUtil.serializeObject(typeInfo); } catch (IOException e) { throw new AssertionError("Could not serialize type information: " + typeInfo, e); } final T deserialized; try { deserialized = InstantiationUtil.deserializeObject(serialized, getClass().getClassLoader()); } catch (IOException | ClassNotFoundException e) { throw new AssertionError("Could not deserialize type information: " + typeInfo, e); } if (typeInfo.hashCode() != deserialized.hashCode() || !typeInfo.equals(deserialized)) { throw new AssertionError("Deserialized type information differs from original one."); } } }
@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())); }
@Override public void write(DataOutputView out) throws IOException { super.write(out); if (typeSerializer instanceof UnloadableDummyTypeSerializer) { UnloadableDummyTypeSerializer<T> dummyTypeSerializer = (UnloadableDummyTypeSerializer<T>) this.typeSerializer; byte[] serializerBytes = dummyTypeSerializer.getActualBytes(); out.write(serializerBytes.length); out.write(serializerBytes); } else { // write in a way that allows the stream to recover from exceptions try (ByteArrayOutputStreamWithPos streamWithPos = new ByteArrayOutputStreamWithPos()) { InstantiationUtil.serializeObject(streamWithPos, typeSerializer); out.writeInt(streamWithPos.getPosition()); out.write(streamWithPos.getBuf(), 0, streamWithPos.getPosition()); } } }
@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); } }