diff --git a/spring-context-support/src/main/java/org/springframework/ui/freemarker/FreeMarkerTemplateUtils.java b/spring-context-support/src/main/java/org/springframework/ui/freemarker/FreeMarkerTemplateUtils.java index 2c9fa7e478..e1ca06bf99 100644 --- a/spring-context-support/src/main/java/org/springframework/ui/freemarker/FreeMarkerTemplateUtils.java +++ b/spring-context-support/src/main/java/org/springframework/ui/freemarker/FreeMarkerTemplateUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -46,7 +46,7 @@ public abstract class FreeMarkerTemplateUtils { public static String processTemplateIntoString(Template template, Object model) throws IOException, TemplateException { - StringWriter result = new StringWriter(); + StringWriter result = new StringWriter(1024); template.process(model, result); return result.toString(); } diff --git a/spring-core/src/main/java/org/springframework/util/FileCopyUtils.java b/spring-core/src/main/java/org/springframework/util/FileCopyUtils.java index d36e698d44..83350ad2d2 100644 --- a/spring-core/src/main/java/org/springframework/util/FileCopyUtils.java +++ b/spring-core/src/main/java/org/springframework/util/FileCopyUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -181,15 +181,15 @@ public abstract class FileCopyUtils { Assert.notNull(out, "No Writer specified"); try { - int byteCount = 0; + int charCount = 0; char[] buffer = new char[BUFFER_SIZE]; - int bytesRead = -1; - while ((bytesRead = in.read(buffer)) != -1) { - out.write(buffer, 0, bytesRead); - byteCount += bytesRead; + int charsRead; + while ((charsRead = in.read(buffer)) != -1) { + out.write(buffer, 0, charsRead); + charCount += charsRead; } out.flush(); - return byteCount; + return charCount; } finally { try { @@ -206,7 +206,7 @@ public abstract class FileCopyUtils { } /** - * Copy the contents of the given String to the given output Writer. + * Copy the contents of the given String to the given Writer. * Closes the writer when done. * @param in the String to copy from * @param out the Writer to copy to @@ -240,7 +240,7 @@ public abstract class FileCopyUtils { return ""; } - StringWriter out = new StringWriter(); + StringWriter out = new StringWriter(BUFFER_SIZE); copy(in, out); return out.toString(); } diff --git a/spring-jms/src/main/java/org/springframework/jms/support/converter/MappingJackson2MessageConverter.java b/spring-jms/src/main/java/org/springframework/jms/support/converter/MappingJackson2MessageConverter.java index 37edf692c6..279382e139 100644 --- a/spring-jms/src/main/java/org/springframework/jms/support/converter/MappingJackson2MessageConverter.java +++ b/spring-jms/src/main/java/org/springframework/jms/support/converter/MappingJackson2MessageConverter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -275,7 +275,7 @@ public class MappingJackson2MessageConverter implements SmartMessageConverter, B protected TextMessage mapToTextMessage(Object object, Session session, ObjectWriter objectWriter) throws JMSException, IOException { - StringWriter writer = new StringWriter(); + StringWriter writer = new StringWriter(1024); objectWriter.writeValue(writer, object); return session.createTextMessage(writer.toString()); } diff --git a/spring-jms/src/main/java/org/springframework/jms/support/converter/MarshallingMessageConverter.java b/spring-jms/src/main/java/org/springframework/jms/support/converter/MarshallingMessageConverter.java index 0c6a7cc482..99c38631fd 100644 --- a/spring-jms/src/main/java/org/springframework/jms/support/converter/MarshallingMessageConverter.java +++ b/spring-jms/src/main/java/org/springframework/jms/support/converter/MarshallingMessageConverter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -48,8 +48,6 @@ import org.springframework.util.Assert; * @author Arjen Poutsma * @author Juergen Hoeller * @since 3.0 - * @see org.springframework.jms.core.JmsTemplate#convertAndSend - * @see org.springframework.jms.core.JmsTemplate#receiveAndConvert */ public class MarshallingMessageConverter implements MessageConverter, InitializingBean { @@ -215,7 +213,7 @@ public class MarshallingMessageConverter implements MessageConverter, Initializi protected TextMessage marshalToTextMessage(Object object, Session session, Marshaller marshaller) throws JMSException, IOException, XmlMappingException { - StringWriter writer = new StringWriter(); + StringWriter writer = new StringWriter(1024); Result result = new StreamResult(writer); marshaller.marshal(object, result); return session.createTextMessage(writer.toString()); diff --git a/spring-messaging/src/main/java/org/springframework/messaging/converter/MappingJackson2MessageConverter.java b/spring-messaging/src/main/java/org/springframework/messaging/converter/MappingJackson2MessageConverter.java index 0e02137d90..c38eb5f302 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/converter/MappingJackson2MessageConverter.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/converter/MappingJackson2MessageConverter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -211,9 +211,11 @@ public class MappingJackson2MessageConverter extends AbstractMessageConverter { JavaType javaType = getJavaType(targetClass, conversionHint); Object payload = message.getPayload(); Class view = getSerializationView(conversionHint); - // Note: in the view case, calling withType instead of forType for compatibility with Jackson <2.5 try { - if (payload instanceof byte[]) { + if (targetClass.isInstance(payload)) { + return payload; + } + else if (payload instanceof byte[]) { if (view != null) { return this.objectMapper.readerWithView(view).forType(javaType).readValue((byte[]) payload); } @@ -222,6 +224,7 @@ public class MappingJackson2MessageConverter extends AbstractMessageConverter { } } else { + // Assuming a text-based source payload if (view != null) { return this.objectMapper.readerWithView(view).forType(javaType).readValue(payload.toString()); } @@ -270,7 +273,8 @@ public class MappingJackson2MessageConverter extends AbstractMessageConverter { payload = out.toByteArray(); } else { - Writer writer = new StringWriter(); + // Assuming a text-based target payload + Writer writer = new StringWriter(1024); if (view != null) { this.objectMapper.writerWithView(view).writeValue(writer, payload); } diff --git a/spring-messaging/src/main/java/org/springframework/messaging/converter/MarshallingMessageConverter.java b/spring-messaging/src/main/java/org/springframework/messaging/converter/MarshallingMessageConverter.java index 3a477c79f4..daef525b18 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/converter/MarshallingMessageConverter.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/converter/MarshallingMessageConverter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -173,13 +173,13 @@ public class MarshallingMessageConverter extends AbstractMessageConverter { Assert.notNull(this.marshaller, "Property 'marshaller' is required"); try { if (byte[].class == getSerializedPayloadClass()) { - ByteArrayOutputStream out = new ByteArrayOutputStream(); + ByteArrayOutputStream out = new ByteArrayOutputStream(1024); Result result = new StreamResult(out); this.marshaller.marshal(payload, result); payload = out.toByteArray(); } else { - Writer writer = new StringWriter(); + Writer writer = new StringWriter(1024); Result result = new StreamResult(writer); this.marshaller.marshal(payload, result); payload = writer.toString();