Construct StringWriter instances with appropriate initial size

Closes gh-25789
This commit is contained in:
Juergen Hoeller
2020-10-07 14:36:30 +02:00
parent 01d86dc498
commit 6d137b56c1
7 changed files with 60 additions and 48 deletions

View File

@@ -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.
@@ -141,6 +141,7 @@ public class MappingJackson2MessageConverter extends AbstractMessageConverter {
}
}
@Override
protected boolean canConvertFrom(Message<?> message, @Nullable Class<?> targetClass) {
if (targetClass == null || !supportsMimeType(message.getHeaders())) {
@@ -223,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());
}
@@ -246,7 +248,7 @@ public class MappingJackson2MessageConverter extends AbstractMessageConverter {
Type genericParameterType = param.getNestedGenericParameterType();
Class<?> contextClass = param.getContainingClass();
Type type = GenericTypeResolver.resolveType(genericParameterType, contextClass);
return this.objectMapper.getTypeFactory().constructType(type);
return this.objectMapper.constructType(type);
}
return this.objectMapper.constructType(targetClass);
}
@@ -271,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);
}
@@ -330,7 +333,7 @@ public class MappingJackson2MessageConverter extends AbstractMessageConverter {
* @return the JSON encoding to use (never {@code null})
*/
protected JsonEncoding getJsonEncoding(@Nullable MimeType contentType) {
if (contentType != null && (contentType.getCharset() != null)) {
if (contentType != null && contentType.getCharset() != null) {
Charset charset = contentType.getCharset();
for (JsonEncoding encoding : JsonEncoding.values()) {
if (charset.name().equals(encoding.getJavaName())) {

View File

@@ -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.
@@ -46,6 +46,8 @@ import org.springframework.util.MimeType;
*
* @author Arjen Poutsma
* @since 4.2
* @see Marshaller
* @see Unmarshaller
*/
public class MarshallingMessageConverter extends AbstractMessageConverter {
@@ -61,7 +63,8 @@ public class MarshallingMessageConverter extends AbstractMessageConverter {
* {@link #setUnmarshaller(Unmarshaller)} to be invoked separately.
*/
public MarshallingMessageConverter() {
this(new MimeType("application", "xml"), new MimeType("text", "xml"), new MimeType("application", "*+xml"));
this(new MimeType("application", "xml"), new MimeType("text", "xml"),
new MimeType("application", "*+xml"));
}
/**
@@ -160,7 +163,7 @@ public class MarshallingMessageConverter extends AbstractMessageConverter {
return new StreamSource(new ByteArrayInputStream((byte[]) payload));
}
else {
return new StreamSource(new StringReader((String) payload));
return new StreamSource(new StringReader(payload.toString()));
}
}
@@ -172,13 +175,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();