Consistently avoid close() call on Servlet OutputStream

Issue: SPR-11413
This commit is contained in:
Juergen Hoeller 2014-02-11 23:42:37 +01:00
parent 648245b200
commit 5f1592a61a
2 changed files with 13 additions and 12 deletions

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.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
@ -31,7 +30,7 @@ import javax.servlet.http.HttpServletResponseWrapper;
import org.springframework.util.Assert;
import org.springframework.util.DigestUtils;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.StreamUtils;
import org.springframework.web.util.WebUtils;
/**
@ -48,9 +47,9 @@ import org.springframework.web.util.WebUtils;
*/
public class ShallowEtagHeaderFilter extends OncePerRequestFilter {
private static String HEADER_ETAG = "ETag";
private static final String HEADER_ETAG = "ETag";
private static String HEADER_IF_NONE_MATCH = "If-None-Match";
private static final String HEADER_IF_NONE_MATCH = "If-None-Match";
/**
@ -117,7 +116,7 @@ public class ShallowEtagHeaderFilter extends OncePerRequestFilter {
private void copyBodyToResponse(byte[] body, HttpServletResponse response) throws IOException {
if (body.length > 0) {
response.setContentLength(body.length);
FileCopyUtils.copy(body, response.getOutputStream());
StreamUtils.copy(body, response.getOutputStream());
}
}
@ -166,7 +165,7 @@ public class ShallowEtagHeaderFilter extends OncePerRequestFilter {
private int statusCode = HttpServletResponse.SC_OK;
private ShallowEtagResponseWrapper(HttpServletResponse response) {
public ShallowEtagResponseWrapper(HttpServletResponse response) {
super(response);
}
@ -233,6 +232,7 @@ public class ShallowEtagHeaderFilter extends OncePerRequestFilter {
return this.content.toByteArray();
}
private class ResponseServletOutputStream extends ServletOutputStream {
@Override
@ -246,9 +246,10 @@ public class ShallowEtagHeaderFilter extends OncePerRequestFilter {
}
}
private class ResponsePrintWriter extends PrintWriter {
private ResponsePrintWriter(String characterEncoding) throws UnsupportedEncodingException {
public ResponsePrintWriter(String characterEncoding) throws UnsupportedEncodingException {
super(new OutputStreamWriter(content, characterEncoding));
}

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.
@ -19,7 +19,6 @@ package org.springframework.web.servlet.resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import javax.activation.FileTypeMap;
import javax.activation.MimetypesFileTypeMap;
import javax.servlet.ServletException;
@ -28,6 +27,7 @@ import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
@ -35,7 +35,7 @@ import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.StreamUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.context.request.ServletWebRequest;
@ -251,7 +251,7 @@ public class ResourceHttpRequestHandler extends WebContentGenerator implements H
* @throws IOException in case of errors while writing the content
*/
protected void writeContent(HttpServletResponse response, Resource resource) throws IOException {
FileCopyUtils.copy(resource.getInputStream(), response.getOutputStream());
StreamUtils.copy(resource.getInputStream(), response.getOutputStream());
}