Skip to content

Commit

Permalink
Consistently avoid close() call on Servlet OutputStream
Browse files Browse the repository at this point in the history
Issue: SPR-11413
  • Loading branch information
jhoeller committed Feb 11, 2014
1 parent 648245b commit 5f1592a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -21,7 +21,6 @@
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
Expand All @@ -31,7 +30,7 @@

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;

/**
Expand All @@ -48,9 +47,9 @@
*/
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";


/**
Expand Down Expand Up @@ -117,7 +116,7 @@ private void updateResponse(HttpServletRequest request, HttpServletResponse resp
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());
}
}

Expand Down Expand Up @@ -166,7 +165,7 @@ private static class ShallowEtagResponseWrapper extends HttpServletResponseWrapp

private int statusCode = HttpServletResponse.SC_OK;

private ShallowEtagResponseWrapper(HttpServletResponse response) {
public ShallowEtagResponseWrapper(HttpServletResponse response) {
super(response);
}

Expand Down Expand Up @@ -233,6 +232,7 @@ private byte[] toByteArray() {
return this.content.toByteArray();
}


private class ResponseServletOutputStream extends ServletOutputStream {

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


private class ResponsePrintWriter extends PrintWriter {

private ResponsePrintWriter(String characterEncoding) throws UnsupportedEncodingException {
public ResponsePrintWriter(String characterEncoding) throws UnsupportedEncodingException {
super(new OutputStreamWriter(content, characterEncoding));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -19,7 +19,6 @@
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import javax.activation.FileTypeMap;
import javax.activation.MimetypesFileTypeMap;
import javax.servlet.ServletException;
Expand All @@ -28,14 +27,15 @@

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;
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;
Expand Down Expand Up @@ -251,7 +251,7 @@ protected void setHeaders(HttpServletResponse response, Resource resource, Media
* @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());
}


Expand Down

0 comments on commit 5f1592a

Please sign in to comment.