Skip to content

Commit dc00380

Browse files
committed
Avoid creating byte[] multiple times
1 parent 4bfc129 commit dc00380

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

spring-web/src/main/java/org/springframework/http/converter/StringHttpMessageConverter.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,7 @@ protected String readInternal(Class<? extends String> clazz, HttpInputMessage in
101101

102102
@Override
103103
protected Long getContentLength(String str, @Nullable MediaType contentType) {
104-
Charset charset = getContentTypeCharset(contentType);
105-
return (long) str.getBytes(charset).length;
104+
return null;
106105
}
107106

108107

@@ -125,7 +124,11 @@ protected void writeInternal(String str, HttpOutputMessage outputMessage) throws
125124
headers.setAcceptCharset(getAcceptedCharsets());
126125
}
127126
Charset charset = getContentTypeCharset(headers.getContentType());
128-
StreamUtils.copy(str, charset, outputMessage.getBody());
127+
byte[] strBytes = str.getBytes(charset);
128+
if (!headers.containsHeader(HttpHeaders.TRANSFER_ENCODING)) {
129+
headers.setContentLength(strBytes.length);
130+
}
131+
StreamUtils.copy(strBytes, outputMessage.getBody());
129132
}
130133

131134

0 commit comments

Comments
 (0)