Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package io.vertx.blog.first;
import java.io.File;
import java.util.Scanner;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
public class MyFirstVerticle extends AbstractVerticle {
static String data;
final static int BLOCK_SIZE = 250000;
@Override
public void start(Future<Void> fut) {
String filename = "./start.json";
System.out.println(filename);
try {
data = new Scanner(new File(filename)).useDelimiter("\\Z").next();
} catch(Exception fnfe) {
System.exit(1);
}
vertx
.createHttpServer()
.requestHandler(r -> {
r.response().setChunked(true).headers().set("content-type", (new Integer(data.length())).toString());
for(int i = 0; i < data.length(); i += BLOCK_SIZE) {
int end = (i + BLOCK_SIZE > data.length()) ? data.length() - 1 : i + BLOCK_SIZE;
r.response().write(data.substring(i, end), "UTF-8");
}
r.response().end();
})
.listen(3000, result -> {
if (result.succeeded()) {
fut.complete();
} else {
fut.fail(result.cause());
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.