Skip to content

Commit 5b4f3e8

Browse files
committed
Construct StringWriter instances with appropriate initial size
Closes gh-25789 (cherry picked from commit 9dfef59)
1 parent 547a139 commit 5b4f3e8

File tree

6 files changed

+26
-24
lines changed

6 files changed

+26
-24
lines changed

spring-context-support/src/main/java/org/springframework/ui/freemarker/FreeMarkerTemplateUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -46,7 +46,7 @@ public abstract class FreeMarkerTemplateUtils {
4646
public static String processTemplateIntoString(Template template, Object model)
4747
throws IOException, TemplateException {
4848

49-
StringWriter result = new StringWriter();
49+
StringWriter result = new StringWriter(1024);
5050
template.process(model, result);
5151
return result.toString();
5252
}

spring-core/src/main/java/org/springframework/util/FileCopyUtils.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -181,15 +181,15 @@ public static int copy(Reader in, Writer out) throws IOException {
181181
Assert.notNull(out, "No Writer specified");
182182

183183
try {
184-
int byteCount = 0;
184+
int charCount = 0;
185185
char[] buffer = new char[BUFFER_SIZE];
186-
int bytesRead = -1;
187-
while ((bytesRead = in.read(buffer)) != -1) {
188-
out.write(buffer, 0, bytesRead);
189-
byteCount += bytesRead;
186+
int charsRead;
187+
while ((charsRead = in.read(buffer)) != -1) {
188+
out.write(buffer, 0, charsRead);
189+
charCount += charsRead;
190190
}
191191
out.flush();
192-
return byteCount;
192+
return charCount;
193193
}
194194
finally {
195195
try {
@@ -206,7 +206,7 @@ public static int copy(Reader in, Writer out) throws IOException {
206206
}
207207

208208
/**
209-
* Copy the contents of the given String to the given output Writer.
209+
* Copy the contents of the given String to the given Writer.
210210
* Closes the writer when done.
211211
* @param in the String to copy from
212212
* @param out the Writer to copy to
@@ -240,7 +240,7 @@ public static String copyToString(@Nullable Reader in) throws IOException {
240240
return "";
241241
}
242242

243-
StringWriter out = new StringWriter();
243+
StringWriter out = new StringWriter(BUFFER_SIZE);
244244
copy(in, out);
245245
return out.toString();
246246
}

spring-jms/src/main/java/org/springframework/jms/support/converter/MappingJackson2MessageConverter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -275,7 +275,7 @@ protected Message toMessage(Object object, Session session, ObjectWriter objectW
275275
protected TextMessage mapToTextMessage(Object object, Session session, ObjectWriter objectWriter)
276276
throws JMSException, IOException {
277277

278-
StringWriter writer = new StringWriter();
278+
StringWriter writer = new StringWriter(1024);
279279
objectWriter.writeValue(writer, object);
280280
return session.createTextMessage(writer.toString());
281281
}

spring-jms/src/main/java/org/springframework/jms/support/converter/MarshallingMessageConverter.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -48,8 +48,6 @@
4848
* @author Arjen Poutsma
4949
* @author Juergen Hoeller
5050
* @since 3.0
51-
* @see org.springframework.jms.core.JmsTemplate#convertAndSend
52-
* @see org.springframework.jms.core.JmsTemplate#receiveAndConvert
5351
*/
5452
public class MarshallingMessageConverter implements MessageConverter, InitializingBean {
5553

@@ -215,7 +213,7 @@ else if (message instanceof BytesMessage) {
215213
protected TextMessage marshalToTextMessage(Object object, Session session, Marshaller marshaller)
216214
throws JMSException, IOException, XmlMappingException {
217215

218-
StringWriter writer = new StringWriter();
216+
StringWriter writer = new StringWriter(1024);
219217
Result result = new StreamResult(writer);
220218
marshaller.marshal(object, result);
221219
return session.createTextMessage(writer.toString());

spring-messaging/src/main/java/org/springframework/messaging/converter/MappingJackson2MessageConverter.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -211,9 +211,11 @@ protected Object convertFromInternal(Message<?> message, Class<?> targetClass, @
211211
JavaType javaType = getJavaType(targetClass, conversionHint);
212212
Object payload = message.getPayload();
213213
Class<?> view = getSerializationView(conversionHint);
214-
// Note: in the view case, calling withType instead of forType for compatibility with Jackson <2.5
215214
try {
216-
if (payload instanceof byte[]) {
215+
if (targetClass.isInstance(payload)) {
216+
return payload;
217+
}
218+
else if (payload instanceof byte[]) {
217219
if (view != null) {
218220
return this.objectMapper.readerWithView(view).forType(javaType).readValue((byte[]) payload);
219221
}
@@ -222,6 +224,7 @@ protected Object convertFromInternal(Message<?> message, Class<?> targetClass, @
222224
}
223225
}
224226
else {
227+
// Assuming a text-based source payload
225228
if (view != null) {
226229
return this.objectMapper.readerWithView(view).forType(javaType).readValue(payload.toString());
227230
}
@@ -270,7 +273,8 @@ protected Object convertToInternal(Object payload, @Nullable MessageHeaders head
270273
payload = out.toByteArray();
271274
}
272275
else {
273-
Writer writer = new StringWriter();
276+
// Assuming a text-based target payload
277+
Writer writer = new StringWriter(1024);
274278
if (view != null) {
275279
this.objectMapper.writerWithView(view).writeValue(writer, payload);
276280
}

spring-messaging/src/main/java/org/springframework/messaging/converter/MarshallingMessageConverter.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -173,13 +173,13 @@ protected Object convertToInternal(Object payload, @Nullable MessageHeaders head
173173
Assert.notNull(this.marshaller, "Property 'marshaller' is required");
174174
try {
175175
if (byte[].class == getSerializedPayloadClass()) {
176-
ByteArrayOutputStream out = new ByteArrayOutputStream();
176+
ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
177177
Result result = new StreamResult(out);
178178
this.marshaller.marshal(payload, result);
179179
payload = out.toByteArray();
180180
}
181181
else {
182-
Writer writer = new StringWriter();
182+
Writer writer = new StringWriter(1024);
183183
Result result = new StreamResult(writer);
184184
this.marshaller.marshal(payload, result);
185185
payload = writer.toString();

0 commit comments

Comments
 (0)