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());
}
use of io.vertx.core.file.OpenOptions in project vert.x by eclipse.
the class FileSystemTest method testDrainNotCalledAfterClose.
@Test
public void testDrainNotCalledAfterClose() throws Exception {
String fileName = "some-file.dat";
vertx.fileSystem().open(testDir + pathSep + fileName, new OpenOptions(), onSuccess(file -> {
Buffer buf = TestUtils.randomBuffer(1024 * 1024);
file.write(buf);
AtomicBoolean drainAfterClose = new AtomicBoolean();
file.drainHandler(v -> {
drainAfterClose.set(true);
});
file.close(ar -> {
assertFalse(drainAfterClose.get());
testComplete();
});
}));
await();
}
use of io.vertx.core.file.OpenOptions in project vert.x by eclipse.
the class FileSystemTest method testWriteStream.
@Test
public void testWriteStream() throws Exception {
String fileName = "some-file.dat";
int chunkSize = 1000;
int chunks = 10;
byte[] content = TestUtils.randomByteArray(chunkSize * chunks);
Buffer buff = Buffer.buffer(content);
vertx.fileSystem().open(testDir + pathSep + fileName, new OpenOptions(), ar -> {
if (ar.succeeded()) {
WriteStream<Buffer> ws = ar.result();
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(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 testPumpFileStreams.
@Test
@SuppressWarnings("unchecked")
public void testPumpFileStreams() throws Exception {
String fileName1 = "some-file.dat";
String fileName2 = "some-other-file.dat";
//Non integer multiple of buffer size
int fileSize = (int) (AsyncFileImpl.DEFAULT_READ_BUFFER_SIZE * 1000.3);
byte[] content = TestUtils.randomByteArray(fileSize);
createFile(fileName1, content);
vertx.fileSystem().open(testDir + pathSep + fileName1, new OpenOptions(), arr -> {
if (arr.succeeded()) {
ReadStream rs = arr.result();
vertx.fileSystem().open(testDir + pathSep + fileName2, new OpenOptions(), ar -> {
if (ar.succeeded()) {
WriteStream ws = ar.result();
Pump p = Pump.pump(rs, ws);
p.start();
rs.endHandler(v -> {
arr.result().close(car -> {
if (car.failed()) {
fail(ar.cause().getMessage());
} else {
ar.result().close(ar2 -> {
if (ar2.failed()) {
fail(ar2.cause().getMessage());
} else {
assertTrue(fileExists(fileName2));
byte[] readBytes;
try {
readBytes = Files.readAllBytes(Paths.get(testDir + pathSep + fileName2));
} catch (IOException e) {
fail(e.getMessage());
return;
}
assertEquals(Buffer.buffer(content), Buffer.buffer(readBytes));
testComplete();
}
});
}
});
});
} else {
fail(ar.cause().getMessage());
}
});
} else {
fail(arr.cause().getMessage());
}
});
await();
}
Aggregations