use of io.vertx.core.file.OpenOptions in project vert.x by eclipse.
the class FileSystemExamples method example3.
public void example3(FileSystem fileSystem) {
OpenOptions options = new OpenOptions();
fileSystem.open("myfile.txt", options, res -> {
if (res.succeeded()) {
AsyncFile file = res.result();
} else {
}
});
}
use of io.vertx.core.file.OpenOptions in project vert.x by eclipse.
the class FileSystemTest method testWriteAsync.
@Test
public void testWriteAsync() throws Exception {
String fileName = "some-file.dat";
int chunkSize = 1000;
int chunks = 10;
byte[] content = TestUtils.randomByteArray(chunkSize * chunks);
Buffer buff = Buffer.buffer(content);
AtomicInteger count = new AtomicInteger();
vertx.fileSystem().open(testDir + pathSep + fileName, new OpenOptions(), arr -> {
if (arr.succeeded()) {
for (int i = 0; i < chunks; i++) {
Buffer chunk = buff.getBuffer(i * chunkSize, (i + 1) * chunkSize);
assertEquals(chunkSize, chunk.length());
arr.result().write(chunk, i * chunkSize, ar -> {
if (ar.succeeded()) {
if (count.incrementAndGet() == chunks) {
arr.result().close(ar2 -> {
if (ar2.failed()) {
fail(ar2.cause().getMessage());
} else {
assertTrue(fileExists(fileName));
byte[] readBytes;
try {
readBytes = Files.readAllBytes(Paths.get(testDir + pathSep + fileName));
} catch (IOException e) {
fail(e.getMessage());
return;
}
Buffer read = Buffer.buffer(readBytes);
assertEquals(buff, read);
testComplete();
}
});
}
} else {
fail(ar.cause().getMessage());
}
});
}
} else {
fail(arr.cause().getMessage());
}
});
await();
}
use of io.vertx.core.file.OpenOptions in project vert.x by eclipse.
the class FileSystemTest method testWriteStreamAppend.
@Test
public void testWriteStreamAppend() throws Exception {
String fileName = "some-file.dat";
int chunkSize = 1000;
int chunks = 10;
byte[] existing = TestUtils.randomByteArray(1000);
createFile(fileName, existing);
byte[] content = TestUtils.randomByteArray(chunkSize * chunks);
Buffer buff = Buffer.buffer(content);
vertx.fileSystem().open(testDir + pathSep + fileName, new OpenOptions(), ar -> {
if (ar.succeeded()) {
AsyncFile ws = ar.result();
long size = vertx.fileSystem().propsBlocking(testDir + pathSep + fileName).size();
ws.setWritePos(size);
ws.exceptionHandler(t -> fail(t.getMessage()));
for (int i = 0; i < chunks; i++) {
Buffer chunk = buff.getBuffer(i * chunkSize, (i + 1) * chunkSize);
assertEquals(chunkSize, chunk.length());
ws.write(chunk);
}
ar.result().close(ar2 -> {
if (ar2.failed()) {
fail(ar2.cause().getMessage());
} else {
assertTrue(fileExists(fileName));
byte[] readBytes;
try {
readBytes = Files.readAllBytes(Paths.get(testDir + pathSep + fileName));
} catch (IOException e) {
fail(e.getMessage());
return;
}
assertEquals(Buffer.buffer(existing).appendBuffer(buff), Buffer.buffer(readBytes));
testComplete();
}
});
} else {
fail(ar.cause().getMessage());
}
});
await();
}
use of io.vertx.core.file.OpenOptions in project vert.x by eclipse.
the class FileSystemTest method testReadStream.
@Test
public void testReadStream() throws Exception {
String fileName = "some-file.dat";
int chunkSize = 1000;
int chunks = 10;
byte[] content = TestUtils.randomByteArray(chunkSize * chunks);
createFile(fileName, content);
vertx.fileSystem().open(testDir + pathSep + fileName, new OpenOptions(), ar -> {
if (ar.succeeded()) {
ReadStream<Buffer> rs = ar.result();
Buffer buff = Buffer.buffer();
rs.handler(buff::appendBuffer);
rs.exceptionHandler(t -> fail(t.getMessage()));
rs.endHandler(v -> {
ar.result().close(ar2 -> {
if (ar2.failed()) {
fail(ar2.cause().getMessage());
} else {
assertEquals(Buffer.buffer(content), buff);
testComplete();
}
});
});
} else {
fail(ar.cause().getMessage());
}
});
await();
}
use of io.vertx.core.file.OpenOptions in project vert.x by eclipse.
the class FileSystemTest method testOpenOptions.
@Test
public void testOpenOptions() {
OpenOptions opts = new OpenOptions();
assertNull(opts.getPerms());
String perms = "rwxrwxrwx";
assertEquals(opts, opts.setPerms(perms));
assertEquals(perms, opts.getPerms());
assertTrue(opts.isCreate());
assertEquals(opts, opts.setCreate(false));
assertFalse(opts.isCreate());
assertFalse(opts.isCreateNew());
assertEquals(opts, opts.setCreateNew(true));
assertTrue(opts.isCreateNew());
assertTrue(opts.isRead());
assertEquals(opts, opts.setRead(false));
assertFalse(opts.isRead());
assertTrue(opts.isWrite());
assertEquals(opts, opts.setWrite(false));
assertFalse(opts.isWrite());
assertFalse(opts.isDsync());
assertEquals(opts, opts.setDsync(true));
assertTrue(opts.isDsync());
assertFalse(opts.isSync());
assertEquals(opts, opts.setSync(true));
assertTrue(opts.isSync());
assertFalse(opts.isDeleteOnClose());
assertEquals(opts, opts.setDeleteOnClose(true));
assertTrue(opts.isDeleteOnClose());
assertFalse(opts.isTruncateExisting());
assertEquals(opts, opts.setTruncateExisting(true));
assertTrue(opts.isTruncateExisting());
assertFalse(opts.isSparse());
assertEquals(opts, opts.setSparse(true));
assertTrue(opts.isSparse());
}
Aggregations