Skip to content

Commit

Permalink
FILEUPLOAD-315 - java8 improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
arturobernalg committed Apr 22, 2021
1 parent b825323 commit 60b7e11
Show file tree
Hide file tree
Showing 13 changed files with 22 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -389,12 +389,7 @@ public Map<String, List<FileItem>> parseParameterMap(final RequestContext ctx)

for (final FileItem fileItem : items) {
final String fieldName = fileItem.getFieldName();
List<FileItem> mappedItems = itemsMap.get(fieldName);

if (mappedItems == null) {
mappedItems = new ArrayList<>();
itemsMap.put(fieldName, mappedItems);
}
List<FileItem> mappedItems = itemsMap.computeIfAbsent(fieldName, k -> new ArrayList<>());

mappedItems.add(fileItem);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ public String readHeaders() throws FileUploadIOException, MalformedStreamExcepti
if (++size > HEADER_PART_SIZE_MAX) {
throw new MalformedStreamException(
format("Header section has more than %s bytes (maybe it is not properly terminated)",
Integer.valueOf(HEADER_PART_SIZE_MAX)));
HEADER_PART_SIZE_MAX));
}
if (b == HEADER_SEPARATOR[i]) {
i++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -607,8 +607,8 @@ private static String getUniqueId() {
@Override
public String toString() {
return format("name=%s, StoreLocation=%s, size=%s bytes, isFormField=%s, FieldName=%s",
getName(), getStoreLocation(), Long.valueOf(getSize()),
Boolean.valueOf(isFormField()), getFieldName());
getName(), getStoreLocation(), getSize(),
isFormField(), getFieldName());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ protected void init(final FileUploadBase fileUploadBase, final RequestContext pR
if (requestSize != -1 && requestSize > sizeMax) {
throw new SizeLimitExceededException(
format("the request was rejected because its size (%s) exceeds the configured maximum (%s)",
Long.valueOf(requestSize), Long.valueOf(sizeMax)),
requestSize, sizeMax),
requestSize, sizeMax);
}
// N.B. this is eventually closed in MultipartStream processing
Expand All @@ -164,7 +164,7 @@ protected void raiseError(final long pSizeMax, final long pCount)
throws IOException {
final FileUploadException ex = new SizeLimitExceededException(
format("the request was rejected because its size (%s) exceeds the configured maximum (%s)",
Long.valueOf(pCount), Long.valueOf(pSizeMax)),
pCount, pSizeMax),
pCount, pSizeMax);
throw new FileUploadIOException(ex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public FileItemStreamImpl(final FileItemIteratorImpl pFileItemIterator, final St
final FileSizeLimitExceededException e =
new FileSizeLimitExceededException(
format("The field %s exceeds its maximum permitted size of %s bytes.",
fieldName, Long.valueOf(fileSizeMax)),
fieldName, fileSizeMax),
pContentLength, fileSizeMax);
e.setFileName(pName);
e.setFieldName(pFieldName);
Expand All @@ -117,7 +117,7 @@ protected void raiseError(final long pSizeMax, final long pCount)
final FileSizeLimitExceededException e =
new FileSizeLimitExceededException(
format("The field %s exceeds its maximum permitted size of %s bytes.",
fieldName, Long.valueOf(pSizeMax)),
fieldName, pSizeMax),
pCount, pSizeMax);
e.setFieldName(fieldName);
e.setFileName(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public InputStream getInputStream() throws IOException {
@Override
public String toString() {
return format("ContentLength=%s, ContentType=%s",
Long.valueOf(this.contentLength()),
this.contentLength(),
this.getContentType());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public InputStream getInputStream() throws IOException {
@Override
public String toString() {
return format("ContentLength=%s, ContentType=%s",
Long.valueOf(this.contentLength()),
this.contentLength(),
this.getContentType());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public InputStream getInputStream() throws IOException {
@Override
public String toString() {
return format("ContentLength=%s, ContentType=%s",
Long.valueOf(this.contentLength()),
this.contentLength(),
this.getContentType());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,7 @@ public Iterator<String> getHeaders(final String name) {
*/
public synchronized void addHeader(final String name, final String value) {
final String nameLower = name.toLowerCase(Locale.ENGLISH);
List<String> headerValueList = headerNameToValueListMap.get(nameLower);
if (null == headerValueList) {
headerValueList = new ArrayList<>();
headerNameToValueListMap.put(nameLower, headerValueList);
}
List<String> headerValueList = headerNameToValueListMap.computeIfAbsent(nameLower, k -> new ArrayList<>());
headerValueList.add(value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ private MimeUtility() {
public static String decodeText(final String text) throws UnsupportedEncodingException {
// if the text contains any encoded tokens, those tokens will be marked with "=?". If the
// source string doesn't contain that sequent, no decoding is required.
if (text.indexOf(ENCODED_TOKEN_MARKER) < 0) {
if (!text.contains(ENCODED_TOKEN_MARKER)) {
return text;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ public void update(final long pBytesRead, final long pContentLength, final int p
assertTrue(pContentLength == -1 || pContentLength == expectedContentLength);
assertTrue(pItems >= 0 && pItems <= expectedItems);

assertTrue(bytesRead == null || pBytesRead >= bytesRead.longValue());
bytesRead = new Long(pBytesRead);
assertTrue(items == null || pItems >= items.intValue());
items = new Integer(pItems);
assertTrue(bytesRead == null || pBytesRead >= bytesRead);
bytesRead = pBytesRead;
assertTrue(items == null || pItems >= items);
items = pItems;
}

void checkFinished(){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,17 +264,17 @@ public void testInvalidFileNameException() throws Exception {
fail("Expected exception");
} catch (final InvalidFileNameException e) {
assertEquals(fileName, e.getName());
assertTrue(e.getMessage().indexOf(fileName) == -1);
assertTrue(e.getMessage().indexOf("foo.exe\\0.png") != -1);
assertTrue(!e.getMessage().contains(fileName));
assertTrue(e.getMessage().contains("foo.exe\\0.png"));
}

try {
final List<FileItem> fileItems = parseUpload(reqBytes);
fail("Expected exception");
} catch (final InvalidFileNameException e) {
assertEquals(fileName, e.getName());
assertTrue(e.getMessage().indexOf(fileName) == -1);
assertTrue(e.getMessage().indexOf("foo.exe\\0.png") != -1);
assertTrue(!e.getMessage().contains(fileName));
assertTrue(e.getMessage().contains("foo.exe\\0.png"));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,7 @@ public void decodeIso88591() throws Exception {

@Test
public void decodeInvalidEncoding() throws Exception {
assertThrows(UnsupportedEncodingException.class, () -> {
RFC2231Utility.decodeText("abc'en'hello");
});
assertThrows(UnsupportedEncodingException.class, () -> RFC2231Utility.decodeText("abc'en'hello"));
}

private static void assertEncoded(final String expected, final String encoded) throws Exception {
Expand Down

0 comments on commit 60b7e11

Please sign in to comment.