Package org.vertx.java.core.buffer

Examples of org.vertx.java.core.buffer.Buffer


    setHeaders(resp, "text/plain", helloWorldContentLength);
    resp.end(helloWorldBuffer);
  }

  private void handleJson(HttpServerRequest req) {
    Buffer buff = new Buffer(Json.encode(Collections.singletonMap("message", "Hello, world!")));
    HttpServerResponse resp = req.response();
    setHeaders(resp, "application/json", String.valueOf(buff.length()));
    resp.end(buff);
  }
View Full Code Here


            .putObject("matcher", new JsonObject().putNumber("_id", (random.nextInt(10000) + 1))),
        handler);
  }

  private void sendResponse(HttpServerRequest req, String result) {
    Buffer buff = new Buffer(result);
    HttpServerResponse resp = req.response();
    setHeaders(resp, "application/json", String.valueOf(buff.length()));
    resp.end(buff);
  }
View Full Code Here

     */
    private void doSendFile(final OutputGroup group, final AsyncFile file, final long position, final Handler<AsyncResult<Void>> doneHandler) {
      if (!group.sendQueueFull()) {
        // If the group's queue is not full then read the next portion of the file
        // and send it through the group.
        file.read(new Buffer(4096), 0, position, 4096, new Handler<AsyncResult<Buffer>>() {
          @Override
          public void handle(AsyncResult<Buffer> result) {
            if (result.failed()) {
              new DefaultFutureResult<Void>(result.cause()).setHandler(doneHandler);
            } else {
              Buffer buffer = result.result();
              if (buffer.length() > 0) {
                // Send the buffer and try to read the next portion of the file.
                group.send(buffer);
                doSendFile(group, file, position+buffer.length(), doneHandler);
              } else {
                // If there's no more data to send then end the group. This will
                // indicate to the other side of the connection that all the
                // file contents have been sent.
                group.end();
View Full Code Here

  private void uploadFile(final AsyncFile file, final String address, final Handler<AsyncResult<Void>> doneHandler) {
    uploadFile(file, 0, address, doneHandler);
  }

  private void uploadFile(final AsyncFile file, final long position, final String address, final Handler<AsyncResult<Void>> doneHandler) {
    Buffer buffer = new Buffer(4096);
    file.read(buffer, 0, position, 4096, new Handler<AsyncResult<Buffer>>() {
      @Override
      public void handle(AsyncResult<Buffer> result) {
        if (result.failed()) {
          new DefaultFutureResult<Void>(result.cause()).setHandler(doneHandler);
        } else {
          final Buffer buffer = result.result();
          vertx.eventBus().sendWithTimeout(address, buffer, DEFAULT_REPLY_TIMEOUT, new Handler<AsyncResult<Message<JsonObject>>>() {
            @Override
            public void handle(AsyncResult<Message<JsonObject>> result) {
              if (result.failed()) {
                new DefaultFutureResult<Void>(new ClusterException(result.cause())).setHandler(doneHandler);
              } else if (result.result().body().getString("status").equals("error")) {
                new DefaultFutureResult<Void>(new ClusterException(result.result().body().getString("message"))).setHandler(doneHandler);
              } else if (result.result().body().getString("status").equals("ok")) {
                if (buffer.length() > 0) {
                  uploadFile(file, position+buffer.length(), address, doneHandler);
                } else {
                  new DefaultFutureResult<Void>((Void) null).setHandler(doneHandler);
                }
              }
            }
View Full Code Here

    final AtomicLong position = new AtomicLong();
    return new Handler<Message<Buffer>>() {
      @Override
      public void handle(final Message<Buffer> message) {
        final Handler<Message<Buffer>> handler = this;
        final Buffer buffer = message.body();
        if (buffer.length() > 0) {
          file.write(buffer, position.get(), new Handler<AsyncResult<Void>>() {
            @Override
            public void handle(AsyncResult<Void> result) {
              if (result.failed()) {
                file.close();
                message.reply(new JsonObject().putString("status", "error").putString("message", result.cause().getMessage()));
                vertx.eventBus().unregisterHandler(address, handler);
              } else {
                position.addAndGet(buffer.length());
                message.reply(new JsonObject().putString("status", "ok"));
              }
            }
          });
        } else {
View Full Code Here

    if (type == null) {
      return message.getValue("value");
    } else {
      switch (type) {
        case "buffer":
          return new Buffer(message.getBinary("value"));
        case "bytes":
          return message.getBinary("value");
        case "serialized":
          byte[] bytes = message.getBinary("value");
          ObjectInputStream stream = null;
View Full Code Here

  /**
   * Sends the file to the given output group.
   */
  private void doSendFile(final AsyncFile file, final OutputGroup group, final int position, final Handler<AsyncResult<Void>> doneHandler) {
    if (!group.sendQueueFull()) {
      file.read(new Buffer(BUFFER_SIZE), 0, position, BUFFER_SIZE, new Handler<AsyncResult<Buffer>>() {
        @Override
        public void handle(AsyncResult<Buffer> result) {
          if (result.failed()) {
            new DefaultFutureResult<Void>(result.cause()).setHandler(doneHandler);
          } else {
            Buffer buffer = result.result();
            if (buffer.length() > 0) {
              group.send(buffer);
              doSendFile(file, group, position+buffer.length(), doneHandler);
            } else {
              group.end();
              new DefaultFutureResult<Void>((Void) null).setHandler(doneHandler);
            }
          }
View Full Code Here

  }

  private void writeWebSocket(final WebSocket ws) {
    if (!ws.writeQueueFull()) {
      //ws.writeTextFrame(message);
      ws.writeBinaryFrame(new Buffer(message));
      vertx.runOnContext(new VoidHandler() {
        public void handle() {
          writeWebSocket(ws);
        }
      });
View Full Code Here

            } else if (line.startsWith("publish")) {
              System.out.println("Publish to topic is " + parts[1]);
              Set<String> actorIDs = vertx.sharedData().getSet(parts[1]);
              for (String actorID : actorIDs) {
                System.out.println("Sending to verticle");
                vertx.eventBus().publish(actorID, new Buffer(parts[2]));
              }
            }
          }
        }));
      }
View Full Code Here

          //Now send some data
          for (int i = 0; i < 10; i++) {
            String str = "hello" + i + "\n";
            System.out.print("Net client sending: " + str);
            socket.write(new Buffer(str));
          }
        } else {
          asyncResult.cause().printStackTrace();
        }
      }
View Full Code Here

TOP

Related Classes of org.vertx.java.core.buffer.Buffer

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.