Browse Source

Release DataBuffer in AbstractCharSequenceDecoder

if String creation fails

See gh-35625

Signed-off-by: Marius Lichtblau <marius@lichtblau.io>
pull/35672/head
Marius Lichtblau 2 months ago committed by rstoyanchev
parent
commit
ba2bb08589
  1. 20
      spring-core/src/main/java/org/springframework/core/codec/AbstractCharSequenceDecoder.java
  2. 4
      spring-core/src/main/java/org/springframework/core/io/buffer/DataBufferLimitException.java
  3. 7
      spring-core/src/main/java/org/springframework/core/io/buffer/NettyDataBuffer.java

20
spring-core/src/main/java/org/springframework/core/codec/AbstractCharSequenceDecoder.java

@ -175,14 +175,18 @@ public abstract class AbstractCharSequenceDecoder<T extends CharSequence> extend @@ -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) {

4
spring-core/src/main/java/org/springframework/core/io/buffer/DataBufferLimitException.java

@ -35,4 +35,8 @@ public class DataBufferLimitException extends IllegalStateException { @@ -35,4 +35,8 @@ public class DataBufferLimitException extends IllegalStateException {
super(message);
}
public DataBufferLimitException(String message, Throwable cause) {
super(message, cause);
}
}

7
spring-core/src/main/java/org/springframework/core/io/buffer/NettyDataBuffer.java

@ -379,7 +379,12 @@ public class NettyDataBuffer implements PooledDataBuffer { @@ -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);
}
}

Loading…
Cancel
Save