public void setOutputDataDistribution(DataDistribution distribution, int outputNum) { this.config.setString(OUTPUT_DATA_DISTRIBUTION_CLASS, distribution.getClass().getName()); final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final DataOutputStream dos = new DataOutputStream(baos); try { distribution.write(dos); } catch (IOException e) { throw new RuntimeException("Error serializing the DataDistribution: " + e.getMessage(), e); } this.config.setBytes(OUTPUT_DATA_DISTRIBUTION_PREFIX + outputNum, baos.toByteArray()); }
public DataDistribution getOutputDataDistribution(int outputNum, final ClassLoader cl) throws ClassNotFoundException { final String className = this.config.getString(OUTPUT_DATA_DISTRIBUTION_CLASS, null); if (className == null) { return null; } final Class<? extends DataDistribution> clazz; try { clazz = (Class<? extends DataDistribution>) Class.forName(className, true, cl).asSubclass(DataDistribution.class); } catch (ClassCastException ccex) { throw new CorruptConfigurationException( "The class noted in the configuration as the data distribution " + "is no subclass of DataDistribution."); } final DataDistribution distribution = InstantiationUtil.instantiate(clazz, DataDistribution.class); final byte[] stateEncoded = this.config.getBytes(OUTPUT_DATA_DISTRIBUTION_PREFIX + outputNum, null); if (stateEncoded == null) { throw new CorruptConfigurationException( "The configuration contained the data distribution type, but no serialized state."); } final ByteArrayInputStream bais = new ByteArrayInputStream(stateEncoded); final DataInputStream in = new DataInputStream(bais); try { distribution.read(in); return distribution; } catch (Exception ex) { throw new RuntimeException( "The deserialization of the encoded data distribution state caused an error" + ex.getMessage() == null ? "." : ": " + ex.getMessage(), ex); } }