ShallowEtagHeaderFilter use specified contentlength

Prior to this commit, the ShallowEtagHeaderFilter did not use the
content length given by the content generator to set the
ByteArrayOutputStream's buffer size.
This can lead to performance issues for large content since the buffer
grows as the content is being written.

This commit adds a new ByteArrayOutputStream variant called
ResizableByteArrayOutputStream. This implementation has public methods
for modifying the internal buffer size and does not synchronize on
buffer access.
This commit also make use of this new variant in
ShallowEtagHeaderFilter.

Issue: SPR-8271
This commit is contained in:
Brian Clozel
2014-03-21 16:46:12 +01:00
parent 4e24e079a8
commit ae012ae6e2
3 changed files with 246 additions and 2 deletions

View File

@@ -16,7 +16,6 @@
package org.springframework.web.filter;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
@@ -30,6 +29,7 @@ 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.StreamUtils;
import org.springframework.web.util.WebUtils;
@@ -175,7 +175,7 @@ public class ShallowEtagHeaderFilter extends OncePerRequestFilter {
*/
private static class ShallowEtagResponseWrapper extends HttpServletResponseWrapper {
private final ByteArrayOutputStream content = new ByteArrayOutputStream();
private final ResizableByteArrayOutputStream content = new ResizableByteArrayOutputStream();
private final ServletOutputStream outputStream = new ResponseServletOutputStream();
@@ -214,6 +214,7 @@ public class ShallowEtagHeaderFilter extends OncePerRequestFilter {
@Override
public void setContentLength(int len) {
this.content.resize(len);
}
@Override