-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Replace commons-io:commons-io #2632
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
9868da8
Replace commons-io:commons-io
dbede0d
Replace commons-io:commons-io
a92217e
Replace commons-io:commons-io
6e944b7
Replace commons-io:commons-io
6bee13d
Replace commons-io:commons-io
294888f
Replace commons-io:commons-io
d4ab4c8
Merge branch 'master' into commons_io
44f3c99
Merge branch 'master' into commons_io
e99f9fc
Merge branch 'master' into commons_io
ff68ff3
Merge branch 'master' into commons_io
b9b3403
Update
7d2e4d6
Merge branch 'master' into commons_io
3345139
Merge branch 'master' into commons_io
a914add
Update
8fc4014
Merge branch 'master' into commons_io
96b0a43
Merge branch 'master' into commons_io
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/main/java/com/github/tomakehurst/wiremock/common/StreamSources.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/* | ||
* Copyright (C) 2022-2023 Thomas Akehurst | ||
* Copyright (C) 2022-2024 Thomas Akehurst | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
|
@@ -47,7 +47,6 @@ | |
import org.apache.commons.fileupload.util.FileItemHeadersImpl; | ||
import org.apache.commons.fileupload.util.LimitedInputStream; | ||
import org.apache.commons.fileupload.util.Streams; | ||
import org.apache.commons.io.IOUtils; | ||
|
||
/** | ||
* The implementation is largely ported from {@link org.apache.commons.fileupload.FileUpload} and | ||
|
@@ -103,7 +102,7 @@ protected FileItemFactory getFileItemFactory() { | |
* @throws FileUploadException if there are problems reading/parsing the request or storing files. | ||
*/ | ||
public List<FileItem> parseRequest(RequestContext ctx) throws FileUploadException { | ||
List<FileItem> items = new ArrayList<FileItem>(); | ||
List<FileItem> items = new ArrayList<>(); | ||
boolean successful = false; | ||
try { | ||
FileItemIterator iter = getItemIterator(ctx); | ||
|
@@ -580,7 +579,7 @@ public void setHeaders(FileItemHeaders pHeaders) { | |
: contentLengthInt; | ||
// CHECKSTYLE:ON | ||
|
||
InputStream input; // N.B. this is eventually closed in MultipartStream processing | ||
; // N.B. this is eventually closed in MultipartStream processing | ||
if (sizeMax >= 0) { | ||
if (requestSize != -1 && requestSize > sizeMax) { | ||
throw new SizeLimitExceededException( | ||
|
@@ -590,50 +589,49 @@ public void setHeaders(FileItemHeaders pHeaders) { | |
requestSize, | ||
sizeMax); | ||
} | ||
// N.B. this is eventually closed in MultipartStream processing | ||
input = | ||
new LimitedInputStream(ctx.getInputStream(), sizeMax) { | ||
@Override | ||
protected void raiseError(long pSizeMax, long pCount) throws IOException { | ||
FileUploadException ex = | ||
new SizeLimitExceededException( | ||
format( | ||
"the request was rejected because its size (%s) exceeds the configured maximum (%s)", | ||
pCount, pSizeMax), | ||
pCount, | ||
pSizeMax); | ||
throw new FileUploadIOException(ex); | ||
} | ||
}; | ||
} else { | ||
input = ctx.getInputStream(); | ||
} | ||
|
||
String charEncoding = headerEncoding; | ||
if (charEncoding == null) { | ||
charEncoding = ctx.getCharacterEncoding(); | ||
} | ||
try (InputStream input = | ||
sizeMax >= 0 | ||
? new LimitedInputStream(ctx.getInputStream(), sizeMax) { | ||
@Override | ||
protected void raiseError(long pSizeMax, long pCount) throws IOException { | ||
FileUploadException ex = | ||
new SizeLimitExceededException( | ||
format( | ||
"the request was rejected because its size (%s) exceeds the configured maximum (%s)", | ||
pCount, pSizeMax), | ||
pCount, | ||
pSizeMax); | ||
throw new FileUploadIOException(ex); | ||
} | ||
} | ||
: ctx.getInputStream()) { | ||
Comment on lines
+594
to
+609
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice, try with resources is better than closing the stream and then throwing. |
||
String charEncoding = headerEncoding; | ||
if (charEncoding == null) { | ||
charEncoding = ctx.getCharacterEncoding(); | ||
} | ||
|
||
boundary = getBoundary(contentType); | ||
if (boundary == null) { | ||
IOUtils.closeQuietly(input); // avoid possible resource leak | ||
throw new FileUploadException( | ||
"the request was rejected because no multipart boundary was found"); | ||
} | ||
boundary = getBoundary(contentType); | ||
if (boundary == null) { | ||
throw new FileUploadException( | ||
"the request was rejected because no multipart boundary was found"); | ||
} | ||
|
||
try { | ||
multi = new MultipartStream(input, boundary, 4096, null); | ||
} catch (IllegalArgumentException iae) { | ||
IOUtils.closeQuietly(input); // avoid possible resource leak | ||
throw new InvalidContentTypeException( | ||
format( | ||
"The boundary specified in the %s header is too long", FileUploadBase.CONTENT_TYPE), | ||
iae); | ||
} | ||
multi.setHeaderEncoding(charEncoding); | ||
try { | ||
multi = new MultipartStream(input, boundary, 4096, null); | ||
} catch (IllegalArgumentException iae) { | ||
throw new InvalidContentTypeException( | ||
format( | ||
"The boundary specified in the %s header is too long", | ||
FileUploadBase.CONTENT_TYPE), | ||
iae); | ||
} | ||
multi.setHeaderEncoding(charEncoding); | ||
|
||
skipPreamble = true; | ||
findNextItem(); | ||
skipPreamble = true; | ||
findNextItem(); | ||
} | ||
} | ||
|
||
/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have you tested the performance impact of this change?
From the docs of
org.apache.commons.io.output.StringBuilderWriter
:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did a simple test
`package org.example;
import org.apache.commons.io.output.StringBuilderWriter;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.infra.Blackhole;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.concurrent.TimeUnit;
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@fork(value = 1, warmups = 1)
@State(Scope.Benchmark)
public class Benchmark {
}`
I got very close results
(INPUT) Mode Cnt Score Error Units
jdkStringWriter
thrpt 5 316,929 ± 2,652 ops/ms
commonsIOStringBuilderWriter
thrpt 5 319,308 ± 0,779 ops/ms
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, I've just tested it in context and there's no noticeable difference:
Pre change:
Post change: