My server-side Dart web app serves image files for certain requests.
Simplified, here's what it currently does:
HttpServer.bind(InternetAddress.ANY_IP_V4, 80)
.then((HttpServer server) {
server.listen((HttpRequest request) {
request.response.statusCode = HttpStatus.OK;
request.response.headers.contentType = ContentType.parse("image/jpg");
var file = new File("C:\\images\\myImage.jpg");
file.readAsBytes().then((List<int> bytes) {
bytes.forEach((int b) => request.response.writeCharCode(b)); // slow!
request.response.close();
});
}
}
This works, but it's fairly slow and I suspect that writing every byte individually via HttpResponse.writeCharCode
is what's slowing things down here.
Unfortunately, there's no such thing as .writeAllCharCodes
on HttpResponse. There's writeAll, but it calls toString()
on every element of the byte array - we need to write the raw bytes.
Any suggestions?