Skip to content

Commit dbd5f67

Browse files
committed
Consistently applied appropriate ByteArrayOutputStream initial capacities across the codebase
Issue: SPR-11594 (cherry picked from commit dd7f54c)
1 parent ab85aa2 commit dbd5f67

File tree

17 files changed

+101
-97
lines changed

17 files changed

+101
-97
lines changed

spring-core/src/main/java/org/springframework/core/convert/support/PropertiesToStringConverter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2009 the original author or authors.
2+
* Copyright 2002-2014 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.
@@ -33,7 +33,7 @@ final class PropertiesToStringConverter implements Converter<Properties, String>
3333

3434
public String convert(Properties source) {
3535
try {
36-
ByteArrayOutputStream os = new ByteArrayOutputStream();
36+
ByteArrayOutputStream os = new ByteArrayOutputStream(256);
3737
source.store(os, null);
3838
return os.toString("ISO-8859-1");
3939
}

spring-core/src/main/java/org/springframework/core/serializer/support/SerializingConverter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2010 the original author or authors.
2+
* Copyright 2002-2014 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.
@@ -56,7 +56,7 @@ public SerializingConverter(Serializer<Object> serializer) {
5656
* Serializes the source object and returns the byte array result.
5757
*/
5858
public byte[] convert(Object source) {
59-
ByteArrayOutputStream byteStream = new ByteArrayOutputStream(128);
59+
ByteArrayOutputStream byteStream = new ByteArrayOutputStream(256);
6060
try {
6161
this.serializer.serialize(source, byteStream);
6262
return byteStream.toByteArray();

spring-core/src/main/java/org/springframework/core/style/ToStringCreator.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2014 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.
@@ -36,11 +36,11 @@ public class ToStringCreator {
3636
new DefaultToStringStyler(StylerUtils.DEFAULT_VALUE_STYLER);
3737

3838

39-
private StringBuilder buffer = new StringBuilder(512);
39+
private final StringBuilder buffer = new StringBuilder(256);
4040

41-
private ToStringStyler styler;
41+
private final ToStringStyler styler;
4242

43-
private Object object;
43+
private final Object object;
4444

4545
private boolean styledFirstField;
4646

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2010 the original author or authors.
2+
* Copyright 2002-2014 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.
@@ -39,7 +39,7 @@ public static byte[] serialize(Object object) {
3939
if (object == null) {
4040
return null;
4141
}
42-
ByteArrayOutputStream baos = new ByteArrayOutputStream();
42+
ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
4343
try {
4444
ObjectOutputStream oos = new ObjectOutputStream(baos);
4545
oos.writeObject(object);

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-2013 the original author or authors.
2+
* Copyright 2002-2014 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.
@@ -213,7 +213,7 @@ protected TextMessage mapToTextMessage(Object object, Session session, ObjectMap
213213
protected BytesMessage mapToBytesMessage(Object object, Session session, ObjectMapper objectMapper)
214214
throws JMSException, IOException {
215215

216-
ByteArrayOutputStream bos = new ByteArrayOutputStream();
216+
ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
217217
OutputStreamWriter writer = new OutputStreamWriter(bos, this.encoding);
218218
objectMapper.writeValue(writer, object);
219219

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 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.
@@ -214,7 +214,7 @@ protected TextMessage mapToTextMessage(Object object, Session session, ObjectMap
214214
protected BytesMessage mapToBytesMessage(Object object, Session session, ObjectMapper objectMapper)
215215
throws JMSException, IOException {
216216

217-
ByteArrayOutputStream bos = new ByteArrayOutputStream();
217+
ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
218218
OutputStreamWriter writer = new OutputStreamWriter(bos, this.encoding);
219219
objectMapper.writeValue(writer, object);
220220

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2014 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.
@@ -228,7 +228,7 @@ protected TextMessage marshalToTextMessage(Object object, Session session, Marsh
228228
protected BytesMessage marshalToBytesMessage(Object object, Session session, Marshaller marshaller)
229229
throws JMSException, IOException, XmlMappingException {
230230

231-
ByteArrayOutputStream bos = new ByteArrayOutputStream();
231+
ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
232232
StreamResult streamResult = new StreamResult(bos);
233233
marshaller.marshal(object, streamResult);
234234
BytesMessage message = session.createBytesMessage();

spring-oxm/src/main/java/org/springframework/oxm/jibx/JibxMarshaller.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ public void setTargetClass(Class<?> targetClass) {
128128
public void setTargetPackage(String targetPackage) {
129129
this.targetPackage = targetPackage;
130130
}
131+
131132
/**
132133
* Set the optional binding name for this instance.
133134
*/
@@ -333,15 +334,15 @@ protected void marshalSaxHandlers(Object graph, ContentHandler contentHandler, L
333334

334335
private void transformAndMarshal(Object graph, Result result) throws IOException {
335336
try {
336-
ByteArrayOutputStream os = new ByteArrayOutputStream();
337+
ByteArrayOutputStream os = new ByteArrayOutputStream(1024);
337338
marshalOutputStream(graph, os);
338339
ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
339340
Transformer transformer = this.transformerFactory.newTransformer();
340341
transformer.transform(new StreamSource(is), result);
341342
}
342343
catch (TransformerException ex) {
343344
throw new MarshallingFailureException(
344-
"Could not transform to [" + ClassUtils.getShortName(result.getClass()) + "]");
345+
"Could not transform to [" + ClassUtils.getShortName(result.getClass()) + "]", ex);
345346
}
346347

347348
}
@@ -395,6 +396,7 @@ protected Object unmarshalReader(Reader reader) throws XmlMappingException, IOEx
395396
}
396397
}
397398

399+
398400
// Unsupported Unmarshalling
399401

400402
@Override
@@ -420,7 +422,7 @@ private Object transformAndUnmarshal(Source source, String encoding) throws IOEx
420422
if (encoding != null) {
421423
transformer.setOutputProperty(OutputKeys.ENCODING, encoding);
422424
}
423-
ByteArrayOutputStream os = new ByteArrayOutputStream();
425+
ByteArrayOutputStream os = new ByteArrayOutputStream(1024);
424426
transformer.transform(source, new StreamResult(os));
425427
ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
426428
return unmarshalInputStream(is);

spring-test/src/main/java/org/springframework/mock/http/MockHttpOutputMessage.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2014 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.
@@ -13,6 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
1617
package org.springframework.mock.http;
1718

1819
import java.io.ByteArrayOutputStream;
@@ -36,7 +37,7 @@ public class MockHttpOutputMessage implements HttpOutputMessage {
3637

3738
private final HttpHeaders headers = new HttpHeaders();
3839

39-
private final ByteArrayOutputStream body = new ByteArrayOutputStream();
40+
private final ByteArrayOutputStream body = new ByteArrayOutputStream(1024);
4041

4142

4243
/**
@@ -74,7 +75,6 @@ public String getBodyAsString() {
7475
public String getBodyAsString(Charset charset) {
7576
byte[] bytes = getBodyAsBytes();
7677
try {
77-
// Use
7878
return new String(bytes, charset.name());
7979
}
8080
catch (UnsupportedEncodingException ex) {

spring-test/src/main/java/org/springframework/mock/web/MockHttpServletResponse.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 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.
@@ -69,7 +69,7 @@ public class MockHttpServletResponse implements HttpServletResponse {
6969

7070
private boolean charset = false;
7171

72-
private final ByteArrayOutputStream content = new ByteArrayOutputStream();
72+
private final ByteArrayOutputStream content = new ByteArrayOutputStream(1024);
7373

7474
private final ServletOutputStream outputStream = new ResponseServletOutputStream(this.content);
7575

@@ -183,8 +183,8 @@ public byte[] getContentAsByteArray() {
183183

184184
public String getContentAsString() throws UnsupportedEncodingException {
185185
flushBuffer();
186-
return (this.characterEncoding != null) ?
187-
this.content.toString(this.characterEncoding) : this.content.toString();
186+
return (this.characterEncoding != null ?
187+
this.content.toString(this.characterEncoding) : this.content.toString());
188188
}
189189

190190
public void setContentLength(int contentLength) {
@@ -201,8 +201,7 @@ public void setContentType(String contentType) {
201201
if (contentType != null) {
202202
int charsetIndex = contentType.toLowerCase().indexOf(CHARSET_PREFIX);
203203
if (charsetIndex != -1) {
204-
String encoding = contentType.substring(charsetIndex + CHARSET_PREFIX.length());
205-
this.characterEncoding = encoding;
204+
this.characterEncoding = contentType.substring(charsetIndex + CHARSET_PREFIX.length());
206205
this.charset = true;
207206
}
208207
updateContentTypeHeader();

0 commit comments

Comments
 (0)