Example usage for io.vertx.core Vertx fileSystem

List of usage examples for io.vertx.core Vertx fileSystem

Introduction

In this page you can find the example usage for io.vertx.core Vertx fileSystem.

Prototype

@CacheReturn
FileSystem fileSystem();

Source Link

Document

Get the filesystem object.

Usage

From source file:com.ddp.SimpleREST.java

License:Open Source License

public static void main(String argv[]) {

    VertxOptions options = new VertxOptions().setBlockedThreadCheckInterval(200000000);
    options.setClustered(true);//from  w w w.  jav  a  2 s .c  om

    Vertx.clusteredVertx(options, res -> {
        if (res.succeeded()) {
            Vertx vertx = res.result();
            final JsonObject js = new JsonObject();
            vertx.fileSystem().readFile("app-conf.json", result -> {
                if (result.succeeded()) {
                    Buffer buff = result.result();
                    js.mergeIn(new JsonObject(buff.toString()));
                    initConfig(js);
                    DeploymentOptions deploymentOptions = new DeploymentOptions().setConfig(js)
                            .setMaxWorkerExecuteTime(5000).setWorker(true).setWorkerPoolSize(5);
                    vertx.deployVerticle(SimpleREST.class.getName(), deploymentOptions);
                } else {
                    System.err.println("Oh oh ..." + result.cause());
                }
            });

        }
    });
}

From source file:de.braintags.netrelay.init.Settings.java

License:Open Source License

/**
 * Loads existing settings from the context, when the property {@link #SETTINGS_LOCATION_PROPERTY} is defined;
 * or loads or generates default settings and stores them in the local user directory, subdirectory .netrelay
 * //w  w  w.j  a v  a 2s  .com
 * @param netRelay
 *          the instance of NetRelay, which would create the default settings
 * @param vertx
 *          the instance of {@link Vertx} to be used
 * @param context
 *          the context, which could contain the property {@link #SETTINGS_LOCATION_PROPERTY}, where the location of
 *          the settings file is defined
 * @return
 */
public static Settings loadSettings(NetRelay netRelay, Vertx vertx, Context context) {
    String path = context.config().getString(SETTINGS_LOCATION_PROPERTY);
    if (path != null) {
        return loadSettings(netRelay, vertx, path);
    } else {
        vertx.fileSystem().mkdirsBlocking(LOCAL_USER_DIRECTORY);
        String localSettingsFileName = LOCAL_USER_DIRECTORY + "/" + netRelay.getClass().getName()
                + ".settings.json";
        return loadSettings(netRelay, vertx, localSettingsFileName);
    }
}

From source file:de.braintags.netrelay.init.Settings.java

License:Open Source License

private static Settings loadSettings(NetRelay netRelay, Vertx vertx, String path) {
    FileSystem fs = vertx.fileSystem();
    if (fs.existsBlocking(path)) {
        LOGGER.info("going to load settings from " + path);
        Buffer buffer = fs.readFileBlocking(path);
        Settings settings = Json.decodeValue(buffer.toString(), Settings.class);
        LOGGER.info("settings successfully loaded from " + path);
        return settings;
    } else {/* ww w  .  j  a v  a  2 s  .com*/
        LOGGER.info("creating default settings and store them in " + path);
        Settings settings = netRelay.createDefaultSettings();
        fs.writeFileBlocking(path, Buffer.buffer(Json.encodePrettily(settings)));
        String message = String.format(
                "Settings file did not exist and was created new in path %s. NOTE: edit the file, set edited to true and restart the server",
                path);
        throw new InitException(message);
    }

}

From source file:examples.CoreExamples.java

License:Open Source License

public void exampleFuture6(Vertx vertx) {

    FileSystem fs = vertx.fileSystem();
    Future<Void> startFuture = Future.future();

    Future<Void> fut1 = Future.future();
    fs.createFile("/foo", fut1.completer());

    fut1.compose(v -> {/* ww w  .ja v  a2  s  . com*/
        // When the file is created (fut1), execute this:
        Future<Void> fut2 = Future.future();
        fs.writeFile("/foo", Buffer.buffer(), fut2.completer());
        return fut2;
    }).compose(v -> {
        // When the file is written (fut2), execute this:
        fs.move("/foo", "/bar", startFuture.completer());
    },
            // mark startFuture it as failed if any step fails.
            startFuture);
}

From source file:examples.FileSystemExamples.java

License:Open Source License

public void example1(Vertx vertx) {
    FileSystem fs = vertx.fileSystem();

    // Copy file from foo.txt to bar.txt
    fs.copy("foo.txt", "bar.txt", res -> {
        if (res.succeeded()) {
            // Copied ok!
        } else {//  www. j  a va2s  .c  o m
            // Something went wrong
        }
    });
}

From source file:examples.FileSystemExamples.java

License:Open Source License

public void example2(Vertx vertx) {
    FileSystem fs = vertx.fileSystem();

    // Copy file from foo.txt to bar.txt synchronously
    fs.copyBlocking("foo.txt", "bar.txt");
}

From source file:examples.FileSystemExamples.java

License:Open Source License

public void asyncAPIExamples() {
    Vertx vertx = Vertx.vertx();

    // Read a file
    vertx.fileSystem().readFile("target/classes/readme.txt", result -> {
        if (result.succeeded()) {
            System.out.println(result.result());
        } else {/*from w  w  w .  jav a  2 s . c  o  m*/
            System.err.println("Oh oh ..." + result.cause());
        }
    });

    // Copy a file
    vertx.fileSystem().copy("target/classes/readme.txt", "target/classes/readme2.txt", result -> {
        if (result.succeeded()) {
            System.out.println("File copied");
        } else {
            System.err.println("Oh oh ..." + result.cause());
        }
    });

    // Write a file
    vertx.fileSystem().writeFile("target/classes/hello.txt", Buffer.buffer("Hello"), result -> {
        if (result.succeeded()) {
            System.out.println("File written");
        } else {
            System.err.println("Oh oh ..." + result.cause());
        }
    });

    // Check existence and delete
    vertx.fileSystem().exists("target/classes/junk.txt", result -> {
        if (result.succeeded() && result.result()) {
            vertx.fileSystem().delete("target/classes/junk.txt", r -> {
                System.out.println("File deleted");
            });
        } else {
            System.err.println("Oh oh ... - cannot delete the file: " + result.cause());
        }
    });
}

From source file:examples.FileSystemExamples.java

License:Open Source License

public void asyncFileWrite() {
    Vertx vertx = Vertx.vertx();
    vertx.fileSystem().open("target/classes/hello.txt", new OpenOptions(), result -> {
        if (result.succeeded()) {
            AsyncFile file = result.result();
            Buffer buff = Buffer.buffer("foo");
            for (int i = 0; i < 5; i++) {
                file.write(buff, buff.length() * i, ar -> {
                    if (ar.succeeded()) {
                        System.out.println("Written ok!");
                        // etc
                    } else {
                        System.err.println("Failed to write: " + ar.cause());
                    }//from ww w. j a  v  a 2  s . c  om
                });
            }
        } else {
            System.err.println("Cannot open file " + result.cause());
        }
    });
}

From source file:examples.FileSystemExamples.java

License:Open Source License

public void asyncFileRead() {
    Vertx vertx = Vertx.vertx();
    vertx.fileSystem().open("target/classes/les_miserables.txt", new OpenOptions(), result -> {
        if (result.succeeded()) {
            AsyncFile file = result.result();
            Buffer buff = Buffer.buffer(1000);
            for (int i = 0; i < 10; i++) {
                file.read(buff, i * 100, i * 100, 100, ar -> {
                    if (ar.succeeded()) {
                        System.out.println("Read ok!");
                    } else {
                        System.err.println("Failed to write: " + ar.cause());
                    }//from   ww  w  . j a v a 2 s  .  c  o m
                });
            }
        } else {
            System.err.println("Cannot open file " + result.cause());
        }
    });
}

From source file:examples.FileSystemExamples.java

License:Open Source License

public void asyncFilePump() {
    Vertx vertx = Vertx.vertx();
    final AsyncFile output = vertx.fileSystem().openBlocking("target/classes/plagiary.txt", new OpenOptions());

    vertx.fileSystem().open("target/classes/les_miserables.txt", new OpenOptions(), result -> {
        if (result.succeeded()) {
            AsyncFile file = result.result();
            Pump.pump(file, output).start();
            file.endHandler((r) -> {/*from w w  w.  ja  va 2 s  .  c o m*/
                System.out.println("Copy done");
            });
        } else {
            System.err.println("Cannot open file " + result.cause());
        }
    });
}