Browse Source

Polish ContentCachingRequestWrapper

Issue: SPR-15762
pull/1687/merge
Brian Clozel 8 years ago
parent
commit
d00f6f09a5
  1. 71
      spring-web/src/main/java/org/springframework/web/util/ContentCachingRequestWrapper.java

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

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2017 the original author or authors. * Copyright 2002-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -214,69 +214,52 @@ public class ContentCachingRequestWrapper extends HttpServletRequestWrapper {
} }
@Override @Override
public int readLine(final byte[] b, final int off, final int len) throws IOException { public int read() throws IOException {
int count = is.readLine(b, off, len); int ch = this.is.read();
cache(b, off, count); if (ch != -1 && !this.overflow) {
return count; if (contentCacheLimit != null && cachedContent.size() == contentCacheLimit) {
}
private void cache(final byte[] b, final int off, final int count) {
if (contentCacheLimit != null && cachedContent.size() == contentCacheLimit) {
this.overflow = true;
handleContentOverflow(contentCacheLimit);
} else {
int sizeToCache = contentCacheLimit == null || count + cachedContent.size() < contentCacheLimit
? count
: contentCacheLimit - cachedContent.size();
cachedContent.write(b, off, sizeToCache);
if (sizeToCache < count) {
this.overflow = true; this.overflow = true;
handleContentOverflow(contentCacheLimit); handleContentOverflow(contentCacheLimit);
} }
else {
cachedContent.write(ch);
}
} }
return ch;
} }
@Override @Override
public int read(final byte[] b) throws IOException { public int read(byte[] b) throws IOException {
int count = super.read(b); int count = this.is.read(b);
writeToCache(b, 0, count);
return count;
}
private void writeToCache(final byte[] b, final int off, int count) {
if (!this.overflow && count > 0) { if (!this.overflow && count > 0) {
if (contentCacheLimit != null && cachedContent.size() == contentCacheLimit) { if (contentCacheLimit != null &&
count + cachedContent.size() > contentCacheLimit) {
this.overflow = true; this.overflow = true;
cachedContent.write(b, off, contentCacheLimit - cachedContent.size());
handleContentOverflow(contentCacheLimit); handleContentOverflow(contentCacheLimit);
} else { return;
int sizeToCache = contentCacheLimit == null || count + cachedContent.size() < contentCacheLimit
? count
: contentCacheLimit - cachedContent.size();
cachedContent.write(b, 0, sizeToCache);
if (sizeToCache < count) {
this.overflow = true;
handleContentOverflow(contentCacheLimit);
}
} }
cachedContent.write(b, off, count);
} }
return count;
} }
@Override @Override
public int read(final byte[] b, final int off, final int len) throws IOException { public int read(final byte[] b, final int off, final int len) throws IOException {
int count = is.read(b, off, len); int count = this.is.read(b, off, len);
cache(b, off, count); writeToCache(b, off, count);
return count; return count;
} }
@Override @Override
public int read() throws IOException { public int readLine(final byte[] b, final int off, final int len) throws IOException {
int ch = this.is.read(); int count = this.is.readLine(b, off, len);
if (ch != -1 && !this.overflow) { writeToCache(b, off, count);
if (contentCacheLimit != null && cachedContent.size() == contentCacheLimit) { return count;
this.overflow = true;
handleContentOverflow(contentCacheLimit);
}
else {
cachedContent.write(ch);
}
}
return ch;
} }
@Override @Override

Loading…
Cancel
Save