use of io.vertx.core.file.AsyncFile in project vert.x by eclipse.
the class FileSystemExamples method asyncFileWrite.
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!");
} else {
System.err.println("Failed to write: " + ar.cause());
}
});
}
} else {
System.err.println("Cannot open file " + result.cause());
}
});
}
use of io.vertx.core.file.AsyncFile in project vert.x by eclipse.
the class FileSystemTest method testIllegalArguments.
@Test
public void testIllegalArguments() throws Exception {
assertNullPointerException(() -> vertx.fileSystem().copy(null, "ignored", h -> {
}));
assertNullPointerException(() -> vertx.fileSystem().copy("ignored", null, h -> {
}));
assertNullPointerException(() -> vertx.fileSystem().copyBlocking(null, "ignored"));
assertNullPointerException(() -> vertx.fileSystem().copyBlocking("ignored", null));
assertNullPointerException(() -> vertx.fileSystem().copyRecursive(null, "ignored", true, h -> {
}));
assertNullPointerException(() -> vertx.fileSystem().copyRecursive("ignored", null, true, h -> {
}));
assertNullPointerException(() -> vertx.fileSystem().copyRecursiveBlocking(null, "ignored", true));
assertNullPointerException(() -> vertx.fileSystem().copyRecursiveBlocking("ignored", null, true));
assertNullPointerException(() -> vertx.fileSystem().move(null, "ignored", h -> {
}));
assertNullPointerException(() -> vertx.fileSystem().move("ignored", null, h -> {
}));
assertNullPointerException(() -> vertx.fileSystem().moveBlocking(null, "ignored"));
assertNullPointerException(() -> vertx.fileSystem().moveBlocking("ignored", null));
assertNullPointerException(() -> vertx.fileSystem().truncate(null, 0, h -> {
}));
assertNullPointerException(() -> vertx.fileSystem().truncateBlocking(null, 0));
assertNullPointerException(() -> vertx.fileSystem().chmod(null, "ignored", h -> {
}));
assertNullPointerException(() -> vertx.fileSystem().chmod("ignored", null, h -> {
}));
assertNullPointerException(() -> vertx.fileSystem().chmodBlocking(null, "ignored"));
assertNullPointerException(() -> vertx.fileSystem().chmodBlocking("ignored", null));
assertNullPointerException(() -> vertx.fileSystem().chmodRecursive(null, "ignored", "ignored", h -> {
}));
assertNullPointerException(() -> vertx.fileSystem().chmodRecursive("ignored", null, "ignored", h -> {
}));
assertNullPointerException(() -> vertx.fileSystem().chmodRecursiveBlocking(null, "ignored", "ignored"));
assertNullPointerException(() -> vertx.fileSystem().chmodRecursiveBlocking("ignored", null, "ignored"));
assertNullPointerException(() -> vertx.fileSystem().chown(null, "ignored", "ignored", h -> {
}));
assertNullPointerException(() -> vertx.fileSystem().chownBlocking(null, "ignored", "ignored"));
assertNullPointerException(() -> vertx.fileSystem().props(null, h -> {
}));
assertNullPointerException(() -> vertx.fileSystem().propsBlocking(null));
assertNullPointerException(() -> vertx.fileSystem().lprops(null, h -> {
}));
assertNullPointerException(() -> vertx.fileSystem().lpropsBlocking(null));
assertNullPointerException(() -> vertx.fileSystem().link(null, "ignored", h -> {
}));
assertNullPointerException(() -> vertx.fileSystem().link("ignored", null, h -> {
}));
assertNullPointerException(() -> vertx.fileSystem().linkBlocking(null, "ignored"));
assertNullPointerException(() -> vertx.fileSystem().linkBlocking("ignored", null));
assertNullPointerException(() -> vertx.fileSystem().symlink(null, "ignored", h -> {
}));
assertNullPointerException(() -> vertx.fileSystem().symlink("ignored", null, h -> {
}));
assertNullPointerException(() -> vertx.fileSystem().symlinkBlocking(null, "ignored"));
assertNullPointerException(() -> vertx.fileSystem().symlinkBlocking("ignored", null));
assertNullPointerException(() -> vertx.fileSystem().unlink(null, h -> {
}));
assertNullPointerException(() -> vertx.fileSystem().unlinkBlocking(null));
assertNullPointerException(() -> vertx.fileSystem().readSymlink(null, h -> {
}));
assertNullPointerException(() -> vertx.fileSystem().readSymlinkBlocking(null));
assertNullPointerException(() -> vertx.fileSystem().delete(null, h -> {
}));
assertNullPointerException(() -> vertx.fileSystem().deleteBlocking(null));
assertNullPointerException(() -> vertx.fileSystem().deleteRecursive(null, true, h -> {
}));
assertNullPointerException(() -> vertx.fileSystem().deleteRecursiveBlocking(null, true));
assertNullPointerException(() -> vertx.fileSystem().mkdir(null, h -> {
}));
assertNullPointerException(() -> vertx.fileSystem().mkdirBlocking(null));
assertNullPointerException(() -> vertx.fileSystem().mkdir(null, "ignored", h -> {
}));
assertNullPointerException(() -> vertx.fileSystem().mkdirBlocking(null, "ignored"));
assertNullPointerException(() -> vertx.fileSystem().mkdirs(null, h -> {
}));
assertNullPointerException(() -> vertx.fileSystem().mkdirsBlocking(null));
assertNullPointerException(() -> vertx.fileSystem().mkdirs(null, "ignored", h -> {
}));
assertNullPointerException(() -> vertx.fileSystem().mkdirsBlocking(null, "ignored"));
assertNullPointerException(() -> vertx.fileSystem().readDir(null, h -> {
}));
assertNullPointerException(() -> vertx.fileSystem().readDirBlocking(null));
assertNullPointerException(() -> vertx.fileSystem().readDir(null, "ignored", h -> {
}));
assertNullPointerException(() -> vertx.fileSystem().readDirBlocking(null, "ignored"));
assertNullPointerException(() -> vertx.fileSystem().readFile(null, h -> {
}));
assertNullPointerException(() -> vertx.fileSystem().readFileBlocking(null));
assertNullPointerException(() -> vertx.fileSystem().writeFile(null, Buffer.buffer(), h -> {
}));
assertNullPointerException(() -> vertx.fileSystem().writeFile("ignored", null, h -> {
}));
assertNullPointerException(() -> vertx.fileSystem().writeFileBlocking(null, Buffer.buffer()));
assertNullPointerException(() -> vertx.fileSystem().writeFileBlocking("ignored", null));
assertNullPointerException(() -> vertx.fileSystem().open(null, new OpenOptions(), h -> {
}));
assertNullPointerException(() -> vertx.fileSystem().open("ignored", null, h -> {
}));
assertNullPointerException(() -> vertx.fileSystem().openBlocking(null, new OpenOptions()));
assertNullPointerException(() -> vertx.fileSystem().openBlocking("ignored", null));
assertNullPointerException(() -> vertx.fileSystem().createFile(null, h -> {
}));
assertNullPointerException(() -> vertx.fileSystem().createFileBlocking(null));
assertNullPointerException(() -> vertx.fileSystem().createFile(null, "ignored", h -> {
}));
assertNullPointerException(() -> vertx.fileSystem().createFileBlocking(null, "ignored"));
assertNullPointerException(() -> vertx.fileSystem().exists(null, h -> {
}));
assertNullPointerException(() -> vertx.fileSystem().existsBlocking(null));
assertNullPointerException(() -> vertx.fileSystem().fsProps(null, h -> {
}));
assertNullPointerException(() -> vertx.fileSystem().fsPropsBlocking(null));
String fileName = "some-file.dat";
AsyncFile asyncFile = vertx.fileSystem().openBlocking(testDir + pathSep + fileName, new OpenOptions());
assertNullPointerException(() -> asyncFile.write(null));
assertIllegalArgumentException(() -> asyncFile.setWriteQueueMaxSize(1));
assertIllegalArgumentException(() -> asyncFile.setWriteQueueMaxSize(0));
assertIllegalArgumentException(() -> asyncFile.setWriteQueueMaxSize(-1));
assertNullPointerException(() -> asyncFile.write(null, 0, h -> {
}));
assertNullPointerException(() -> asyncFile.write(Buffer.buffer(), 0, null));
assertIllegalArgumentException(() -> asyncFile.write(Buffer.buffer(), -1, h -> {
}));
assertNullPointerException(() -> asyncFile.read(null, 0, 0, 0, h -> {
}));
assertNullPointerException(() -> asyncFile.read(Buffer.buffer(), 0, 0, 0, null));
assertIllegalArgumentException(() -> asyncFile.read(Buffer.buffer(), -1, 0, 0, h -> {
}));
assertIllegalArgumentException(() -> asyncFile.read(Buffer.buffer(), 0, -1, 0, h -> {
}));
assertIllegalArgumentException(() -> asyncFile.read(Buffer.buffer(), 0, 0, -1, h -> {
}));
}
use of io.vertx.core.file.AsyncFile in project vert.x by eclipse.
the class FileSystemTest method testReadStreamWithBufferSize.
@Test
public void testReadStreamWithBufferSize() throws Exception {
String fileName = "some-file.dat";
int chunkSize = 16384;
int chunks = 1;
byte[] content = TestUtils.randomByteArray(chunkSize * chunks);
createFile(fileName, content);
vertx.fileSystem().open(testDir + pathSep + fileName, new OpenOptions(), ar -> {
if (ar.succeeded()) {
AsyncFile rs = ar.result();
rs.setReadBufferSize(chunkSize);
Buffer buff = Buffer.buffer();
int[] callCount = { 0 };
rs.handler((buff1) -> {
buff.appendBuffer(buff1);
callCount[0]++;
});
rs.exceptionHandler(t -> fail(t.getMessage()));
rs.endHandler(v -> {
ar.result().close(ar2 -> {
if (ar2.failed()) {
fail(ar2.cause().getMessage());
} else {
assertEquals(1, callCount[0]);
assertEquals(Buffer.buffer(content), buff);
testComplete();
}
});
});
} else {
fail(ar.cause().getMessage());
}
});
await();
}
use of io.vertx.core.file.AsyncFile in project vertx-examples by vert-x3.
the class JsonStreamingExample method start.
@Override
public void start() throws Exception {
vertx.fileSystem().open("large.json", new OpenOptions(), ar -> {
if (ar.succeeded()) {
AsyncFile asyncFile = ar.result();
AtomicInteger counter = new AtomicInteger();
// Here a Json streaming parser is created wrapping an AsyncFile
// JsonParser is a ReadStream of JsonEvent and can wrap any ReadStream of Buffer
JsonParser jsonParser = JsonParser.newParser(asyncFile);
// We want to parse a giant array of small obects so we switch the parser to object-value mode
// The parser will then emit objects inside the array as a single value event
// This is pretty convenient for the developer
jsonParser.objectValueMode().exceptionHandler(t -> {
t.printStackTrace();
asyncFile.close();
}).endHandler(v -> {
System.out.println("Done!");
asyncFile.close();
}).handler(event -> {
// In object-value mode the event should always be of type "VALUE"
if (event.type() == VALUE) {
// Use mapTo to map the JSON obect to a Java class
DataPoint dataPoint = event.mapTo(DataPoint.class);
// Let's not log all objects from this giant file...
if (counter.incrementAndGet() % 100 == 0) {
System.out.println("DataPoint = " + dataPoint);
}
}
});
} else {
ar.cause().printStackTrace();
}
});
}
use of io.vertx.core.file.AsyncFile in project vertx-examples by vert-x3.
the class Client method start.
@Override
public void start() throws Exception {
HttpClientRequest req = vertx.createHttpClient(new HttpClientOptions()).put(8080, "localhost", "/someurl", resp -> {
System.out.println("Response " + resp.statusCode());
});
String filename = "upload.txt";
FileSystem fs = vertx.fileSystem();
fs.props(filename, ares -> {
FileProps props = ares.result();
System.out.println("props is " + props);
long size = props.size();
req.headers().set("content-length", "" + size);
fs.open(filename, new OpenOptions(), ares2 -> {
AsyncFile file = ares2.result();
Pump pump = Pump.pump(file, req);
file.endHandler(v -> {
req.end();
});
pump.start();
});
});
}
Aggregations