Revised ResizableByteArrayOutputStream as an actual subclass of ByteArrayOutputStream, and consistently applied appropriate ByteArrayOutputStream initial capacities across the codebase
Issue: SPR-11594
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2014 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.
|
||||
@@ -13,6 +13,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.mock.http;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
@@ -36,7 +37,7 @@ public class MockHttpOutputMessage implements HttpOutputMessage {
|
||||
|
||||
private final HttpHeaders headers = new HttpHeaders();
|
||||
|
||||
private final ByteArrayOutputStream body = new ByteArrayOutputStream();
|
||||
private final ByteArrayOutputStream body = new ByteArrayOutputStream(1024);
|
||||
|
||||
|
||||
/**
|
||||
@@ -76,7 +77,6 @@ public class MockHttpOutputMessage implements HttpOutputMessage {
|
||||
public String getBodyAsString(Charset charset) {
|
||||
byte[] bytes = getBodyAsBytes();
|
||||
try {
|
||||
// Use
|
||||
return new String(bytes, charset.name());
|
||||
}
|
||||
catch (UnsupportedEncodingException ex) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
* Copyright 2002-2014 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.
|
||||
@@ -71,7 +71,7 @@ public class MockHttpServletResponse implements HttpServletResponse {
|
||||
|
||||
private boolean charset = false;
|
||||
|
||||
private final ByteArrayOutputStream content = new ByteArrayOutputStream();
|
||||
private final ByteArrayOutputStream content = new ByteArrayOutputStream(1024);
|
||||
|
||||
private final ServletOutputStream outputStream = new ResponseServletOutputStream(this.content);
|
||||
|
||||
@@ -189,8 +189,8 @@ public class MockHttpServletResponse implements HttpServletResponse {
|
||||
|
||||
public String getContentAsString() throws UnsupportedEncodingException {
|
||||
flushBuffer();
|
||||
return (this.characterEncoding != null) ?
|
||||
this.content.toString(this.characterEncoding) : this.content.toString();
|
||||
return (this.characterEncoding != null ?
|
||||
this.content.toString(this.characterEncoding) : this.content.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -218,8 +218,7 @@ public class MockHttpServletResponse implements HttpServletResponse {
|
||||
if (contentType != null) {
|
||||
int charsetIndex = contentType.toLowerCase().indexOf(CHARSET_PREFIX);
|
||||
if (charsetIndex != -1) {
|
||||
String encoding = contentType.substring(charsetIndex + CHARSET_PREFIX.length());
|
||||
this.characterEncoding = encoding;
|
||||
this.characterEncoding = contentType.substring(charsetIndex + CHARSET_PREFIX.length());
|
||||
this.charset = true;
|
||||
}
|
||||
updateContentTypeHeader();
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2014 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.
|
||||
@@ -56,7 +56,7 @@ public class MockMimeResponse extends MockPortletResponse implements MimeRespons
|
||||
|
||||
private int bufferSize = 4096;
|
||||
|
||||
private final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||
private final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(1024);
|
||||
|
||||
private final CacheControl cacheControl = new MockCacheControl();
|
||||
|
||||
@@ -130,9 +130,9 @@ public class MockMimeResponse extends MockPortletResponse implements MimeRespons
|
||||
@Override
|
||||
public PrintWriter getWriter() throws UnsupportedEncodingException {
|
||||
if (this.writer == null) {
|
||||
Writer targetWriter = (this.characterEncoding != null
|
||||
? new OutputStreamWriter(this.outputStream, this.characterEncoding)
|
||||
: new OutputStreamWriter(this.outputStream));
|
||||
Writer targetWriter = (this.characterEncoding != null ?
|
||||
new OutputStreamWriter(this.outputStream, this.characterEncoding) :
|
||||
new OutputStreamWriter(this.outputStream));
|
||||
this.writer = new PrintWriter(targetWriter);
|
||||
}
|
||||
return this.writer;
|
||||
@@ -145,9 +145,8 @@ public class MockMimeResponse extends MockPortletResponse implements MimeRespons
|
||||
|
||||
public String getContentAsString() throws UnsupportedEncodingException {
|
||||
flushBuffer();
|
||||
return (this.characterEncoding != null)
|
||||
? this.outputStream.toString(this.characterEncoding)
|
||||
: this.outputStream.toString();
|
||||
return (this.characterEncoding != null ?
|
||||
this.outputStream.toString(this.characterEncoding) : this.outputStream.toString());
|
||||
}
|
||||
|
||||
public void setLocale(Locale locale) {
|
||||
@@ -174,13 +173,11 @@ public class MockMimeResponse extends MockPortletResponse implements MimeRespons
|
||||
if (this.writer != null) {
|
||||
this.writer.flush();
|
||||
}
|
||||
if (this.outputStream != null) {
|
||||
try {
|
||||
this.outputStream.flush();
|
||||
}
|
||||
catch (IOException ex) {
|
||||
throw new IllegalStateException("Could not flush OutputStream: " + ex.getMessage());
|
||||
}
|
||||
try {
|
||||
this.outputStream.flush();
|
||||
}
|
||||
catch (IOException ex) {
|
||||
throw new IllegalStateException("Could not flush OutputStream: " + ex.getMessage());
|
||||
}
|
||||
this.committed = true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user