Created
August 22, 2016 17:34
-
-
Save base698/6968e8ae07991fb0c2126d421734a41d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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