Revised ByteArrayOutputStream handling in MarshallingView and co

Issue: SPR-11646
This commit is contained in:
Juergen Hoeller
2014-04-02 20:57:09 +02:00
parent 3a96f16654
commit 8006696613
3 changed files with 10 additions and 12 deletions

View File

@@ -29,7 +29,7 @@ import java.io.ByteArrayOutputStream;
* *
* @author Brian Clozel * @author Brian Clozel
* @author Juergen Hoeller * @author Juergen Hoeller
* @since 4.0 * @since 4.0.3
*/ */
public class ResizableByteArrayOutputStream extends ByteArrayOutputStream { public class ResizableByteArrayOutputStream extends ByteArrayOutputStream {

View File

@@ -267,7 +267,7 @@ public abstract class AbstractRequestLoggingFilter extends OncePerRequestFilter
} }
if (isIncludePayload() && request instanceof RequestCachingRequestWrapper) { if (isIncludePayload() && request instanceof RequestCachingRequestWrapper) {
RequestCachingRequestWrapper wrapper = (RequestCachingRequestWrapper) request; RequestCachingRequestWrapper wrapper = (RequestCachingRequestWrapper) request;
byte[] buf = wrapper.toByteArray(); byte[] buf = wrapper.getContentAsByteArray();
if (buf.length > 0) { if (buf.length > 0) {
int length = Math.min(buf.length, getMaxPayloadLength()); int length = Math.min(buf.length, getMaxPayloadLength());
String payload; String payload;
@@ -304,7 +304,7 @@ public abstract class AbstractRequestLoggingFilter extends OncePerRequestFilter
private static class RequestCachingRequestWrapper extends HttpServletRequestWrapper { private static class RequestCachingRequestWrapper extends HttpServletRequestWrapper {
private final ByteArrayOutputStream bos = new ByteArrayOutputStream(1024); private final ByteArrayOutputStream cachedContent = new ByteArrayOutputStream(1024);
private final ServletInputStream inputStream; private final ServletInputStream inputStream;
@@ -334,8 +334,8 @@ public abstract class AbstractRequestLoggingFilter extends OncePerRequestFilter
return this.reader; return this.reader;
} }
private byte[] toByteArray() { private byte[] getContentAsByteArray() {
return this.bos.toByteArray(); return this.cachedContent.toByteArray();
} }
@@ -351,7 +351,7 @@ public abstract class AbstractRequestLoggingFilter extends OncePerRequestFilter
public int read() throws IOException { public int read() throws IOException {
int ch = this.is.read(); int ch = this.is.read();
if (ch != -1) { if (ch != -1) {
bos.write(ch); cachedContent.write(ch);
} }
return ch; return ch;
} }

View File

@@ -24,7 +24,6 @@ import javax.xml.transform.stream.StreamResult;
import org.springframework.oxm.Marshaller; import org.springframework.oxm.Marshaller;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.StreamUtils;
import org.springframework.validation.BindingResult; import org.springframework.validation.BindingResult;
import org.springframework.web.servlet.View; import org.springframework.web.servlet.View;
import org.springframework.web.servlet.view.AbstractView; import org.springframework.web.servlet.view.AbstractView;
@@ -104,13 +103,12 @@ public class MarshallingView extends AbstractView {
if (toBeMarshalled == null) { if (toBeMarshalled == null) {
throw new IllegalStateException("Unable to locate object to be marshalled in model: " + model); throw new IllegalStateException("Unable to locate object to be marshalled in model: " + model);
} }
ByteArrayOutputStream bos = new ByteArrayOutputStream(1024); ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
this.marshaller.marshal(toBeMarshalled, new StreamResult(bos)); this.marshaller.marshal(toBeMarshalled, new StreamResult(baos));
setResponseContentType(request, response); setResponseContentType(request, response);
response.setContentLength(bos.size()); response.setContentLength(baos.size());
baos.writeTo(response.getOutputStream());
StreamUtils.copy(bos.toByteArray(), response.getOutputStream());
} }
/** /**