use of org.apache.crunch.impl.mr.run.CrunchRuntimeException in project crunch by cloudera.
the class Writables method deepCopy.
/**
* Perform a deep copy of a writable value.
*
* @param value
* The value to be copied
* @param writableClass
* The Writable class of the value to be copied
* @return A fully detached deep copy of the input value
*/
public static <T extends Writable> T deepCopy(T value, Class<T> writableClass) {
ByteArrayOutputStream byteOutStream = new ByteArrayOutputStream();
DataOutputStream dataOut = new DataOutputStream(byteOutStream);
T copiedValue = null;
try {
value.write(dataOut);
dataOut.flush();
ByteArrayInputStream byteInStream = new ByteArrayInputStream(byteOutStream.toByteArray());
DataInput dataInput = new DataInputStream(byteInStream);
copiedValue = writableClass.newInstance();
copiedValue.readFields(dataInput);
} catch (Exception e) {
throw new CrunchRuntimeException("Error while deep copying " + value, e);
}
return copiedValue;
}
Aggregations