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 vertx-web by vert-x3.
the class WebClientTest method testRequestWithBody.
private void testRequestWithBody(HttpMethod method, boolean chunked) throws Exception {
String expected = TestUtils.randomAlphaString(1024 * 1024);
File f = File.createTempFile("vertx", ".data");
f.deleteOnExit();
Files.write(f.toPath(), expected.getBytes());
waitFor(2);
server.requestHandler(req -> req.bodyHandler(buff -> {
assertEquals(method, req.method());
assertEquals(Buffer.buffer(expected), buff);
complete();
req.response().end();
}));
startServer();
vertx.runOnContext(v -> {
AsyncFile asyncFile = vertx.fileSystem().openBlocking(f.getAbsolutePath(), new OpenOptions());
HttpRequest<Buffer> builder = null;
switch(method) {
case POST:
builder = client.post(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath");
break;
case PUT:
builder = client.put(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath");
break;
case PATCH:
builder = client.patch(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath");
break;
default:
fail("Invalid HTTP method");
}
if (!chunked) {
builder = builder.putHeader("Content-Length", "" + expected.length());
}
builder.sendStream(asyncFile, onSuccess(resp -> {
assertEquals(200, resp.statusCode());
complete();
}));
});
await();
}
use of io.vertx.core.file.AsyncFile in project georocket by georocket.
the class FileStore method getOne.
@Override
public void getOne(String path, Handler<AsyncResult<ChunkReadStream>> handler) {
String absolutePath = Paths.get(root, path).toString();
// check if chunk exists
FileSystem fs = vertx.fileSystem();
ObservableFuture<Boolean> observable = RxHelper.observableFuture();
fs.exists(absolutePath, observable.toHandler());
observable.flatMap(exists -> {
if (!exists) {
return Observable.error(new FileNotFoundException("Could not find chunk: " + path));
}
return Observable.just(exists);
}).flatMap(exists -> {
// get chunk's size
ObservableFuture<FileProps> propsObservable = RxHelper.observableFuture();
fs.props(absolutePath, propsObservable.toHandler());
return propsObservable;
}).map(props -> props.size()).flatMap(size -> {
// open chunk
ObservableFuture<AsyncFile> openObservable = RxHelper.observableFuture();
OpenOptions openOptions = new OpenOptions().setCreate(false).setWrite(false);
fs.open(absolutePath, openOptions, openObservable.toHandler());
return openObservable.map(f -> new FileChunkReadStream(size, f));
}).subscribe(readStream -> {
// send chunk to peer
handler.handle(Future.succeededFuture(readStream));
}, err -> {
handler.handle(Future.failedFuture(err));
});
}
use of io.vertx.core.file.AsyncFile in project georocket by georocket.
the class ImportCommand method importFile.
/**
* Upload a file to GeoRocket
* @param path path to file to import
* @param client the GeoRocket client
* @param vertx the Vert.x instance
* @return an observable that will emit when the file has been uploaded
*/
protected Observable<Void> importFile(String path, GeoRocketClient client, Vertx vertx) {
// open file
FileSystem fs = vertx.fileSystem();
OpenOptions openOptions = new OpenOptions().setCreate(false).setWrite(false);
return fs.rxOpen(path, openOptions).flatMap(f -> fs.rxProps(path).map(props -> Pair.of(f, props.size()))).flatMapObservable(f -> {
ObservableFuture<Void> o = RxHelper.observableFuture();
Handler<AsyncResult<Void>> handler = o.toHandler();
AsyncFile file = f.getLeft().getDelegate();
WriteStream<Buffer> out = client.getStore().startImport(layer, tags, properties, Optional.of(f.getRight()), fallbackCRS, handler);
AtomicBoolean fileClosed = new AtomicBoolean();
Pump pump = Pump.pump(file, out);
file.endHandler(v -> {
file.close();
out.end();
fileClosed.set(true);
});
Handler<Throwable> exceptionHandler = t -> {
if (!fileClosed.get()) {
file.endHandler(null);
file.close();
}
handler.handle(Future.failedFuture(t));
};
file.exceptionHandler(exceptionHandler);
out.exceptionHandler(exceptionHandler);
pump.start();
return o;
});
}
Aggregations