use of com.linkedin.databus.core.DbusEventV1 in project databus by linkedin.
the class DbusEventCorrupter method makeWritable.
/**
* Converts an event (readable or otherwise) to writable for testing. EEEEEEEVIL!
*
* Note that all DbusEvents are fundamentally just little windows into some
* DbusEventBuffer; the various event classes differ only in the methods they
* expose. So pulling the buffer and offset variables out of a readable and
* using them to construct a writable is effectively the same as typecasting
* the readable to writable (i.e., "const_cast<DbusEventInternalWritable>()",
* so to speak).
*
* @param event DbusEvent to be abused
*/
public static DbusEventInternalWritable makeWritable(DbusEvent event) throws InvalidEventException {
// version is always in the first byte // TODO: add DbusEvent version() accessor? seems like good idea...
byte version = event.getRawBytes().get(0);
ByteBuffer buf;
int pos;
try {
Field reflectedBuf = DbusEventSerializable.class.getDeclaredField("_buf");
Field reflectedPosition = DbusEventSerializable.class.getDeclaredField("_position");
reflectedBuf.setAccessible(true);
reflectedPosition.setAccessible(true);
buf = (ByteBuffer) reflectedBuf.get(event);
pos = reflectedPosition.getInt(event);
} catch (Exception e) {
throw new InvalidEventException(e);
}
return (version == 0) ? new DbusEventV1(buf, pos) : new DbusEventV2(buf, pos);
}
Aggregations