TitleCrawlerHandler : Stream output.
This commit is contained in:
parent
f2d9fdae9b
commit
194c8b2eed
|
@ -27,20 +27,22 @@ class TitleCrawlerHandler extends HttpHandler, Handler {
|
|||
|
||||
// I don't know if this one is ugly, but I wanted to show off a bit.
|
||||
extension (exchange: HttpExchange) private def sendResponse(request: Array[Byte]): Unit = {
|
||||
exchange.sendResponseHeaders(200, 0)
|
||||
val reader = new BufferedReader(new InputStreamReader(ByteArrayInputStream(request)))
|
||||
val response = Iterator
|
||||
val output = exchange.getResponseBody
|
||||
|
||||
Iterator
|
||||
.continually(reader.readLine)
|
||||
.takeWhile(null !=)
|
||||
.filter(HttpUtil.isUrl)
|
||||
.map({ url => s"$url => ${runCrawler(url)}" })
|
||||
.mkString("\n")
|
||||
.getBytes()
|
||||
.map({ url => s"$url => ${runCrawler(url)}\n" })
|
||||
.foreach({ result => {
|
||||
val bytes = result.getBytes
|
||||
StreamUtil.copyStream(new ByteArrayInputStream(bytes), System.out)
|
||||
StreamUtil.copyStream(new ByteArrayInputStream(bytes), output)
|
||||
output.flush()
|
||||
}})
|
||||
|
||||
exchange.sendResponseHeaders(200, response.length)
|
||||
|
||||
val output = exchange.getResponseBody
|
||||
|
||||
StreamUtil.copyStream(new ByteArrayInputStream(response), output)
|
||||
output.close()
|
||||
}
|
||||
|
||||
|
|
Reference in a new issue