소스 검색

Write fortunes output with an output stream.

Signed-off-by: Santiago Pericas-Geertsen <[email protected]>
Santiago Pericas-Geertsen 10 달 전
부모
커밋
df21e04a1d
1개의 변경된 파일14개의 추가작업 그리고 3개의 파일을 삭제
  1. 14 3
      frameworks/Java/helidon/nima/src/main/java/io/helidon/benchmark/nima/services/FortuneHandler.java

+ 14 - 3
frameworks/Java/helidon/nima/src/main/java/io/helidon/benchmark/nima/services/FortuneHandler.java

@@ -1,12 +1,14 @@
 
 package io.helidon.benchmark.nima.services;
 
+import java.io.IOException;
 import java.util.Collections;
 import java.util.List;
 
 import com.fizzed.rocker.runtime.ArrayOfByteArraysOutput;
 import io.helidon.benchmark.nima.models.DbRepository;
 import io.helidon.benchmark.nima.models.Fortune;
+import io.helidon.common.buffers.BufferData;
 import io.helidon.webserver.http.Handler;
 import io.helidon.webserver.http.ServerRequest;
 import io.helidon.webserver.http.ServerResponse;
@@ -14,6 +16,7 @@ import views.fortunes;
 
 import static io.helidon.benchmark.nima.Main.CONTENT_TYPE_HTML;
 import static io.helidon.benchmark.nima.Main.SERVER;
+import static io.helidon.http.HeaderNames.CONTENT_LENGTH;
 
 public class FortuneHandler implements Handler {
 
@@ -30,11 +33,19 @@ public class FortuneHandler implements Handler {
     public void handle(ServerRequest req, ServerResponse res) {
         res.header(SERVER);
         res.header(CONTENT_TYPE_HTML);
+
         List<Fortune> fortuneList = repository.getFortunes();
         fortuneList.add(ADDITIONAL_FORTUNE);
         Collections.sort(fortuneList);
-        res.send(fortunes.template(fortuneList)
-                .render(ArrayOfByteArraysOutput.FACTORY)
-                .toByteArray());
+        ArrayOfByteArraysOutput output = fortunes.template(fortuneList).render(ArrayOfByteArraysOutput.FACTORY);
+        List<byte[]> entity = output.getArrays();
+        BufferData bufferData = BufferData.create(entity.stream().map(BufferData::create).toList());
+        int length = bufferData.available();
+        res.header(CONTENT_LENGTH, String.valueOf(length));
+        try (var out = res.outputStream()) {
+            bufferData.writeTo(out);
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
     }
 }