Java Code Examples for io.vertx.core.shareddata.impl.ClusterSerializable#writeToBuffer()

The following examples show how to use io.vertx.core.shareddata.impl.ClusterSerializable#writeToBuffer() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UserHolder.java    From vertx-web with Apache License 2.0 6 votes vote down vote up
@Override
public void writeToBuffer(Buffer buffer) {
  // try to get the user from the context otherwise fall back to any cached version
  final User user;

  synchronized (this) {
    user = context != null ? context.user() : this.user;
    // clear the context as this holder is not in a request anymore
    context = null;
  }

  if (user instanceof ClusterSerializable) {
    buffer.appendByte((byte)1);
    String className = user.getClass().getName();
    if (className == null) {
      throw new IllegalStateException("Cannot serialize " + user.getClass().getName());
    }
    byte[] bytes = className.getBytes(StandardCharsets.UTF_8);
    buffer.appendInt(bytes.length);
    buffer.appendBytes(bytes);
    ClusterSerializable cs = (ClusterSerializable)user;
    cs.writeToBuffer(buffer);
  } else {
    buffer.appendByte((byte)0);
  }
}
 
Example 2
Source File: DataConverter.java    From vertx-infinispan with Apache License 2.0 5 votes vote down vote up
private static void writeClusterSerializable(ByteArrayOutputStream baos, ClusterSerializable o) {
  Buffer buffer = Buffer.buffer();
  byte[] classNameBytes = o.getClass().getName().getBytes(StandardCharsets.UTF_8);
  buffer.appendInt(classNameBytes.length).appendBytes(classNameBytes);
  o.writeToBuffer(buffer);
  baos.write(buffer.getBytes(), 0, buffer.length());
}
 
Example 3
Source File: BasicAuthHandlerTest.java    From vertx-web with Apache License 2.0 5 votes vote down vote up
@Override
public void put(Session session, Handler<AsyncResult<Void>> resultHandler) {
  ClusterSerializable cs = (ClusterSerializable)session;
  Buffer buff = Buffer.buffer();
  cs.writeToBuffer(buff);
  sessions.put(session.id(), buff);
  vertx.runOnContext(v -> resultHandler.handle(Future.succeededFuture()));
}
 
Example 4
Source File: ClusterSerializationUtils.java    From vertx-ignite with Apache License 2.0 4 votes vote down vote up
private static ClusterSerializableValue marshal0(ClusterSerializable obj) {
  Buffer buffer = Buffer.buffer();
  obj.writeToBuffer(buffer);
  return new ClusterSerializableValue(obj.getClass().getName(), buffer.getBytes());
}