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:
Juergen Hoeller
2014-03-24 22:57:38 +01:00
parent 05213c684c
commit dd7f54c3c0
23 changed files with 165 additions and 233 deletions

View File

@@ -32,7 +32,7 @@ import org.springframework.util.concurrent.ListenableFuture;
*/
abstract class AbstractBufferingAsyncClientHttpRequest extends AbstractAsyncClientHttpRequest {
private ByteArrayOutputStream bufferedOutput = new ByteArrayOutputStream();
private ByteArrayOutputStream bufferedOutput = new ByteArrayOutputStream(1024);
@Override

View File

@@ -31,7 +31,7 @@ import org.springframework.http.HttpHeaders;
*/
abstract class AbstractBufferingClientHttpRequest extends AbstractClientHttpRequest {
private ByteArrayOutputStream bufferedOutput = new ByteArrayOutputStream();
private ByteArrayOutputStream bufferedOutput = new ByteArrayOutputStream(1024);
@Override

View File

@@ -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.
@@ -27,9 +27,9 @@ import org.springframework.util.StreamUtils;
/**
* Implementation of {@link HttpMessageConverter} that can read and write byte arrays.
*
* <p>By default, this converter supports all media types ({@code &#42;&#47;&#42;}), and writes with a {@code
* Content-Type} of {@code application/octet-stream}. This can be overridden by setting the {@link
* #setSupportedMediaTypes(java.util.List) supportedMediaTypes} property.
* <p>By default, this converter supports all media types ({@code &#42;&#47;&#42;}), and
* writes with a {@code Content-Type} of {@code application/octet-stream}. This can be
* overridden by setting the {@link #setSupportedMediaTypes supportedMediaTypes} property.
*
* @author Arjen Poutsma
* @since 3.0
@@ -49,7 +49,8 @@ public class ByteArrayHttpMessageConverter extends AbstractHttpMessageConverter<
@Override
public byte[] readInternal(Class<? extends byte[]> clazz, HttpInputMessage inputMessage) throws IOException {
long contentLength = inputMessage.getHeaders().getContentLength();
ByteArrayOutputStream bos = new ByteArrayOutputStream(contentLength >= 0 ? (int) contentLength : StreamUtils.BUFFER_SIZE);
ByteArrayOutputStream bos =
new ByteArrayOutputStream(contentLength >= 0 ? (int) contentLength : StreamUtils.BUFFER_SIZE);
StreamUtils.copy(inputMessage.getBody(), bos);
return bos.toByteArray();
}

View File

@@ -34,7 +34,6 @@ import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.http.HttpHeaders;
@@ -56,6 +55,7 @@ public class ServletServerHttpRequest implements ServerHttpRequest {
private static final String METHOD_POST = "POST";
private final HttpServletRequest servletRequest;
private HttpHeaders headers;
@@ -167,7 +167,7 @@ public class ServletServerHttpRequest implements ServerHttpRequest {
* to access a parameter thus causing the input stream to be "consumed".
*/
private InputStream getBodyFromServletRequestParameters(HttpServletRequest request) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
Writer writer = new OutputStreamWriter(bos, FORM_CHARSET);
Map<String, String[]> form = request.getParameterMap();

View File

@@ -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.
@@ -21,7 +21,6 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
@@ -53,9 +52,9 @@ import org.springframework.web.util.WebUtils;
* @author Rob Harrop
* @author Juergen Hoeller
* @author Rossen Stoyanchev
* @since 1.2.5
* @see #beforeRequest
* @see #afterRequest
* @since 1.2.5
*/
public abstract class AbstractRequestLoggingFilter extends OncePerRequestFilter {
@@ -69,6 +68,7 @@ public abstract class AbstractRequestLoggingFilter extends OncePerRequestFilter
private static final int DEFAULT_MAX_PAYLOAD_LENGTH = 50;
private boolean includeQueryString = false;
private boolean includeClientInfo = false;
@@ -85,6 +85,7 @@ public abstract class AbstractRequestLoggingFilter extends OncePerRequestFilter
private String afterMessageSuffix = DEFAULT_AFTER_MESSAGE_SUFFIX;
/**
* Set whether or not the query string should be included in the log message. <p>Should be configured using an
* {@code &lt;init-param&gt;} for parameter name "includeQueryString" in the filter definition in
@@ -177,6 +178,7 @@ public abstract class AbstractRequestLoggingFilter extends OncePerRequestFilter
this.afterMessageSuffix = afterMessageSuffix;
}
/**
* The default value is "false" so that the filter may log a "before" message
* at the start of request processing and an "after" message at the end from
@@ -188,9 +190,8 @@ public abstract class AbstractRequestLoggingFilter extends OncePerRequestFilter
}
/**
* Forwards the request to the next filter in the chain and delegates down to the subclasses to perform the actual
* request logging both before and after the request is processed.
*
* Forwards the request to the next filter in the chain and delegates down to the subclasses
* to perform the actual request logging both before and after the request is processed.
* @see #beforeRequest
* @see #afterRequest
*/
@@ -221,7 +222,6 @@ public abstract class AbstractRequestLoggingFilter extends OncePerRequestFilter
/**
* Get the message to write to the log before the request.
*
* @see #createMessage
*/
private String getBeforeMessage(HttpServletRequest request) {
@@ -230,7 +230,6 @@ public abstract class AbstractRequestLoggingFilter extends OncePerRequestFilter
/**
* Get the message to write to the log after the request.
*
* @see #createMessage
*/
private String getAfterMessage(HttpServletRequest request) {
@@ -238,10 +237,12 @@ public abstract class AbstractRequestLoggingFilter extends OncePerRequestFilter
}
/**
* Create a log message for the given request, prefix and suffix. <p>If {@code includeQueryString} is
* {@code true} then the inner part of the log message will take the form {@code request_uri?query_string}
* otherwise the message will simply be of the form {@code request_uri}. <p>The final message is composed of the
* inner part as described and the supplied prefix and suffix.
* Create a log message for the given request, prefix and suffix.
* <p>If {@code includeQueryString} is {@code true}, then the inner part
* of the log message will take the form {@code request_uri?query_string};
* otherwise the message will simply be of the form {@code request_uri}.
* <p>The final message is composed of the inner part as described and
* the supplied prefix and suffix.
*/
protected String createMessage(HttpServletRequest request, String prefix, String suffix) {
StringBuilder msg = new StringBuilder();
@@ -285,16 +286,16 @@ public abstract class AbstractRequestLoggingFilter extends OncePerRequestFilter
}
/**
* Concrete subclasses should implement this method to write a log message <i>before</i> the request is processed.
*
* Concrete subclasses should implement this method to write a log message
* <i>before</i> the request is processed.
* @param request current HTTP request
* @param message the message to log
*/
protected abstract void beforeRequest(HttpServletRequest request, String message);
/**
* Concrete subclasses should implement this method to write a log message <i>after</i> the request is processed.
*
* Concrete subclasses should implement this method to write a log message
* <i>after</i> the request is processed.
* @param request current HTTP request
* @param message the message to log
*/
@@ -303,7 +304,7 @@ public abstract class AbstractRequestLoggingFilter extends OncePerRequestFilter
private static class RequestCachingRequestWrapper extends HttpServletRequestWrapper {
private final ByteArrayOutputStream bos = new ByteArrayOutputStream();
private final ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
private final ServletInputStream inputStream;
@@ -316,19 +317,19 @@ public abstract class AbstractRequestLoggingFilter extends OncePerRequestFilter
@Override
public ServletInputStream getInputStream() throws IOException {
return inputStream;
return this.inputStream;
}
@Override
public String getCharacterEncoding() {
return super.getCharacterEncoding() != null ? super.getCharacterEncoding() :
WebUtils.DEFAULT_CHARACTER_ENCODING;
String enc = super.getCharacterEncoding();
return (enc != null ? enc : WebUtils.DEFAULT_CHARACTER_ENCODING);
}
@Override
public BufferedReader getReader() throws IOException {
if (this.reader == null) {
this.reader = new BufferedReader(new InputStreamReader(inputStream, getCharacterEncoding()));
this.reader = new BufferedReader(new InputStreamReader(this.inputStream, getCharacterEncoding()));
}
return this.reader;
}
@@ -337,17 +338,18 @@ public abstract class AbstractRequestLoggingFilter extends OncePerRequestFilter
return this.bos.toByteArray();
}
private class RequestCachingInputStream extends ServletInputStream {
private final ServletInputStream is;
private RequestCachingInputStream(ServletInputStream is) {
public RequestCachingInputStream(ServletInputStream is) {
this.is = is;
}
@Override
public int read() throws IOException {
int ch = is.read();
int ch = this.is.read();
if (ch != -1) {
bos.write(ch);
}

View File

@@ -29,8 +29,8 @@ import javax.servlet.http.HttpServletResponseWrapper;
import org.springframework.http.HttpMethod;
import org.springframework.util.Assert;
import org.springframework.util.ResizableByteArrayOutputStream;
import org.springframework.util.DigestUtils;
import org.springframework.util.ResizableByteArrayOutputStream;
import org.springframework.util.StreamUtils;
import org.springframework.web.util.WebUtils;
@@ -175,7 +175,7 @@ public class ShallowEtagHeaderFilter extends OncePerRequestFilter {
*/
private static class ShallowEtagResponseWrapper extends HttpServletResponseWrapper {
private final ResizableByteArrayOutputStream content = new ResizableByteArrayOutputStream();
private final ResizableByteArrayOutputStream content = new ResizableByteArrayOutputStream(1024);
private final ServletOutputStream outputStream = new ResponseServletOutputStream();
@@ -232,22 +232,22 @@ public class ShallowEtagHeaderFilter extends OncePerRequestFilter {
return this.writer;
}
@Override
public void resetBuffer() {
this.content.reset();
}
@Override
public void reset() {
super.reset();
resetBuffer();
}
private int getStatusCode() {
@Override
public void resetBuffer() {
this.content.reset();
}
public int getStatusCode() {
return this.statusCode;
}
private byte[] toByteArray() {
public byte[] toByteArray() {
return this.content.toByteArray();
}