|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2017 the original author or authors. |
| 2 | + * Copyright 2002-2018 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
@@ -214,69 +214,52 @@ public ContentCachingInputStream(ServletInputStream is) {
|
214 | 214 | }
|
215 | 215 |
|
216 | 216 | @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) { |
233 | 221 | this.overflow = true;
|
234 | 222 | handleContentOverflow(contentCacheLimit);
|
235 | 223 | }
|
| 224 | + else { |
| 225 | + cachedContent.write(ch); |
| 226 | + } |
236 | 227 | }
|
| 228 | + return ch; |
237 | 229 | }
|
238 | 230 |
|
239 | 231 | @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) { |
242 | 239 | if (!this.overflow && count > 0) {
|
243 |
| - if (contentCacheLimit != null && cachedContent.size() == contentCacheLimit) { |
| 240 | + if (contentCacheLimit != null && |
| 241 | + count + cachedContent.size() > contentCacheLimit) { |
244 | 242 | this.overflow = true;
|
| 243 | + cachedContent.write(b, off, contentCacheLimit - cachedContent.size()); |
245 | 244 | 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; |
255 | 246 | }
|
| 247 | + cachedContent.write(b, off, count); |
256 | 248 | }
|
257 |
| - return count; |
258 | 249 | }
|
259 | 250 |
|
260 | 251 | @Override
|
261 | 252 | 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); |
264 | 255 | return count;
|
265 | 256 | }
|
266 | 257 |
|
267 | 258 | @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; |
280 | 263 | }
|
281 | 264 |
|
282 | 265 | @Override
|
|
0 commit comments