Skip to content

Commit f969d2a

Browse files
committed
WIP: MessageEncoder: must returns a ByteBuffer (not a byte[]) #2100
- change method signature of MessageEncoder
1 parent 7a4b811 commit f969d2a

File tree

27 files changed

+130
-73
lines changed

27 files changed

+130
-73
lines changed

jooby/src/main/java/io/jooby/DefaultContext.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ default boolean isSecure() {
463463
try {
464464
Route route = getRoute();
465465
MessageEncoder encoder = route.getEncoder();
466-
byte[] bytes = encoder.encode(this, value);
466+
var bytes = encoder.encode(this, value);
467467
if (bytes == null) {
468468
if (!isResponseStarted()) {
469469
throw new IllegalStateException("The response was not encoded");

jooby/src/main/java/io/jooby/MessageEncoder.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
package io.jooby;
77

8+
import java.nio.ByteBuffer;
89
import java.nio.charset.StandardCharsets;
910

1011
import edu.umd.cs.findbugs.annotations.NonNull;
@@ -23,7 +24,7 @@ public interface MessageEncoder {
2324
MessageEncoder TO_STRING =
2425
(ctx, value) -> {
2526
if (ctx.accept(ctx.getResponseType())) {
26-
return value.toString().getBytes(StandardCharsets.UTF_8);
27+
return ByteBuffer.wrap(value.toString().getBytes(StandardCharsets.UTF_8));
2728
}
2829
throw new NotAcceptableException(ctx.header("Accept").valueOrNull());
2930
};
@@ -37,5 +38,5 @@ public interface MessageEncoder {
3738
* @return Value as byte array or <code>null</code> if given object isn't supported it.
3839
* @throws Exception If something goes wrong.
3940
*/
40-
@Nullable byte[] encode(@NonNull Context ctx, @NonNull Object value) throws Exception;
41+
@Nullable ByteBuffer encode(@NonNull Context ctx, @NonNull Object value) throws Exception;
4142
}

jooby/src/main/java/io/jooby/ServerSentMessage.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ public ServerSentMessage(@NonNull Object data) {
133133
try {
134134
Route route = ctx.getRoute();
135135
MessageEncoder encoder = route.getEncoder();
136-
byte[] bytes = encoder.encode(ctx, data);
136+
// TODO: ByteBuffer fix me this need to be better once we add buffer API
137+
var bytes = encoder.encode(ctx, data).array();
137138

138139
ByteArrayOutputStream buffer = new ByteArrayOutputStream(bytes.length);
139140
if (id != null) {

jooby/src/main/java/io/jooby/TemplateEngine.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
package io.jooby;
77

8+
import java.nio.ByteBuffer;
89
import java.nio.charset.StandardCharsets;
910
import java.util.Collections;
1011
import java.util.List;
@@ -37,14 +38,14 @@ public interface TemplateEngine extends MessageEncoder {
3738
String render(Context ctx, ModelAndView modelAndView) throws Exception;
3839

3940
@Override
40-
default byte[] encode(@NonNull Context ctx, @NonNull Object value) throws Exception {
41+
default ByteBuffer encode(@NonNull Context ctx, @NonNull Object value) throws Exception {
4142
// initialize flash and session attributes (if any)
4243
ctx.flash();
4344
ctx.sessionOrNull();
4445

4546
ctx.setDefaultResponseType(MediaType.html);
4647
String output = render(ctx, (ModelAndView) value);
47-
return output.getBytes(StandardCharsets.UTF_8);
48+
return ByteBuffer.wrap(output.getBytes(StandardCharsets.UTF_8));
4849
}
4950

5051
/**

jooby/src/main/java/io/jooby/WebSocket.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
package io.jooby;
77

8+
import java.nio.ByteBuffer;
89
import java.util.List;
910
import java.util.Map;
1011

@@ -229,7 +230,15 @@ interface WriteCallback {
229230
* @param callback Write callback.
230231
* @return This websocket.
231232
*/
232-
@NonNull WebSocket send(@NonNull byte[] message, @NonNull WriteCallback callback);
233+
default @NonNull WebSocket send(@NonNull byte[] message, @NonNull WriteCallback callback) {
234+
return send(ByteBuffer.wrap(message), callback);
235+
}
236+
237+
default @NonNull WebSocket send(@NonNull ByteBuffer message) {
238+
return send(message, WriteCallback.NOOP);
239+
}
240+
241+
@NonNull WebSocket send(@NonNull ByteBuffer message, @NonNull WriteCallback callback);
233242

234243
/**
235244
* Send a binary message to client.
@@ -267,7 +276,15 @@ interface WriteCallback {
267276
* @param callback Write callback.
268277
* @return This websocket.
269278
*/
270-
@NonNull WebSocket sendBinary(@NonNull byte[] message, @NonNull WriteCallback callback);
279+
default @NonNull WebSocket sendBinary(@NonNull byte[] message, @NonNull WriteCallback callback) {
280+
return sendBinary(ByteBuffer.wrap(message), callback);
281+
}
282+
283+
default @NonNull WebSocket sendBinary(@NonNull ByteBuffer message) {
284+
return sendBinary(message, WriteCallback.NOOP);
285+
}
286+
287+
@NonNull WebSocket sendBinary(@NonNull ByteBuffer message, @NonNull WriteCallback callback);
271288

272289
/**
273290
* Encode a value and send a text message to client.

jooby/src/main/java/io/jooby/internal/HeadContext.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,7 @@
1818
import java.nio.file.Path;
1919

2020
import edu.umd.cs.findbugs.annotations.NonNull;
21-
import io.jooby.Context;
22-
import io.jooby.FileDownload;
23-
import io.jooby.ForwardingContext;
24-
import io.jooby.MediaType;
25-
import io.jooby.MessageEncoder;
26-
import io.jooby.Route;
27-
import io.jooby.Sender;
28-
import io.jooby.SneakyThrows;
29-
import io.jooby.StatusCode;
21+
import io.jooby.*;
3022

3123
public class HeadContext extends ForwardingContext {
3224
/**
@@ -126,7 +118,7 @@ public Context render(@NonNull Object value) {
126118
try {
127119
Route route = getRoute();
128120
MessageEncoder encoder = route.getEncoder();
129-
byte[] bytes = encoder.encode(this, value);
121+
var bytes = encoder.encode(this, value);
130122
if (bytes == null) {
131123
if (!isResponseStarted()) {
132124
throw new IllegalStateException("The response was not encoded");

jooby/src/main/java/io/jooby/internal/HttpMessageEncoder.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public HttpMessageEncoder add(MediaType type, MessageEncoder encoder) {
4545
}
4646

4747
@Override
48-
public byte[] encode(@NonNull Context ctx, @NonNull Object value) throws Exception {
48+
public ByteBuffer encode(@NonNull Context ctx, @NonNull Object value) throws Exception {
4949
if (value instanceof ModelAndView) {
5050
ModelAndView modelAndView = (ModelAndView) value;
5151
for (TemplateEngine engine : templateEngineList) {
@@ -85,14 +85,14 @@ public byte[] encode(@NonNull Context ctx, @NonNull Object value) throws Excepti
8585
}
8686
/** Strings: */
8787
if (value instanceof CharSequence) {
88-
return value.toString().getBytes(StandardCharsets.UTF_8);
88+
return ByteBuffer.wrap(value.toString().getBytes(StandardCharsets.UTF_8));
8989
}
9090
if (value instanceof Number) {
91-
return value.toString().getBytes(StandardCharsets.UTF_8);
91+
return ByteBuffer.wrap(value.toString().getBytes(StandardCharsets.UTF_8));
9292
}
9393
/** RawByte: */
9494
if (value instanceof byte[]) {
95-
return (byte[]) value;
95+
return ByteBuffer.wrap((byte[]) value);
9696
}
9797
if (value instanceof ByteBuffer) {
9898
ctx.send((ByteBuffer) value);

jooby/src/main/java/io/jooby/internal/WebSocketSender.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
package io.jooby.internal;
77

8+
import java.nio.ByteBuffer;
89
import java.nio.charset.Charset;
910
import java.time.Instant;
1011
import java.util.Date;
@@ -56,6 +57,16 @@ public Context send(@NonNull byte[] data) {
5657
return this;
5758
}
5859

60+
@NonNull @Override
61+
public Context send(@NonNull ByteBuffer data) {
62+
if (binary) {
63+
ws.sendBinary(data, callback);
64+
} else {
65+
ws.send(data, callback);
66+
}
67+
return this;
68+
}
69+
5970
@NonNull @Override
6071
public Context render(@NonNull Object value) {
6172
DefaultContext.super.render(value);

jooby/src/main/java/io/jooby/internal/handler/ChunkedSubscriber.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ public void onNext(Object item) {
4444
after.apply(ctx, item, null);
4545
}
4646
MessageEncoder encoder = route.getEncoder();
47-
byte[] data = encoder.encode(ctx, item);
47+
// TODO: ByteBuffer fix me this need to be better once we add buffer API
48+
var data = encoder.encode(ctx, item).array();
4849

4950
if (responseType == null) {
5051
responseType = ctx.getResponseType();

jooby/src/test/java/io/jooby/ServerSentMessageTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import static org.mockito.Mockito.mock;
1010
import static org.mockito.Mockito.when;
1111

12+
import java.nio.ByteBuffer;
1213
import java.nio.charset.StandardCharsets;
1314

1415
import org.junit.jupiter.api.Test;
@@ -21,7 +22,8 @@ public void shouldFormatMessage() throws Exception {
2122
Context ctx = mock(Context.class);
2223

2324
MessageEncoder encoder = mock(MessageEncoder.class);
24-
when(encoder.encode(ctx, data)).thenReturn(data.getBytes(StandardCharsets.UTF_8));
25+
when(encoder.encode(ctx, data))
26+
.thenReturn(ByteBuffer.wrap(data.getBytes(StandardCharsets.UTF_8)));
2527

2628
Route route = mock(Route.class);
2729
when(route.getEncoder()).thenReturn(encoder);

0 commit comments

Comments
 (0)