Java Code Examples for io.vertx.core.file.FileSystem#open()

The following examples show how to use io.vertx.core.file.FileSystem#open() . 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: WebClientExamples.java    From vertx-web with Apache License 2.0 6 votes vote down vote up
public void sendStream(WebClient client, FileSystem fs) {
  fs.open("content.txt", new OpenOptions(), fileRes -> {
    if (fileRes.succeeded()) {
      ReadStream<Buffer> fileStream = fileRes.result();

      String fileLen = "1024";

      // Send the file to the server using POST
      client
        .post(8080, "myserver.mycompany.com", "/some-uri")
        .putHeader("content-length", fileLen)
        .sendStream(fileStream)
        .onSuccess(res -> {
          // OK
        })
      ;
    }
  });
}
 
Example 2
Source File: ApiClient.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
/**
 * File Download handling.
 *
 * @param response The HTTP response
 * @param handler The response handler
 */
protected <T> void handleFileDownload(HttpResponse<Buffer> response, Handler<AsyncResult<T>> handler) {
    FileSystem fs = getVertx().fileSystem();

    String filename = generateFilename(response.headers());
    Consumer<String> fileHandler = directory -> {
        fs.open(directory + filename, FILE_DOWNLOAD_OPTIONS, asyncFileResult -> {
            if (asyncFileResult.succeeded()) {
                AsyncFile asyncFile = asyncFileResult.result();
                asyncFile.write(response.bodyAsBuffer());
                //noinspection unchecked
                handler.handle(Future.succeededFuture((T) asyncFile));
            } else {
                handler.handle(ApiException.fail(asyncFileResult.cause()));
            }
        });
    };

    String dir = getDownloadsDir();
    if (dir != null && !dir.isEmpty()) {
        fs.mkdirs(dir, mkdirResult -> {
            String sanitizedFolder = dir.endsWith("/") ? dir : dir + "/";
            fileHandler.accept(sanitizedFolder);
        });
    } else {
        fileHandler.accept("");
    }
}
 
Example 3
Source File: NativeExamples.java    From vertx-rx with Apache License 2.0 5 votes vote down vote up
public void toObservable(Vertx vertx) {
  FileSystem fileSystem = vertx.fileSystem();
  fileSystem.open("/data.txt", new OpenOptions(), result -> {
    AsyncFile file = result.result();
    Observable<Buffer> observable = RxHelper.toObservable(file);
    observable.forEach(data -> System.out.println("Read data: " + data.toString("UTF-8")));
  });
}
 
Example 4
Source File: NativeExamples.java    From vertx-rx with Apache License 2.0 5 votes vote down vote up
public void unmarshaller(FileSystem fileSystem) {
  fileSystem.open("/data.txt", new OpenOptions(), result -> {
    AsyncFile file = result.result();
    Observable<Buffer> observable = RxHelper.toObservable(file);
    observable.lift(RxHelper.unmarshaller(MyPojo.class)).subscribe(
        mypojo -> {
          // Process the object
        }
    );
  });
}
 
Example 5
Source File: NativeExamples.java    From vertx-rx with Apache License 2.0 5 votes vote down vote up
public void toFlowable(Vertx vertx) {
  FileSystem fileSystem = vertx.fileSystem();
  fileSystem.open("/data.txt", new OpenOptions(), result -> {
    AsyncFile file = result.result();
    Flowable<Buffer> observable = FlowableHelper.toFlowable(file);
    observable.forEach(data -> System.out.println("Read data: " + data.toString("UTF-8")));
  });
}
 
Example 6
Source File: NativeExamples.java    From vertx-rx with Apache License 2.0 5 votes vote down vote up
public void unmarshaller(FileSystem fileSystem) {
  fileSystem.open("/data.txt", new OpenOptions(), result -> {
    AsyncFile file = result.result();
    Flowable<Buffer> observable = FlowableHelper.toFlowable(file);
    observable.compose(FlowableHelper.unmarshaller(MyPojo.class)).subscribe(
        mypojo -> {
          // Process the object
        }
    );
  });
}