Polish ContentCachingResponseWrapper[Tests]

This commit is contained in:
Sam Brannen
2024-02-24 13:34:48 +01:00
parent 3cc64968b9
commit 8787381892
2 changed files with 21 additions and 18 deletions

View File

@@ -38,7 +38,7 @@ import org.springframework.util.FastByteArrayOutputStream;
/**
* {@link jakarta.servlet.http.HttpServletResponse} wrapper that caches all content written to
* the {@linkplain #getOutputStream() output stream} and {@linkplain #getWriter() writer},
* and allows this content to be retrieved via a {@link #getContentAsByteArray() byte array}.
* and allows this content to be retrieved via a {@linkplain #getContentAsByteArray() byte array}.
*
* <p>Used e.g. by {@link org.springframework.web.filter.ShallowEtagHeaderFilter}.
*
@@ -120,9 +120,16 @@ public class ContentCachingResponseWrapper extends HttpServletResponseWrapper {
return this.writer;
}
/**
* This method neither flushes content to the client nor commits the underlying
* response, since the content has not yet been copied to the response.
* <p>Invoke {@link #copyBodyToResponse()} to copy the cached body content to
* the wrapped response object and flush its buffer.
* @see jakarta.servlet.ServletResponseWrapper#flushBuffer()
*/
@Override
public void flushBuffer() throws IOException {
// do not flush the underlying response as the content has not been copied to it yet
// no-op
}
@Override
@@ -139,15 +146,11 @@ public class ContentCachingResponseWrapper extends HttpServletResponseWrapper {
throw new IllegalArgumentException("Content-Length exceeds ContentCachingResponseWrapper's maximum (" +
Integer.MAX_VALUE + "): " + len);
}
int lenInt = (int) len;
if (lenInt > this.content.size()) {
this.content.resize(lenInt);
}
this.contentLength = lenInt;
setContentLength((int) len);
}
@Override
public void setContentType(String type) {
public void setContentType(@Nullable String type) {
this.contentType = type;
}