use of io.vertx.core.file.AsyncFile 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.AsyncFile 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.AsyncFile 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();
}
use of io.vertx.core.file.AsyncFile in project vert.x by eclipse.
the class FileSystemExamples method asyncFilePump.
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) -> {
System.out.println("Copy done");
});
} else {
System.err.println("Cannot open file " + result.cause());
}
});
}
use of io.vertx.core.file.AsyncFile in project vert.x by eclipse.
the class FileSystemExamples method asyncFileRead.
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());
}
});
}
} else {
System.err.println("Cannot open file " + result.cause());
}
});
}
Aggregations