Use DataBuffer::toString instead of CharBuffer

This commit ensures that converting DataBuffers to a String does not
use an expensive ByteBuffer allocation.

Closes gh-29889
This commit is contained in:
xavier-b
2023-01-27 00:53:55 +01:00
committed by Arjen Poutsma
parent f9884e08af
commit b7d53ac9a4
2 changed files with 2 additions and 6 deletions

View File

@@ -16,7 +16,6 @@
package org.springframework.core.codec;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
@@ -189,9 +188,8 @@ public final class StringDecoder extends AbstractDataBufferDecoder<String> {
@Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {
Charset charset = getCharset(mimeType);
CharBuffer charBuffer = charset.decode(dataBuffer.toByteBuffer());
String value = dataBuffer.toString(charset);
DataBufferUtils.release(dataBuffer);
String value = charBuffer.toString();
LogFormatUtils.traceDebug(logger, traceOn -> {
String formatted = LogFormatUtils.formatValue(value, !traceOn);
return Hints.getLogPrefix(hints) + "Decoded " + formatted;