use of io.vertx.core.file.OpenOptions in project vert.x by eclipse.
the class FileSystemTest method testWriteStreamWithCompositeBuffer.
@Test
public void testWriteStreamWithCompositeBuffer() throws Exception {
String fileName = "some-file.dat";
int chunkSize = 1000;
int chunks = 10;
byte[] content1 = TestUtils.randomByteArray(chunkSize * (chunks / 2));
byte[] content2 = TestUtils.randomByteArray(chunkSize * (chunks / 2));
ByteBuf byteBuf = Unpooled.wrappedBuffer(content1, content2);
Buffer buff = Buffer.buffer(byteBuf);
vertx.fileSystem().open(testDir + pathSep + fileName, new OpenOptions(), ar -> {
if (ar.succeeded()) {
WriteStream<Buffer> ws = ar.result();
ws.exceptionHandler(t -> fail(t.getMessage()));
ws.write(buff);
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));
byteBuf.release();
testComplete();
}
});
} else {
fail(ar.cause().getMessage());
}
});
await();
}
use of io.vertx.core.file.OpenOptions in project vert.x by eclipse.
the class FileSystemTest method testAsyncFileCloseHandlerIsAsync.
@Test
public void testAsyncFileCloseHandlerIsAsync() throws Exception {
String fileName = "some-file.dat";
createFileWithJunk(fileName, 100);
AsyncFile file = vertx.fileSystem().openBlocking(testDir + pathSep + fileName, new OpenOptions());
ThreadLocal stack = new ThreadLocal();
stack.set(true);
file.close(ar -> {
assertNull(stack.get());
assertTrue(Vertx.currentContext().isEventLoopContext());
testComplete();
});
await();
}
use of io.vertx.core.file.OpenOptions in project vert.x by eclipse.
the class FileSystemTest method testReadAsync.
@Test
public void testReadAsync() throws Exception {
String fileName = "some-file.dat";
int chunkSize = 1000;
int chunks = 10;
byte[] content = TestUtils.randomByteArray(chunkSize * chunks);
Buffer expected = Buffer.buffer(content);
createFile(fileName, content);
AtomicInteger reads = new AtomicInteger();
vertx.fileSystem().open(testDir + pathSep + fileName, new OpenOptions(), arr -> {
if (arr.succeeded()) {
Buffer buff = Buffer.buffer(chunks * chunkSize);
for (int i = 0; i < chunks; i++) {
arr.result().read(buff, i * chunkSize, i * chunkSize, chunkSize, arb -> {
if (arb.succeeded()) {
if (reads.incrementAndGet() == chunks) {
arr.result().close(ar -> {
if (ar.failed()) {
fail(ar.cause().getMessage());
} else {
assertEquals(expected, buff);
assertEquals(buff, arb.result());
testComplete();
}
});
}
} else {
fail(arb.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 testReadStreamSetReadPos.
@Test
public void testReadStreamSetReadPos() 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()) {
AsyncFile rs = ar.result();
rs.setReadPos(chunkSize * chunks / 2);
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(chunkSize * chunks / 2, buff.length());
byte[] lastHalf = new byte[chunkSize * chunks / 2];
System.arraycopy(content, chunkSize * chunks / 2, lastHalf, 0, chunkSize * chunks / 2);
assertEquals(Buffer.buffer(lastHalf), buff);
testComplete();
}
});
});
} else {
fail(ar.cause().getMessage());
}
});
await();
}
Aggregations