Skip to content

Commit

Permalink
Merge 5a1f402 into 80385a1
Browse files Browse the repository at this point in the history
  • Loading branch information
arturobernalg committed Dec 27, 2020
2 parents 80385a1 + 5a1f402 commit 09e0517
Show file tree
Hide file tree
Showing 20 changed files with 61 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public interface FileItemStream extends FileItemHeadersSupport {
* {@link java.util.Iterator#hasNext()} has been invoked on the
* iterator, which created the {@link FileItemStream}.
*/
public static class ItemSkippedException extends IOException {
class ItemSkippedException extends IOException {

/**
* The exceptions serial version UID, which is being used
Expand Down
15 changes: 3 additions & 12 deletions src/main/java/org/apache/commons/fileupload2/FileUploadBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import static java.lang.String.format;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
Expand Down 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 Expand Up @@ -423,11 +418,7 @@ public byte[] getBoundary(final String contentType) {
return null;
}
byte[] boundary;
try {
boundary = boundaryStr.getBytes("ISO-8859-1");
} catch (final UnsupportedEncodingException e) {
boundary = boundaryStr.getBytes(); // Intentionally falls back to default charset
}
boundary = boundaryStr.getBytes(StandardCharsets.ISO_8859_1);
return boundary;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ public byte readByte() throws IOException {
public boolean readBoundary()
throws FileUploadIOException, MalformedStreamException {
final byte[] marker = new byte[2];
boolean nextChunk = false;
final boolean nextChunk;

head += boundaryLength;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ protected void init(final FileUploadBase fileUploadBase, final RequestContext pR
: contentLengthInt;
// CHECKSTYLE:ON

InputStream input; // N.B. this is eventually closed in MultipartStream processing
final InputStream input; // N.B. this is eventually closed in MultipartStream processing
if (sizeMax >= 0) {
if (requestSize != -1 && requestSize > sizeMax) {
throw new SizeLimitExceededException(
Expand Down Expand Up @@ -218,7 +218,7 @@ private boolean findNextItem() throws FileUploadException, IOException {
}
final MultipartStream multi = getMultiPartStream();
for (;;) {
boolean nextPart;
final boolean nextPart;
if (skipPreamble) {
nextPart = multi.skipPreamble();
} else {
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 @@ -19,6 +19,7 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
Expand Down Expand Up @@ -239,7 +240,7 @@ private static String decodeWord(final String word) throws ParseException, Unsup
// the decoder writes directly to an output stream.
final ByteArrayOutputStream out = new ByteArrayOutputStream(encodedText.length());

final byte[] encodedData = encodedText.getBytes(US_ASCII_CHARSET);
final byte[] encodedData = encodedText.getBytes(StandardCharsets.US_ASCII);

// Base64 encoded?
if (encoding.equals(BASE64_ENCODING_MARKER)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,6 @@
import java.util.Arrays;
import org.apache.commons.io.FileUtils;

import org.apache.commons.fileupload2.DefaultFileItem;
import org.apache.commons.fileupload2.DefaultFileItemFactory;
import org.apache.commons.fileupload2.FileItem;
import org.apache.commons.fileupload2.FileItemFactory;
import org.apache.commons.io.FileUtils;

import org.junit.jupiter.api.Test;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static org.junit.jupiter.api.Assertions.*;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
Expand Down Expand Up @@ -86,7 +87,7 @@ public void testMoveFile() throws Exception {
"This is the content of the file\n" +
"\r\n" +
"-----1234--\r\n";
final byte[] contentBytes = content.getBytes("US-ASCII");
final byte[] contentBytes = content.getBytes(StandardCharsets.US_ASCII);
final HttpServletRequest request = new MockHttpServletRequest(contentBytes, Constants.CONTENT_TYPE);
final List<FileItem> items = myUpload.parseRequest(request);
assertNotNull(items);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.stream.Stream;

Expand Down Expand Up @@ -223,7 +224,7 @@ public void testFILEUPLOAD62(final FileUpload upload) throws Exception {
"...contents of file2.gif...\r\n" +
"--BbC04y--\r\n" +
"--AaB03x--";
final List<FileItem> fileItems = Util.parseUpload(upload, request.getBytes("US-ASCII"), contentType);
final List<FileItem> fileItems = Util.parseUpload(upload, request.getBytes(StandardCharsets.US_ASCII), contentType);
assertEquals(3, fileItems.size());
final FileItem item0 = fileItems.get(0);
assertEquals("field1", item0.getFieldName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ public void setCharacterEncoding(final String arg0)
*/
@Override
public int getContentLength() {
int iLength = 0;
final int iLength;

if (null == m_requestData) {
iLength = -1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

import org.apache.commons.fileupload2.FileItemIterator;
import org.apache.commons.fileupload2.FileItemStream;
Expand Down Expand Up @@ -81,13 +82,13 @@ public void testProgressListener() throws Exception {
final String header = "-----1234\r\n"
+ "Content-Disposition: form-data; name=\"field" + (i+1) + "\"\r\n"
+ "\r\n";
baos.write(header.getBytes("US-ASCII"));
baos.write(header.getBytes(StandardCharsets.US_ASCII));
for (int j = 0; j < 16384+i; j++) {
baos.write((byte) j);
}
baos.write("\r\n".getBytes("US-ASCII"));
baos.write("\r\n".getBytes(StandardCharsets.US_ASCII));
}
baos.write("-----1234--\r\n".getBytes("US-ASCII"));
baos.write("-----1234--\r\n".getBytes(StandardCharsets.US_ASCII));
final byte[] contents = baos.toByteArray();

MockHttpServletRequest request = new MockHttpServletRequest(contents, Constants.CONTENT_TYPE);
Expand Down
25 changes: 13 additions & 12 deletions src/test/java/org/apache/commons/fileupload2/SizesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Iterator;
import java.util.List;

Expand Down Expand Up @@ -62,13 +63,13 @@ public void testFileUpload()
final String header = "-----1234\r\n"
+ "Content-Disposition: form-data; name=\"field" + (num++) + "\"\r\n"
+ "\r\n";
baos.write(header.getBytes("US-ASCII"));
baos.write(header.getBytes(StandardCharsets.US_ASCII));
for (int j = 0; j < i; j++) {
baos.write((byte) j);
}
baos.write("\r\n".getBytes("US-ASCII"));
baos.write("\r\n".getBytes(StandardCharsets.US_ASCII));
}
baos.write("-----1234--\r\n".getBytes("US-ASCII"));
baos.write("-----1234--\r\n".getBytes(StandardCharsets.US_ASCII));

final List<FileItem> fileItems =
Util.parseUpload(new ServletFileUpload(new DiskFileItemFactory()), baos.toByteArray());
Expand Down Expand Up @@ -107,23 +108,23 @@ public void testFileSizeLimit()
ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory());
upload.setFileSizeMax(-1);
HttpServletRequest req = new MockHttpServletRequest(
request.getBytes("US-ASCII"), Constants.CONTENT_TYPE);
request.getBytes(StandardCharsets.US_ASCII), Constants.CONTENT_TYPE);
List<FileItem> fileItems = upload.parseRequest(req);
assertEquals(1, fileItems.size());
FileItem item = fileItems.get(0);
assertEquals("This is the content of the file\n", new String(item.get()));

upload = new ServletFileUpload(new DiskFileItemFactory());
upload.setFileSizeMax(40);
req = new MockHttpServletRequest(request.getBytes("US-ASCII"), Constants.CONTENT_TYPE);
req = new MockHttpServletRequest(request.getBytes(StandardCharsets.US_ASCII), Constants.CONTENT_TYPE);
fileItems = upload.parseRequest(req);
assertEquals(1, fileItems.size());
item = fileItems.get(0);
assertEquals("This is the content of the file\n", new String(item.get()));

upload = new ServletFileUpload(new DiskFileItemFactory());
upload.setFileSizeMax(30);
req = new MockHttpServletRequest(request.getBytes("US-ASCII"), Constants.CONTENT_TYPE);
req = new MockHttpServletRequest(request.getBytes(StandardCharsets.US_ASCII), Constants.CONTENT_TYPE);
try {
upload.parseRequest(req);
fail("Expected exception.");
Expand All @@ -150,15 +151,15 @@ public void testFileSizeLimitWithFakedContentLength()
ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory());
upload.setFileSizeMax(-1);
HttpServletRequest req = new MockHttpServletRequest(
request.getBytes("US-ASCII"), Constants.CONTENT_TYPE);
request.getBytes(StandardCharsets.US_ASCII), Constants.CONTENT_TYPE);
List<FileItem> fileItems = upload.parseRequest(req);
assertEquals(1, fileItems.size());
FileItem item = fileItems.get(0);
assertEquals("This is the content of the file\n", new String(item.get()));

upload = new ServletFileUpload(new DiskFileItemFactory());
upload.setFileSizeMax(40);
req = new MockHttpServletRequest(request.getBytes("US-ASCII"), Constants.CONTENT_TYPE);
req = new MockHttpServletRequest(request.getBytes(StandardCharsets.US_ASCII), Constants.CONTENT_TYPE);
fileItems = upload.parseRequest(req);
assertEquals(1, fileItems.size());
item = fileItems.get(0);
Expand All @@ -167,7 +168,7 @@ public void testFileSizeLimitWithFakedContentLength()
// provided Content-Length is larger than the FileSizeMax -> handled by ctor
upload = new ServletFileUpload(new DiskFileItemFactory());
upload.setFileSizeMax(5);
req = new MockHttpServletRequest(request.getBytes("US-ASCII"), Constants.CONTENT_TYPE);
req = new MockHttpServletRequest(request.getBytes(StandardCharsets.US_ASCII), Constants.CONTENT_TYPE);
try {
upload.parseRequest(req);
fail("Expected exception.");
Expand All @@ -178,7 +179,7 @@ public void testFileSizeLimitWithFakedContentLength()
// provided Content-Length is wrong, actual content is larger -> handled by LimitedInputStream
upload = new ServletFileUpload(new DiskFileItemFactory());
upload.setFileSizeMax(15);
req = new MockHttpServletRequest(request.getBytes("US-ASCII"), Constants.CONTENT_TYPE);
req = new MockHttpServletRequest(request.getBytes(StandardCharsets.US_ASCII), Constants.CONTENT_TYPE);
try {
upload.parseRequest(req);
fail("Expected exception.");
Expand Down Expand Up @@ -213,7 +214,7 @@ public void testMaxSizeLimit()
upload.setSizeMax(200);

final MockHttpServletRequest req = new MockHttpServletRequest(
request.getBytes("US-ASCII"), Constants.CONTENT_TYPE);
request.getBytes(StandardCharsets.US_ASCII), Constants.CONTENT_TYPE);
try {
upload.parseRequest(req);
fail("Expected exception.");
Expand Down Expand Up @@ -250,7 +251,7 @@ public void testMaxSizeLimitUnknownContentLength()
// otherwise the buffer would be immediately filled

final MockHttpServletRequest req = new MockHttpServletRequest(
request.getBytes("US-ASCII"), Constants.CONTENT_TYPE);
request.getBytes(StandardCharsets.US_ASCII), Constants.CONTENT_TYPE);
req.setContentLength(-1);
req.setReadLimit(10);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
Expand Down Expand Up @@ -206,7 +207,7 @@ private String getFooter() {

private byte[] newShortRequest() throws IOException {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final OutputStreamWriter osw = new OutputStreamWriter(baos, "US-ASCII");
final OutputStreamWriter osw = new OutputStreamWriter(baos, StandardCharsets.US_ASCII);
osw.write(getHeader("field"));
osw.write("123");
osw.write("\r\n");
Expand All @@ -217,7 +218,7 @@ private byte[] newShortRequest() throws IOException {

private byte[] newRequest() throws IOException {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final OutputStreamWriter osw = new OutputStreamWriter(baos, "US-ASCII");
final OutputStreamWriter osw = new OutputStreamWriter(baos, StandardCharsets.US_ASCII);
int add = 16;
int num = 0;
for (int i = 0; i < 16384; i += add) {
Expand Down Expand Up @@ -262,7 +263,7 @@ public void testInvalidFileNameException() throws Exception {
"\r\n" +
"value2\r\n" +
"-----1234--\r\n";
final byte[] reqBytes = request.getBytes("US-ASCII");
final byte[] reqBytes = request.getBytes(StandardCharsets.US_ASCII);

final FileItemIterator fileItemIter = parseUpload(reqBytes.length, new ByteArrayInputStream(reqBytes));
final FileItemStream fileItemStream = fileItemIter.next();
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/org/apache/commons/fileupload2/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.apache.commons.fileupload2;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;

Expand Down Expand Up @@ -49,7 +50,7 @@ public static List<FileItem> parseUpload(final FileUpload upload, final byte[] b

public static List<FileItem> parseUpload(final FileUpload upload, final String content)
throws UnsupportedEncodingException, FileUploadException {
final byte[] bytes = content.getBytes("US-ASCII");
final byte[] bytes = content.getBytes(StandardCharsets.US_ASCII);
return parseUpload(upload, bytes, Constants.CONTENT_TYPE);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;

Expand All @@ -31,7 +32,7 @@
import org.junit.jupiter.api.Test;

/**
* Test for {@link ServletFileUpload}.
* Test for {@link org.apache.commons.fileupload2.servlet.ServletFileUpload}.
*
* @see FileUploadTest
* @since 1.4
Expand Down Expand Up @@ -63,7 +64,7 @@ public void parseParameterMap()
"\r\n" +
"value2\r\n" +
"-----1234--\r\n";
final byte[] bytes = text.getBytes("US-ASCII");
final byte[] bytes = text.getBytes(StandardCharsets.US_ASCII);
final HttpServletRequest request = new MockJakSrvltHttpRequest(bytes, Constants.CONTENT_TYPE);

final JakSrvltFileUpload upload = new JakSrvltFileUpload(new DiskFileItemFactory());
Expand All @@ -86,18 +87,18 @@ public void parseImpliedUtf8()
final String text = "-----1234\r\n" +
"Content-Disposition: form-data; name=\"utf8Html\"\r\n" +
"\r\n" +
"Thís ís the coñteñt of the fíle\n" +
"Th�s �s the co�te�t of the f�le\n" +
"\r\n" +
"-----1234--\r\n";

final byte[] bytes = text.getBytes("UTF-8");
final byte[] bytes = text.getBytes(StandardCharsets.UTF_8);
final HttpServletRequest request = new MockJakSrvltHttpRequest(bytes, Constants.CONTENT_TYPE);

final DiskFileItemFactory fileItemFactory = new DiskFileItemFactory();
fileItemFactory.setDefaultCharset("UTF-8");
final JakSrvltFileUpload upload = new JakSrvltFileUpload(fileItemFactory);
final List<FileItem> fileItems = upload.parseRequest(request);
final FileItem fileItem = fileItems.get(0);
assertTrue(fileItem.getString().contains("coñteñt"), fileItem.getString());
assertTrue(fileItem.getString().contains("co�te�t"), fileItem.getString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ public void setCharacterEncoding(final String arg0)
*/
@Override
public int getContentLength() {
int iLength = 0;
final int iLength;

if (null == m_requestData) {
iLength = -1;
Expand Down

0 comments on commit 09e0517

Please sign in to comment.