Skip to content

Commit df21e04

Browse files
committed
Write fortunes output with an output stream.
Signed-off-by: Santiago Pericas-Geertsen <[email protected]>
1 parent a7deef0 commit df21e04

File tree

1 file changed

+14
-3
lines changed
  • frameworks/Java/helidon/nima/src/main/java/io/helidon/benchmark/nima/services

1 file changed

+14
-3
lines changed

frameworks/Java/helidon/nima/src/main/java/io/helidon/benchmark/nima/services/FortuneHandler.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11

22
package io.helidon.benchmark.nima.services;
33

4+
import java.io.IOException;
45
import java.util.Collections;
56
import java.util.List;
67

78
import com.fizzed.rocker.runtime.ArrayOfByteArraysOutput;
89
import io.helidon.benchmark.nima.models.DbRepository;
910
import io.helidon.benchmark.nima.models.Fortune;
11+
import io.helidon.common.buffers.BufferData;
1012
import io.helidon.webserver.http.Handler;
1113
import io.helidon.webserver.http.ServerRequest;
1214
import io.helidon.webserver.http.ServerResponse;
1315
import views.fortunes;
1416

1517
import static io.helidon.benchmark.nima.Main.CONTENT_TYPE_HTML;
1618
import static io.helidon.benchmark.nima.Main.SERVER;
19+
import static io.helidon.http.HeaderNames.CONTENT_LENGTH;
1720

1821
public class FortuneHandler implements Handler {
1922

@@ -30,11 +33,19 @@ public FortuneHandler(DbRepository repository) {
3033
public void handle(ServerRequest req, ServerResponse res) {
3134
res.header(SERVER);
3235
res.header(CONTENT_TYPE_HTML);
36+
3337
List<Fortune> fortuneList = repository.getFortunes();
3438
fortuneList.add(ADDITIONAL_FORTUNE);
3539
Collections.sort(fortuneList);
36-
res.send(fortunes.template(fortuneList)
37-
.render(ArrayOfByteArraysOutput.FACTORY)
38-
.toByteArray());
40+
ArrayOfByteArraysOutput output = fortunes.template(fortuneList).render(ArrayOfByteArraysOutput.FACTORY);
41+
List<byte[]> entity = output.getArrays();
42+
BufferData bufferData = BufferData.create(entity.stream().map(BufferData::create).toList());
43+
int length = bufferData.available();
44+
res.header(CONTENT_LENGTH, String.valueOf(length));
45+
try (var out = res.outputStream()) {
46+
bufferData.writeTo(out);
47+
} catch (IOException e) {
48+
throw new RuntimeException(e);
49+
}
3950
}
4051
}

0 commit comments

Comments
 (0)