Code example for ByteBuffer

Methods: get, order

0
   * @param position   byte-offset of the serialized event in the buffer 
   * @return  a writable DbusEvent 
   */ 
  public DbusEventInternalWritable createWritableDbusEventFromBuffer(ByteBuffer buf, int position)
  { 
    if (buf.order() != getByteOrder())
    { 
      throw new DatabusRuntimeException("ByteBuffer byte-order mismatch [DbusEventFactory.createWritableDbusEventFromBuffer(): buf = " + buf.order() + ", this = " + getByteOrder() + "]");
    } 
    return DbusEventFactory.createWritableDbusEventFromBufferUnchecked(buf, position);
  } 
 
  /** 
   * Creates a writable DbusEvent out of an already initialized (serialized) buffer. 
   * 
   * @param buf        buffer containing the serialized event 
   * @param position   byte-offset of the serialized event in the buffer 
   * @return  a writable DbusEvent 
   */ 
  private static DbusEventInternalWritable createWritableDbusEventFromBufferUnchecked(ByteBuffer buf, int position)
  { 
    byte version = buf.get(position);
    if (version == DBUS_EVENT_V1)
    { 
      return new DbusEventV1(buf, position);
    } 
    else if (version == DBUS_EVENT_V2)
    { 
      return new DbusEventV2(buf, position);
    } 
    else if (version == DBUS_EVENT_CURLY_BRACE)
    { 
      // Looks like a JSON-encoding exception (e.g., from the relay if we're the client lib), so try to print it. 
      // TODO:  Longer-term, handle exceptions at the client by checking the response header first (DDSDBUS-xxx).