Skip to content

Commit 85e336e

Browse files
committed
Consistently avoid close() call on Servlet OutputStream
Issue: SPR-11413 (cherry picked from commit 5f1592a)
1 parent b223e6e commit 85e336e

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

spring-web/src/main/java/org/springframework/web/filter/ShallowEtagHeaderFilter.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,7 +21,6 @@
2121
import java.io.OutputStreamWriter;
2222
import java.io.PrintWriter;
2323
import java.io.UnsupportedEncodingException;
24-
2524
import javax.servlet.FilterChain;
2625
import javax.servlet.ServletException;
2726
import javax.servlet.ServletOutputStream;
@@ -31,7 +30,7 @@
3130

3231
import org.springframework.util.Assert;
3332
import org.springframework.util.DigestUtils;
34-
import org.springframework.util.FileCopyUtils;
33+
import org.springframework.util.StreamUtils;
3534
import org.springframework.web.util.WebUtils;
3635

3736
/**
@@ -48,9 +47,9 @@
4847
*/
4948
public class ShallowEtagHeaderFilter extends OncePerRequestFilter {
5049

51-
private static String HEADER_ETAG = "ETag";
50+
private static final String HEADER_ETAG = "ETag";
5251

53-
private static String HEADER_IF_NONE_MATCH = "If-None-Match";
52+
private static final String HEADER_IF_NONE_MATCH = "If-None-Match";
5453

5554

5655
/**
@@ -117,7 +116,7 @@ private void updateResponse(HttpServletRequest request, HttpServletResponse resp
117116
private void copyBodyToResponse(byte[] body, HttpServletResponse response) throws IOException {
118117
if (body.length > 0) {
119118
response.setContentLength(body.length);
120-
FileCopyUtils.copy(body, response.getOutputStream());
119+
StreamUtils.copy(body, response.getOutputStream());
121120
}
122121
}
123122

@@ -166,7 +165,7 @@ private static class ShallowEtagResponseWrapper extends HttpServletResponseWrapp
166165

167166
private int statusCode = HttpServletResponse.SC_OK;
168167

169-
private ShallowEtagResponseWrapper(HttpServletResponse response) {
168+
public ShallowEtagResponseWrapper(HttpServletResponse response) {
170169
super(response);
171170
}
172171

@@ -233,6 +232,7 @@ private byte[] toByteArray() {
233232
return this.content.toByteArray();
234233
}
235234

235+
236236
private class ResponseServletOutputStream extends ServletOutputStream {
237237

238238
@Override
@@ -246,9 +246,10 @@ public void write(byte[] b, int off, int len) throws IOException {
246246
}
247247
}
248248

249+
249250
private class ResponsePrintWriter extends PrintWriter {
250251

251-
private ResponsePrintWriter(String characterEncoding) throws UnsupportedEncodingException {
252+
public ResponsePrintWriter(String characterEncoding) throws UnsupportedEncodingException {
252253
super(new OutputStreamWriter(content, characterEncoding));
253254
}
254255

spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,7 +19,6 @@
1919
import java.io.IOException;
2020
import java.io.InputStream;
2121
import java.util.List;
22-
2322
import javax.activation.FileTypeMap;
2423
import javax.activation.MimetypesFileTypeMap;
2524
import javax.servlet.ServletException;
@@ -28,14 +27,15 @@
2827

2928
import org.apache.commons.logging.Log;
3029
import org.apache.commons.logging.LogFactory;
30+
3131
import org.springframework.beans.factory.InitializingBean;
3232
import org.springframework.core.io.ClassPathResource;
3333
import org.springframework.core.io.Resource;
3434
import org.springframework.http.MediaType;
3535
import org.springframework.util.Assert;
3636
import org.springframework.util.ClassUtils;
3737
import org.springframework.util.CollectionUtils;
38-
import org.springframework.util.FileCopyUtils;
38+
import org.springframework.util.StreamUtils;
3939
import org.springframework.util.StringUtils;
4040
import org.springframework.web.HttpRequestHandler;
4141
import org.springframework.web.context.request.ServletWebRequest;
@@ -249,7 +249,7 @@ protected void setHeaders(HttpServletResponse response, Resource resource, Media
249249
* @throws IOException in case of errors while writing the content
250250
*/
251251
protected void writeContent(HttpServletResponse response, Resource resource) throws IOException {
252-
FileCopyUtils.copy(resource.getInputStream(), response.getOutputStream());
252+
StreamUtils.copy(resource.getInputStream(), response.getOutputStream());
253253
}
254254

255255

0 commit comments

Comments
 (0)