Use decode from a DataBuffer where feasible

See gh-22782
This commit is contained in:
Rossen Stoyanchev
2019-04-10 22:08:00 -04:00
parent a912d8de1e
commit f89d2ac148
3 changed files with 21 additions and 22 deletions

View File

@@ -106,10 +106,11 @@ public class ServerSentEventHttpMessageReader implements HttpMessageReader<Objec
return stringDecoder.decode(message.getBody(), STRING_TYPE, null, hints)
.bufferUntil(line -> line.equals(""))
.concatMap(lines -> buildEvent(lines, valueType, shouldWrap, hints));
.concatMap(lines -> Mono.justOrEmpty(buildEvent(lines, valueType, shouldWrap, hints)));
}
private Mono<?> buildEvent(List<String> lines, ResolvableType valueType, boolean shouldWrap,
@Nullable
private Object buildEvent(List<String> lines, ResolvableType valueType, boolean shouldWrap,
Map<String, Object> hints) {
ServerSentEvent.Builder<Object> sseBuilder = shouldWrap ? ServerSentEvent.builder() : null;
@@ -138,34 +139,32 @@ public class ServerSentEventHttpMessageReader implements HttpMessageReader<Objec
}
}
Mono<?> decodedData = (data != null ? decodeData(data.toString(), valueType, hints) : Mono.empty());
Object decodedData = data != null ? decodeData(data.toString(), valueType, hints) : null;
if (shouldWrap) {
if (comment != null) {
sseBuilder.comment(comment.toString().substring(0, comment.length() - 1));
}
return decodedData.map(o -> {
sseBuilder.data(o);
return sseBuilder.build();
});
if (decodedData != null) {
sseBuilder.data(decodedData);
}
return sseBuilder.build();
}
else {
return decodedData;
}
}
private Mono<?> decodeData(String data, ResolvableType dataType, Map<String, Object> hints) {
private Object decodeData(String data, ResolvableType dataType, Map<String, Object> hints) {
if (String.class == dataType.resolve()) {
return Mono.just(data.substring(0, data.length() - 1));
return data.substring(0, data.length() - 1);
}
if (this.decoder == null) {
return Mono.error(new CodecException("No SSE decoder configured and the data is not String."));
throw new CodecException("No SSE decoder configured and the data is not String.");
}
byte[] bytes = data.getBytes(StandardCharsets.UTF_8);
DataBuffer buffer = bufferFactory.wrap(bytes); // wrapping only, no allocation
return this.decoder.decodeToMono(Mono.just(buffer), dataType, MediaType.TEXT_EVENT_STREAM, hints);
return this.decoder.decode(buffer, dataType, MediaType.TEXT_EVENT_STREAM, hints);
}
@Override