Java Code Examples for io.vertx.core.buffer.Buffer#appendInt()

The following examples show how to use io.vertx.core.buffer.Buffer#appendInt() . 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: PgSocketConnection.java    From vertx-sql-client with Apache License 2.0 6 votes vote down vote up
void sendCancelRequestMessage(int processId, int secretKey, Handler<AsyncResult<Void>> handler) {
  Buffer buffer = Buffer.buffer(16);
  buffer.appendInt(16);
  // cancel request code
  buffer.appendInt(80877102);
  buffer.appendInt(processId);
  buffer.appendInt(secretKey);

  socket.write(buffer, ar -> {
    if (ar.succeeded()) {
      // directly close this connection
      if (status == Status.CONNECTED) {
        status = Status.CLOSING;
        socket.close();
      }
      handler.handle(Future.succeededFuture());
    } else {
      handler.handle(Future.failedFuture(ar.cause()));
    }
  });
}
 
Example 2
Source File: PojoEventBusCodec.java    From raml-module-builder with Apache License 2.0 6 votes vote down vote up
@Override
public void encodeToWire(Buffer buffer, Object pojo) {

  try {
    String value = MAPPER.writeValueAsString(pojo);
    String clazz = pojo.getClass().getName();
    int clazzLength = clazz.getBytes().length;
    buffer.appendInt(clazzLength);
    buffer.appendString(clazz);
    if(value != null){
      int dataLength = value.getBytes().length;
      // Write data into given buffer
      buffer.appendInt(dataLength);
      buffer.appendString(value);
    }
  } catch (JsonProcessingException e) {
    log.error(e.getMessage(), e);
  }
}
 
Example 3
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 4
Source File: SampleDatabaseWriter.java    From vertx-in-action with MIT License 5 votes vote down vote up
public static void main(String[] args) {
  Vertx vertx = Vertx.vertx();
  AsyncFile file = vertx.fileSystem().openBlocking("sample.db",
    new OpenOptions().setWrite(true).setCreate(true));

  Buffer buffer = Buffer.buffer();

  // Magic number
  buffer.appendBytes(new byte[] { 1, 2, 3, 4});

  // Version
  buffer.appendInt(2);

  // DB name
  buffer.appendString("Sample database\n");

  // Entry 1
  String key = "abc";
  String value = "123456-abcdef";
  buffer
    .appendInt(key.length())
    .appendString(key)
    .appendInt(value.length())
    .appendString(value);

  // Entry 2
  key = "foo@bar";
  value = "Foo Bar Baz";
  buffer
    .appendInt(key.length())
    .appendString(key)
    .appendInt(value.length())
    .appendString(value);

  file.end(buffer, ar -> vertx.close());
}
 
Example 5
Source File: SerializationSupport.java    From vertx-vaadin with MIT License 5 votes vote down vote up
public static void writeToBuffer(Buffer buffer, Object object) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try (ObjectOutputStream objectStream = new ObjectOutputStream(baos)) {
        objectStream.writeObject(object);
        objectStream.flush();
    } catch (Exception ex) {
        logger.error("Error serializing object of type {}", object.getClass(), ex);
    }
    buffer.appendInt(baos.size());
    buffer.appendBytes(baos.toByteArray());
}
 
Example 6
Source File: SerializationSupport.java    From vertx-vaadin with MIT License 5 votes vote down vote up
public static void writeToBuffer(Buffer buffer, Object object) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try (ObjectOutputStream objectStream = new ObjectOutputStream(baos)) {
        objectStream.writeObject(object);
        objectStream.flush();
    } catch (Exception ex) {
        logger.error("Error serializing object of type {}", object.getClass(), ex);
    }
    buffer.appendInt(baos.size());
    buffer.appendBytes(baos.toByteArray());
}
 
Example 7
Source File: JacksonCodec.java    From kyoko with MIT License 5 votes vote down vote up
@Override
public void encodeToWire(final Buffer buffer, final T object) {
    try {
        final byte[] data = JsonUtil.toJSON(object).getBytes(StandardCharsets.UTF_8);
        buffer.appendInt(data.length);
        buffer.appendBytes(data);
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }
}
 
Example 8
Source File: CustomMessageCodec.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@Override
public void encodeToWire(Buffer buffer, CustomMessage customMessage) {
    String jsonStr = Json.encode(customMessage);
    int length = jsonStr.getBytes().length;
    buffer.appendInt(length);
    buffer.appendString(jsonStr);
}
 
Example 9
Source File: SharedDataSessionImpl.java    From vertx-web with Apache License 2.0 5 votes vote down vote up
@Override
public void writeToBuffer(Buffer buff) {
  byte[] bytes = id().getBytes(UTF8);
  buff.appendInt(bytes.length).appendBytes(bytes);
  buff.appendLong(timeout());
  buff.appendLong(lastAccessed());
  buff.appendInt(version());
  // use cache
  Buffer dataBuf = writeDataToBuffer();
  buff.appendBuffer(dataBuf);
}
 
Example 10
Source File: ServiceExceptionMessageCodec.java    From vertx-service-proxy with Apache License 2.0 5 votes vote down vote up
@Override
public void encodeToWire(Buffer buffer, ServiceException body) {
  buffer.appendInt(body.failureCode());
  if (body.getMessage() == null) {
    buffer.appendByte((byte)0);
  } else {
    buffer.appendByte((byte)1);
    byte[] encoded = body.getMessage().getBytes(CharsetUtil.UTF_8);
    buffer.appendInt(encoded.length);
    buffer.appendBytes(encoded);
  }
  body.getDebugInfo().writeToBuffer(buffer);
}
 
Example 11
Source File: AbstractUser.java    From vertx-auth with Apache License 2.0 5 votes vote down vote up
private void writeStringSet(Buffer buff, Set<String> set) {
  buff.appendInt(set == null ? 0 : set.size());
  if (set != null) {
    for (String entry : set) {
      byte[] bytes = entry.getBytes(StandardCharsets.UTF_8);
      buff.appendInt(bytes.length).appendBytes(bytes);
    }
  }
}
 
Example 12
Source File: VertxBufferImpl.java    From quarkus-http with Apache License 2.0 4 votes vote down vote up
@Override
public void writeToBuffer(Buffer buff) {
  buff.appendInt(this.length());
  buff.appendBuffer(this);
}
 
Example 13
Source File: VertxBufferImpl.java    From quarkus with Apache License 2.0 4 votes vote down vote up
@Override
public void writeToBuffer(Buffer buff) {
    buff.appendInt(this.length());
    buff.appendBuffer(this);
}
 
Example 14
Source File: PersonCodec.java    From vertx-camel-bridge with Apache License 2.0 4 votes vote down vote up
@Override
public void encodeToWire(Buffer buffer, Person person) {
  String encoded = Json.encode(person);
  buffer.appendInt(encoded.length());
  buffer.appendString(encoded);
}
 
Example 15
Source File: VertxBufferTest.java    From Lealone-Plugins with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) {
    AsyncConnection c = new TcpServerConnection(null, null, false);
    Buffer b = Buffer.buffer();
    b.appendInt(8); // packetLength
    b.appendInt(1);
    b.appendInt(2);
    c.handle(newBuffer(b));

    b = Buffer.buffer();
    b.appendInt(8); // packetLength
    b.appendInt(1);
    c.handle(newBuffer(b));

    b = Buffer.buffer();
    b.appendInt(2);
    c.handle(newBuffer(b));

    // c.tc.handle(newBuffer(b));ull;
    b = Buffer.buffer();
    b.appendShort((short) 0);
    c.handle(newBuffer(b));
    b = Buffer.buffer();
    b.appendShort((short) 8);
    c.handle(newBuffer(b));

    // c.tmpBuffer = null;
    b = Buffer.buffer();
    // packet 1
    b.appendInt(8); // packetLength
    b.appendInt(1);
    b.appendInt(2);
    // packet 2
    b.appendInt(12); // packetLength
    b.appendInt(1);
    b.appendInt(2);
    b.appendInt(3);

    // packet 3 half
    b.appendInt(12); // packetLength
    b.appendInt(1);
    b.appendInt(2);
    c.handle(newBuffer(b));
    // packet 3
    b = Buffer.buffer();
    b.appendInt(3);
    c.handle(newBuffer(b));
}