Merge branch '5.2.x'

# Conflicts:
#	build.gradle
This commit is contained in:
Juergen Hoeller
2020-05-18 20:18:31 +02:00
3 changed files with 22 additions and 29 deletions

View File

@@ -47,10 +47,10 @@ import org.springframework.lang.Nullable;
*/
public class ServerSentEventHttpMessageReader implements HttpMessageReader<Object> {
private static final DataBufferFactory bufferFactory = new DefaultDataBufferFactory();
private static final ResolvableType STRING_TYPE = ResolvableType.forClass(String.class);
private static final DataBufferFactory bufferFactory = new DefaultDataBufferFactory();
@Nullable
private final Decoder<?> decoder;
@@ -58,8 +58,6 @@ public class ServerSentEventHttpMessageReader implements HttpMessageReader<Objec
private final StringDecoder lineDecoder = StringDecoder.textPlainOnly();
/**
* Constructor without a {@code Decoder}. In this mode only {@code String}
* is supported as the data of an event.
@@ -171,11 +169,11 @@ public class ServerSentEventHttpMessageReader implements HttpMessageReader<Objec
}
}
Object decodedData = (data != null ? decodeData(data.toString(), valueType, hints) : null);
Object decodedData = (data != null ? decodeData(data, valueType, hints) : null);
if (shouldWrap) {
if (comment != null) {
sseBuilder.comment(comment.toString().substring(0, comment.length() - 1));
sseBuilder.comment(comment.substring(0, comment.length() - 1));
}
if (decodedData != null) {
sseBuilder.data(decodedData);
@@ -183,19 +181,19 @@ public class ServerSentEventHttpMessageReader implements HttpMessageReader<Objec
return sseBuilder.build();
}
else {
return (decodedData);
return decodedData;
}
}
@Nullable
private Object decodeData(String data, ResolvableType dataType, Map<String, Object> hints) {
private Object decodeData(StringBuilder data, ResolvableType dataType, Map<String, Object> hints) {
if (String.class == dataType.resolve()) {
return data.substring(0, data.length() - 1);
}
if (this.decoder == null) {
throw new CodecException("No SSE decoder configured and the data is not String.");
}
byte[] bytes = data.getBytes(StandardCharsets.UTF_8);
byte[] bytes = data.toString().getBytes(StandardCharsets.UTF_8);
DataBuffer buffer = bufferFactory.wrap(bytes); // wrapping only, no allocation
return this.decoder.decode(buffer, dataType, MediaType.TEXT_EVENT_STREAM, hints);
}
@@ -221,7 +219,6 @@ public class ServerSentEventHttpMessageReader implements HttpMessageReader<Objec
private int accumulated = 0;
public void afterLineParsed(String line) {
if (getMaxInMemorySize() < 0) {
return;
@@ -242,8 +239,7 @@ public class ServerSentEventHttpMessageReader implements HttpMessageReader<Objec
private void raiseLimitException() {
// Do not release here, it's likely down via doOnDiscard..
throw new DataBufferLimitException(
"Exceeded limit on max bytes to buffer : " + getMaxInMemorySize());
throw new DataBufferLimitException("Exceeded limit on max bytes to buffer : " + getMaxInMemorySize());
}
}

View File

@@ -153,7 +153,7 @@ public class ServerSentEventHttpMessageWriter implements HttpMessageWriter<Objec
result = Flux.just(encodeText(sb + (String) data + "\n\n", mediaType, factory));
}
else {
result = encodeEvent(sb.toString(), data, dataType, mediaType, factory, hints);
result = encodeEvent(sb, data, dataType, mediaType, factory, hints);
}
return result.doOnDiscard(PooledDataBuffer.class, DataBufferUtils::release);
@@ -161,7 +161,7 @@ public class ServerSentEventHttpMessageWriter implements HttpMessageWriter<Objec
}
@SuppressWarnings("unchecked")
private <T> Flux<DataBuffer> encodeEvent(String eventContent, T data, ResolvableType dataType,
private <T> Flux<DataBuffer> encodeEvent(StringBuilder eventContent, T data, ResolvableType dataType,
MediaType mediaType, DataBufferFactory factory, Map<String, Object> hints) {
if (this.encoder == null) {
@@ -174,16 +174,13 @@ public class ServerSentEventHttpMessageWriter implements HttpMessageWriter<Objec
}
private void writeField(String fieldName, Object fieldValue, StringBuilder sb) {
sb.append(fieldName);
sb.append(':');
sb.append(fieldValue.toString());
sb.append("\n");
sb.append(fieldName).append(':').append(fieldValue).append("\n");
}
private DataBuffer encodeText(CharSequence text, MediaType mediaType, DataBufferFactory bufferFactory) {
Assert.notNull(mediaType.getCharset(), "Expected MediaType with charset");
byte[] bytes = text.toString().getBytes(mediaType.getCharset());
return bufferFactory.wrap(bytes); // wrapping, not allocating
return bufferFactory.wrap(bytes); // wrapping, not allocating
}
@Override