From 99a3e9cacbbd27615f941b1f9041bb571656ae4c Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Wed, 2 Apr 2014 20:57:09 +0200 Subject: [PATCH] Revised ByteArrayOutputStream handling in MarshallingView and co Issue: SPR-11646 (cherry picked from commit 8006696) --- .../web/filter/AbstractRequestLoggingFilter.java | 10 +++++----- .../web/servlet/view/xml/MarshallingView.java | 10 ++++------ 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/web/filter/AbstractRequestLoggingFilter.java b/spring-web/src/main/java/org/springframework/web/filter/AbstractRequestLoggingFilter.java index b68f10e8d75..5e4eea21d86 100644 --- a/spring-web/src/main/java/org/springframework/web/filter/AbstractRequestLoggingFilter.java +++ b/spring-web/src/main/java/org/springframework/web/filter/AbstractRequestLoggingFilter.java @@ -267,7 +267,7 @@ public abstract class AbstractRequestLoggingFilter extends OncePerRequestFilter } if (isIncludePayload() && request instanceof RequestCachingRequestWrapper) { RequestCachingRequestWrapper wrapper = (RequestCachingRequestWrapper) request; - byte[] buf = wrapper.toByteArray(); + byte[] buf = wrapper.getContentAsByteArray(); if (buf.length > 0) { int length = Math.min(buf.length, getMaxPayloadLength()); String payload; @@ -304,7 +304,7 @@ public abstract class AbstractRequestLoggingFilter extends OncePerRequestFilter private static class RequestCachingRequestWrapper extends HttpServletRequestWrapper { - private final ByteArrayOutputStream bos = new ByteArrayOutputStream(1024); + private final ByteArrayOutputStream cachedContent = new ByteArrayOutputStream(1024); private final ServletInputStream inputStream; @@ -334,8 +334,8 @@ public abstract class AbstractRequestLoggingFilter extends OncePerRequestFilter return this.reader; } - private byte[] toByteArray() { - return this.bos.toByteArray(); + private byte[] getContentAsByteArray() { + return this.cachedContent.toByteArray(); } @@ -351,7 +351,7 @@ public abstract class AbstractRequestLoggingFilter extends OncePerRequestFilter public int read() throws IOException { int ch = this.is.read(); if (ch != -1) { - bos.write(ch); + cachedContent.write(ch); } return ch; } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/view/xml/MarshallingView.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/view/xml/MarshallingView.java index 748ea07d344..64c81b52a4c 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/view/xml/MarshallingView.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/view/xml/MarshallingView.java @@ -25,7 +25,6 @@ import javax.xml.transform.stream.StreamResult; import org.springframework.oxm.Marshaller; import org.springframework.util.Assert; -import org.springframework.util.StreamUtils; import org.springframework.validation.BindingResult; import org.springframework.web.servlet.View; import org.springframework.web.servlet.view.AbstractView; @@ -105,13 +104,12 @@ public class MarshallingView extends AbstractView { if (toBeMarshalled == null) { throw new ServletException("Unable to locate object to be marshalled in model: " + model); } - ByteArrayOutputStream bos = new ByteArrayOutputStream(1024); - this.marshaller.marshal(toBeMarshalled, new StreamResult(bos)); + ByteArrayOutputStream baos = new ByteArrayOutputStream(1024); + this.marshaller.marshal(toBeMarshalled, new StreamResult(baos)); setResponseContentType(request, response); - response.setContentLength(bos.size()); - - StreamUtils.copy(bos.toByteArray(), response.getOutputStream()); + response.setContentLength(baos.size()); + baos.writeTo(response.getOutputStream()); } /**