Skip to content

Instantly share code, notes, and snippets.

@base698
Created August 22, 2016 17:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save base698/6968e8ae07991fb0c2126d421734a41d to your computer and use it in GitHub Desktop.
Save base698/6968e8ae07991fb0c2126d421734a41d to your computer and use it in GitHub Desktop.
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