Release DataBuffer in AbstractCharSequenceDecoder

if String creation fails

See gh-35625

Signed-off-by: Marius Lichtblau <marius@lichtblau.io>
This commit is contained in:
Marius Lichtblau
2025-10-13 17:58:04 +02:00
committed by rstoyanchev
parent 141df5291d
commit ba2bb08589
3 changed files with 22 additions and 9 deletions
@@ -175,14 +175,18 @@ public abstract class AbstractCharSequenceDecoder<T extends CharSequence> extend
public final T decode(DataBuffer dataBuffer, ResolvableType elementType,
@Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {
Charset charset = getCharset(mimeType);
T value = decodeInternal(dataBuffer, charset);
DataBufferUtils.release(dataBuffer);
LogFormatUtils.traceDebug(logger, traceOn -> {
String formatted = LogFormatUtils.formatValue(value, !traceOn);
return Hints.getLogPrefix(hints) + "Decoded " + formatted;
});
return value;
try {
Charset charset = getCharset(mimeType);
T value = decodeInternal(dataBuffer, charset);
LogFormatUtils.traceDebug(logger, traceOn -> {
String formatted = LogFormatUtils.formatValue(value, !traceOn);
return Hints.getLogPrefix(hints) + "Decoded " + formatted;
});
return value;
}
finally {
DataBufferUtils.release(dataBuffer);
}
}
private Charset getCharset(@Nullable MimeType mimeType) {
@@ -35,4 +35,8 @@ public class DataBufferLimitException extends IllegalStateException {
super(message);
}
public DataBufferLimitException(String message, Throwable cause) {
super(message, cause);
}
}
@@ -379,7 +379,12 @@ public class NettyDataBuffer implements PooledDataBuffer {
@Override
public String toString() {
return this.byteBuf.toString();
try {
return this.byteBuf.toString();
}
catch (OutOfMemoryError ex) {
throw new DataBufferLimitException("Failed to convert data buffer to string: " + ex.getMessage(), ex);
}
}