use of io.vertx.core.file.AsyncFile in project vert.x by eclipse.
the class Http2ServerResponse method sendFile.
@Override
public HttpServerResponse sendFile(String filename, long offset, long length, Handler<AsyncResult<Void>> resultHandler) {
synchronized (conn) {
checkValid();
}
Handler<AsyncResult<Void>> h;
if (resultHandler != null) {
Context resultCtx = stream.vertx.getOrCreateContext();
h = ar -> {
resultCtx.runOnContext((v) -> {
resultHandler.handle(ar);
});
};
} else {
h = ar -> {
};
}
HttpUtils.resolveFile(stream.vertx, filename, offset, length, ar -> {
if (ar.succeeded()) {
AsyncFile file = ar.result();
long contentLength = Math.min(length, file.getReadLength());
if (headers.get(HttpHeaderNames.CONTENT_LENGTH) == null) {
putHeader(HttpHeaderNames.CONTENT_LENGTH, String.valueOf(contentLength));
}
if (headers.get(HttpHeaderNames.CONTENT_TYPE) == null) {
String contentType = MimeMapping.getMimeTypeForFilename(filename);
if (contentType != null) {
putHeader(HttpHeaderNames.CONTENT_TYPE, contentType);
}
}
checkSendHeaders(false);
file.pipeTo(this, ar1 -> file.close(ar2 -> {
Throwable failure = ar1.failed() ? ar1.cause() : ar2.failed() ? ar2.cause() : null;
if (failure == null)
h.handle(ar1);
else
h.handle(Future.failedFuture(failure));
}));
} else {
h.handle(ar.mapEmpty());
}
});
return this;
}
use of io.vertx.core.file.AsyncFile in project vertx-web by vert-x3.
the class WebClientTest method testAsyncFileResponseBodyStream.
@Test
public void testAsyncFileResponseBodyStream() throws Exception {
server.requestHandler(req -> {
HttpServerResponse resp = req.response();
resp.end(TestUtils.randomBuffer(1024 * 1024));
});
startServer();
AtomicLong received = new AtomicLong();
AtomicBoolean closed = new AtomicBoolean();
AsyncFile file = new AsyncFile() {
public Future<Void> write(Buffer buffer, long position) {
throw new UnsupportedOperationException();
}
public Future<Buffer> read(Buffer buffer, int offset, long position, int length) {
throw new UnsupportedOperationException();
}
public AsyncFile handler(Handler<Buffer> handler) {
throw new UnsupportedOperationException();
}
public AsyncFile pause() {
throw new UnsupportedOperationException();
}
public AsyncFile resume() {
throw new UnsupportedOperationException();
}
public AsyncFile endHandler(Handler<Void> handler) {
throw new UnsupportedOperationException();
}
public AsyncFile setWriteQueueMaxSize(int i) {
throw new UnsupportedOperationException();
}
public AsyncFile drainHandler(Handler<Void> handler) {
throw new UnsupportedOperationException();
}
public AsyncFile fetch(long l) {
throw new UnsupportedOperationException();
}
public Future<Void> end() {
return close();
}
public void end(Handler<AsyncResult<Void>> handler) {
close(handler);
}
public Future<Void> close() {
throw new UnsupportedOperationException();
}
public void write(Buffer buffer, long l, Handler<AsyncResult<Void>> handler) {
throw new UnsupportedOperationException();
}
public AsyncFile read(Buffer buffer, int i, long l, int i1, Handler<AsyncResult<Buffer>> handler) {
throw new UnsupportedOperationException();
}
public Future<Void> flush() {
throw new UnsupportedOperationException();
}
public AsyncFile flush(Handler<AsyncResult<Void>> handler) {
throw new UnsupportedOperationException();
}
public AsyncFile setReadPos(long l) {
throw new UnsupportedOperationException();
}
public AsyncFile setReadLength(long l) {
throw new UnsupportedOperationException();
}
public long getReadLength() {
throw new UnsupportedOperationException();
}
public AsyncFile setWritePos(long l) {
throw new UnsupportedOperationException();
}
public AsyncFile setReadBufferSize(int i) {
throw new UnsupportedOperationException();
}
public long sizeBlocking() {
return 0;
}
public Future<Long> size() {
return Future.succeededFuture(0L);
}
public AsyncFile exceptionHandler(Handler<Throwable> handler) {
return this;
}
public boolean writeQueueFull() {
return false;
}
public long getWritePos() {
throw new UnsupportedOperationException();
}
public Future<Void> write(Buffer buffer) {
Promise<Void> promise = Promise.promise();
write(buffer, promise);
return promise.future();
}
public void write(Buffer buffer, Handler<AsyncResult<Void>> handler) {
received.addAndGet(buffer.length());
if (handler != null) {
handler.handle(Future.succeededFuture());
}
}
public void close(Handler<AsyncResult<Void>> handler) {
vertx.setTimer(500, id -> {
closed.set(true);
handler.handle(Future.succeededFuture());
});
}
};
HttpRequest<Buffer> get = webClient.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath");
get.as(BodyCodec.pipe(file)).send(onSuccess(v -> {
assertEquals(1024 * 1024, received.get());
assertTrue(closed.get());
testComplete();
}));
await();
}
use of io.vertx.core.file.AsyncFile in project vertx-openshift-it by cescoffier.
the class StreamingResultsTest method handle.
@Override
public void handle(RoutingContext rc) {
jdbcClient.getConnection(ar -> {
if (ar.failed()) {
fail(rc, ar.cause());
return;
}
SQLConnection connection = ar.result();
rc.response().bodyEndHandler(v -> {
connection.close();
});
FileSystem fileSystem = rc.vertx().fileSystem();
fileSystem.open("db/migration/strings", new OpenOptions(), ores -> {
if (ores.failed()) {
fail(rc, ores.cause());
return;
}
AsyncFile asyncFile = ores.result();
List<String> values = new ArrayList<>();
RecordParser parser = RecordParser.newDelimited("\n", out -> values.add(out.toString()));
asyncFile.handler(parser);
asyncFile.endHandler(v -> {
asyncFile.close();
List<String> statements = values.stream().map(value -> "insert into random_string (value) values ('" + value + "')").collect(toList());
connection.batch(statements, ires -> {
if (ires.failed()) {
fail(rc, ires.cause());
return;
}
connection.queryStream("select value from random_string", sres -> {
if (sres.failed()) {
fail(rc, sres.cause());
return;
}
List<String> storedValues = new ArrayList<>(values.size());
SQLRowStream rowStream = sres.result();
rowStream.handler(row -> {
storedValues.add(row.getString(0));
}).endHandler(endRows -> {
if (!storedValues.equals(values)) {
fail(rc, storedValues.toString());
} else {
rc.response().setStatusCode(200).end();
}
});
});
});
});
});
});
}
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