Skip to content

Commit d00f6f0

Browse files
committed
Polish ContentCachingRequestWrapper
Issue: SPR-15762
1 parent 4d0800f commit d00f6f0

File tree

1 file changed

+27
-44
lines changed

1 file changed

+27
-44
lines changed

spring-web/src/main/java/org/springframework/web/util/ContentCachingRequestWrapper.java

Lines changed: 27 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -214,69 +214,52 @@ public ContentCachingInputStream(ServletInputStream is) {
214214
}
215215

216216
@Override
217-
public int readLine(final byte[] b, final int off, final int len) throws IOException {
218-
int count = is.readLine(b, off, len);
219-
cache(b, off, count);
220-
return count;
221-
}
222-
223-
private void cache(final byte[] b, final int off, final int count) {
224-
if (contentCacheLimit != null && cachedContent.size() == contentCacheLimit) {
225-
this.overflow = true;
226-
handleContentOverflow(contentCacheLimit);
227-
} else {
228-
int sizeToCache = contentCacheLimit == null || count + cachedContent.size() < contentCacheLimit
229-
? count
230-
: contentCacheLimit - cachedContent.size();
231-
cachedContent.write(b, off, sizeToCache);
232-
if (sizeToCache < count) {
217+
public int read() throws IOException {
218+
int ch = this.is.read();
219+
if (ch != -1 && !this.overflow) {
220+
if (contentCacheLimit != null && cachedContent.size() == contentCacheLimit) {
233221
this.overflow = true;
234222
handleContentOverflow(contentCacheLimit);
235223
}
224+
else {
225+
cachedContent.write(ch);
226+
}
236227
}
228+
return ch;
237229
}
238230

239231
@Override
240-
public int read(final byte[] b) throws IOException {
241-
int count = super.read(b);
232+
public int read(byte[] b) throws IOException {
233+
int count = this.is.read(b);
234+
writeToCache(b, 0, count);
235+
return count;
236+
}
237+
238+
private void writeToCache(final byte[] b, final int off, int count) {
242239
if (!this.overflow && count > 0) {
243-
if (contentCacheLimit != null && cachedContent.size() == contentCacheLimit) {
240+
if (contentCacheLimit != null &&
241+
count + cachedContent.size() > contentCacheLimit) {
244242
this.overflow = true;
243+
cachedContent.write(b, off, contentCacheLimit - cachedContent.size());
245244
handleContentOverflow(contentCacheLimit);
246-
} else {
247-
int sizeToCache = contentCacheLimit == null || count + cachedContent.size() < contentCacheLimit
248-
? count
249-
: contentCacheLimit - cachedContent.size();
250-
cachedContent.write(b, 0, sizeToCache);
251-
if (sizeToCache < count) {
252-
this.overflow = true;
253-
handleContentOverflow(contentCacheLimit);
254-
}
245+
return;
255246
}
247+
cachedContent.write(b, off, count);
256248
}
257-
return count;
258249
}
259250

260251
@Override
261252
public int read(final byte[] b, final int off, final int len) throws IOException {
262-
int count = is.read(b, off, len);
263-
cache(b, off, count);
253+
int count = this.is.read(b, off, len);
254+
writeToCache(b, off, count);
264255
return count;
265256
}
266257

267258
@Override
268-
public int read() throws IOException {
269-
int ch = this.is.read();
270-
if (ch != -1 && !this.overflow) {
271-
if (contentCacheLimit != null && cachedContent.size() == contentCacheLimit) {
272-
this.overflow = true;
273-
handleContentOverflow(contentCacheLimit);
274-
}
275-
else {
276-
cachedContent.write(ch);
277-
}
278-
}
279-
return ch;
259+
public int readLine(final byte[] b, final int off, final int len) throws IOException {
260+
int count = this.is.readLine(b, off, len);
261+
writeToCache(b, off, count);
262+
return count;
280263
}
281264

282265
@Override

0 commit comments

Comments
 (0)