Skip to content

Commit d3f8b08

Browse files
committed
nima stream
1 parent e43a596 commit d3f8b08

File tree

5 files changed

+65
-14
lines changed

5 files changed

+65
-14
lines changed

http-api/pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,15 @@
1414
<developerConnection>scm:git:[email protected]:avaje/avaje-http.git</developerConnection>
1515
<tag>avaje-http-parent-1.19</tag>
1616
</scm>
17+
18+
<dependencies>
19+
<dependency>
20+
<groupId>io.helidon.nima.webserver</groupId>
21+
<artifactId>helidon-nima-webserver</artifactId>
22+
<version>4.0.0-ALPHA5</version>
23+
<optional>true</optional>
24+
<scope>provided</scope>
25+
</dependency>
26+
</dependencies>
1727

1828
</project>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package io.avaje.http.api;
2+
3+
import java.io.IOException;
4+
import java.io.OutputStream;
5+
6+
import io.helidon.nima.webserver.http.ServerResponse;
7+
8+
public class NimaJsonbStream extends OutputStream {
9+
10+
private final ServerResponse response;
11+
private OutputStream outputStream;
12+
private byte[] firstBuffer;
13+
private int firstOff;
14+
private int firstLen;
15+
private boolean chunked;
16+
17+
public NimaJsonbStream(ServerResponse response) {
18+
this.response = response;
19+
}
20+
21+
@Override
22+
public void write(int b) throws IOException {}
23+
24+
@Override
25+
public void write(byte[] b, int off, int len) throws IOException {
26+
if (chunked) {
27+
outputStream.write(b, off, len);
28+
} else if (firstBuffer == null) {
29+
firstBuffer = b;
30+
firstOff = off;
31+
firstLen = len;
32+
} else {
33+
chunked = true;
34+
outputStream = response.outputStream();
35+
outputStream.write(firstBuffer, firstOff, firstLen);
36+
outputStream.write(b, off, len);
37+
firstBuffer = null;
38+
}
39+
}
40+
41+
@Override
42+
public void flush() throws IOException {
43+
if (!chunked) {
44+
response.contentLength(firstLen);
45+
outputStream = response.outputStream();
46+
outputStream.write(firstBuffer, firstOff, firstLen);
47+
}
48+
outputStream.flush();
49+
}
50+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module io.avaje.http.api {
22

33
exports io.avaje.http.api;
4+
requires static io.helidon.nima.webserver;
45

56
}

http-generator-nima/src/main/java/io/avaje/http/generator/helidon/nima/ControllerMethodWriter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ void writeHandler(boolean requestScoped) {
4242
final var bodyType = method.bodyType();
4343
if (bodyType != null) {
4444

45-
if (bodyType.equals("InputStream")) {
45+
if ("InputStream".equals(bodyType)) {
4646
writer.append(" var %s = req.content().inputStream();", method.bodyName()).eol();
4747
} else if (useJsonB) {
4848
final var fieldName =
@@ -118,7 +118,7 @@ void writeHandler(boolean requestScoped) {
118118
writeContextReturn();
119119
if (producesJson()) {
120120
final var uType = UType.parse(method.returnType());
121-
writer.append(" %sJsonType.toJson(result, res.outputStream());", uType.shortName()).eol();
121+
writer.append(" %sJsonType.toJson(result, new NimaJsonbStream(res));", uType.shortName()).eol();
122122
} else {
123123
writer.append(" res.send(result);").eol();
124124
}

tests/test-nima-jsonb/pom.xml

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,29 +36,19 @@
3636
<dependency>
3737
<groupId>io.helidon.nima.webserver</groupId>
3838
<artifactId>helidon-nima-webserver</artifactId>
39-
<version>4.0.0-ALPHA4</version>
39+
<version>4.0.0-ALPHA5</version>
4040
</dependency>
4141
<dependency>
4242
<groupId>io.helidon.nima.http.media</groupId>
4343
<artifactId>helidon-nima-http-media-jsonb</artifactId>
44-
<version>4.0.0-ALPHA4</version>
44+
<version>4.0.0-ALPHA5</version>
4545
</dependency>
4646
<dependency>
4747
<groupId>io.avaje</groupId>
4848
<artifactId>avaje-http-nima-generator</artifactId>
4949
<version>${project.version}</version>
5050
<scope>test</scope>
5151
</dependency>
52-
<!-- <dependency>-->
53-
<!-- <groupId>io.helidon.nima.http2</groupId>-->
54-
<!-- <artifactId>helidon-nima-http2-webserver</artifactId>-->
55-
<!-- <version>4.0.0-SNAPSHOT</version>-->
56-
<!-- </dependency>-->
57-
<!-- <dependency>-->
58-
<!-- <groupId>io.helidon.nima.websocket</groupId>-->
59-
<!-- <artifactId>helidon-nima-websocket-webserver</artifactId>-->
60-
<!-- <version>4.0.0-SNAPSHOT</version>-->
61-
<!-- </dependency>-->
6252
<dependency>
6353
<groupId>io.avaje</groupId>
6454
<artifactId>junit</artifactId>

0 commit comments

Comments
 (0)