Remove duplicate flushes in HttpMessageConverter implementations

Prior to this commit, a few implementations of the `HttpMessageConverter`
contract were inheriting from abstract classes. Those classes were
performing extra `OutputStream#flush` on the response body even though
this is the responsibility of the super class. Such abstract classes
do flush already, after delegating to the `writeInternal` method.

This commit ensures that we remove such extra calls as they tend to
waste resources for no added benefit.

Closes gh-36383
This commit is contained in:
Brian Clozel
2026-02-24 16:49:37 +01:00
parent b1cb9c5052
commit a37447e07f
5 changed files with 1 additions and 7 deletions
@@ -23,7 +23,6 @@ import org.jspecify.annotations.Nullable;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
import org.springframework.util.StreamUtils;
/**
* Implementation of {@link HttpMessageConverter} that can read and write byte arrays.
@@ -65,7 +64,7 @@ public class ByteArrayHttpMessageConverter extends AbstractHttpMessageConverter<
@Override
protected void writeInternal(byte[] bytes, HttpOutputMessage outputMessage) throws IOException {
StreamUtils.copy(bytes, outputMessage.getBody());
outputMessage.getBody().write(bytes);
}
@Override
@@ -91,7 +91,6 @@ public abstract class KotlinSerializationBinaryHttpMessageConverter<T extends Bi
try {
byte[] bytes = format.encodeToByteArray(serializer, object);
outputMessage.getBody().write(bytes);
outputMessage.getBody().flush();
}
catch (SerializationException ex) {
throw new HttpMessageNotWritableException("Could not write " + format + ": " + ex.getMessage(), ex);
@@ -98,7 +98,6 @@ public abstract class KotlinSerializationStringHttpMessageConverter<T extends St
String s = format.encodeToString(serializer, object);
Charset charset = charset(outputMessage.getHeaders().getContentType());
outputMessage.getBody().write(s.getBytes(charset));
outputMessage.getBody().flush();
}
catch (SerializationException ex) {
throw new HttpMessageNotWritableException("Could not write " + format + ": " + ex.getMessage(), ex);
@@ -156,7 +156,6 @@ public class ResourceHttpMessageConverter extends AbstractHttpMessageConverter<R
try {
OutputStream out = outputMessage.getBody();
in.transferTo(out);
out.flush();
}
catch (NullPointerException ignored) {
// see SPR-13620
@@ -237,11 +237,9 @@ public class ProtobufHttpMessageConverter extends AbstractHttpMessageConverter<M
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputMessage.getBody(), charset);
TextFormat.printer().print(message, outputStreamWriter);
outputStreamWriter.flush();
outputMessage.getBody().flush();
}
else if (this.protobufFormatDelegate != null) {
this.protobufFormatDelegate.print(message, outputMessage, contentType, charset);
outputMessage.getBody().flush();
}
}