Search in sources :

Example 1 with DbusEventV2

use of com.linkedin.databus.core.DbusEventV2 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);
}
Also used : Field(java.lang.reflect.Field) InvalidEventException(com.linkedin.databus.core.InvalidEventException) ByteBuffer(java.nio.ByteBuffer) DbusEventV2(com.linkedin.databus.core.DbusEventV2) InvalidEventException(com.linkedin.databus.core.InvalidEventException) DbusEventV1(com.linkedin.databus.core.DbusEventV1)

Aggregations

DbusEventV1 (com.linkedin.databus.core.DbusEventV1)1 DbusEventV2 (com.linkedin.databus.core.DbusEventV2)1 InvalidEventException (com.linkedin.databus.core.InvalidEventException)1 Field (java.lang.reflect.Field)1 ByteBuffer (java.nio.ByteBuffer)1