diff --git a/src/main/java/org/apache/commons/fileupload2/FileItemStream.java b/src/main/java/org/apache/commons/fileupload2/FileItemStream.java index e36a2fec3e..4b311854f9 100644 --- a/src/main/java/org/apache/commons/fileupload2/FileItemStream.java +++ b/src/main/java/org/apache/commons/fileupload2/FileItemStream.java @@ -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 diff --git a/src/main/java/org/apache/commons/fileupload2/FileUploadBase.java b/src/main/java/org/apache/commons/fileupload2/FileUploadBase.java index ef93788043..8f5200c54f 100644 --- a/src/main/java/org/apache/commons/fileupload2/FileUploadBase.java +++ b/src/main/java/org/apache/commons/fileupload2/FileUploadBase.java @@ -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; @@ -389,12 +389,7 @@ public Map> parseParameterMap(final RequestContext ctx) for (final FileItem fileItem : items) { final String fieldName = fileItem.getFieldName(); - List mappedItems = itemsMap.get(fieldName); - - if (mappedItems == null) { - mappedItems = new ArrayList<>(); - itemsMap.put(fieldName, mappedItems); - } + List mappedItems = itemsMap.computeIfAbsent(fieldName, k -> new ArrayList<>()); mappedItems.add(fileItem); } @@ -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; } diff --git a/src/main/java/org/apache/commons/fileupload2/MultipartStream.java b/src/main/java/org/apache/commons/fileupload2/MultipartStream.java index 30116287de..2a06a4cf17 100644 --- a/src/main/java/org/apache/commons/fileupload2/MultipartStream.java +++ b/src/main/java/org/apache/commons/fileupload2/MultipartStream.java @@ -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 { diff --git a/src/main/java/org/apache/commons/fileupload2/impl/FileItemIteratorImpl.java b/src/main/java/org/apache/commons/fileupload2/impl/FileItemIteratorImpl.java index 482a196d66..be258bb9b9 100644 --- a/src/main/java/org/apache/commons/fileupload2/impl/FileItemIteratorImpl.java +++ b/src/main/java/org/apache/commons/fileupload2/impl/FileItemIteratorImpl.java @@ -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( @@ -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 { diff --git a/src/main/java/org/apache/commons/fileupload2/util/FileItemHeadersImpl.java b/src/main/java/org/apache/commons/fileupload2/util/FileItemHeadersImpl.java index 06fdf60afa..2c237a7d98 100644 --- a/src/main/java/org/apache/commons/fileupload2/util/FileItemHeadersImpl.java +++ b/src/main/java/org/apache/commons/fileupload2/util/FileItemHeadersImpl.java @@ -87,11 +87,7 @@ public Iterator getHeaders(final String name) { */ public synchronized void addHeader(final String name, final String value) { final String nameLower = name.toLowerCase(Locale.ENGLISH); - List headerValueList = headerNameToValueListMap.get(nameLower); - if (null == headerValueList) { - headerValueList = new ArrayList<>(); - headerNameToValueListMap.put(nameLower, headerValueList); - } + List headerValueList = headerNameToValueListMap.computeIfAbsent(nameLower, k -> new ArrayList<>()); headerValueList.add(value); } diff --git a/src/main/java/org/apache/commons/fileupload2/util/mime/MimeUtility.java b/src/main/java/org/apache/commons/fileupload2/util/mime/MimeUtility.java index eac17a3ba4..3c9c696717 100644 --- a/src/main/java/org/apache/commons/fileupload2/util/mime/MimeUtility.java +++ b/src/main/java/org/apache/commons/fileupload2/util/mime/MimeUtility.java @@ -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; @@ -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)) { diff --git a/src/test/java/org/apache/commons/fileupload2/DefaultFileItemTest.java b/src/test/java/org/apache/commons/fileupload2/DefaultFileItemTest.java index e1d51014ad..4a7c33ed29 100644 --- a/src/test/java/org/apache/commons/fileupload2/DefaultFileItemTest.java +++ b/src/test/java/org/apache/commons/fileupload2/DefaultFileItemTest.java @@ -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; /** diff --git a/src/test/java/org/apache/commons/fileupload2/DiskFileUploadTest.java b/src/test/java/org/apache/commons/fileupload2/DiskFileUploadTest.java index ed88165727..6f703e9139 100644 --- a/src/test/java/org/apache/commons/fileupload2/DiskFileUploadTest.java +++ b/src/test/java/org/apache/commons/fileupload2/DiskFileUploadTest.java @@ -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; @@ -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 items = myUpload.parseRequest(request); assertNotNull(items); diff --git a/src/test/java/org/apache/commons/fileupload2/FileUploadTest.java b/src/test/java/org/apache/commons/fileupload2/FileUploadTest.java index 9d73a605f6..a0f28df342 100644 --- a/src/test/java/org/apache/commons/fileupload2/FileUploadTest.java +++ b/src/test/java/org/apache/commons/fileupload2/FileUploadTest.java @@ -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; @@ -223,7 +224,7 @@ public void testFILEUPLOAD62(final FileUpload upload) throws Exception { "...contents of file2.gif...\r\n" + "--BbC04y--\r\n" + "--AaB03x--"; - final List fileItems = Util.parseUpload(upload, request.getBytes("US-ASCII"), contentType); + final List fileItems = Util.parseUpload(upload, request.getBytes(StandardCharsets.US_ASCII), contentType); assertEquals(3, fileItems.size()); final FileItem item0 = fileItems.get(0); assertEquals("field1", item0.getFieldName()); diff --git a/src/test/java/org/apache/commons/fileupload2/MockHttpServletRequest.java b/src/test/java/org/apache/commons/fileupload2/MockHttpServletRequest.java index 12d1bde486..486c6dee77 100644 --- a/src/test/java/org/apache/commons/fileupload2/MockHttpServletRequest.java +++ b/src/test/java/org/apache/commons/fileupload2/MockHttpServletRequest.java @@ -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; diff --git a/src/test/java/org/apache/commons/fileupload2/ProgressListenerTest.java b/src/test/java/org/apache/commons/fileupload2/ProgressListenerTest.java index 27df818f5d..9a2cef6e51 100644 --- a/src/test/java/org/apache/commons/fileupload2/ProgressListenerTest.java +++ b/src/test/java/org/apache/commons/fileupload2/ProgressListenerTest.java @@ -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; @@ -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); diff --git a/src/test/java/org/apache/commons/fileupload2/SizesTest.java b/src/test/java/org/apache/commons/fileupload2/SizesTest.java index 9dc9ef7c28..bb6585ccc8 100644 --- a/src/test/java/org/apache/commons/fileupload2/SizesTest.java +++ b/src/test/java/org/apache/commons/fileupload2/SizesTest.java @@ -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; @@ -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 fileItems = Util.parseUpload(new ServletFileUpload(new DiskFileItemFactory()), baos.toByteArray()); @@ -107,7 +108,7 @@ 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 fileItems = upload.parseRequest(req); assertEquals(1, fileItems.size()); FileItem item = fileItems.get(0); @@ -115,7 +116,7 @@ public void testFileSizeLimit() 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); @@ -123,7 +124,7 @@ public void testFileSizeLimit() 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."); @@ -150,7 +151,7 @@ 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 fileItems = upload.parseRequest(req); assertEquals(1, fileItems.size()); FileItem item = fileItems.get(0); @@ -158,7 +159,7 @@ public void testFileSizeLimitWithFakedContentLength() 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); @@ -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."); @@ -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."); @@ -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."); @@ -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); diff --git a/src/test/java/org/apache/commons/fileupload2/StreamingTest.java b/src/test/java/org/apache/commons/fileupload2/StreamingTest.java index 89c4737b73..3075a31d54 100644 --- a/src/test/java/org/apache/commons/fileupload2/StreamingTest.java +++ b/src/test/java/org/apache/commons/fileupload2/StreamingTest.java @@ -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; @@ -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"); @@ -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) { @@ -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(); diff --git a/src/test/java/org/apache/commons/fileupload2/Util.java b/src/test/java/org/apache/commons/fileupload2/Util.java index 5ca30e4d5f..c747f7884f 100644 --- a/src/test/java/org/apache/commons/fileupload2/Util.java +++ b/src/test/java/org/apache/commons/fileupload2/Util.java @@ -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; @@ -49,7 +50,7 @@ public static List parseUpload(final FileUpload upload, final byte[] b public static List 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); } diff --git a/src/test/java/org/apache/commons/fileupload2/jaksrvlt/JakSrvltFileUploadTest.java b/src/test/java/org/apache/commons/fileupload2/jaksrvlt/JakSrvltFileUploadTest.java index e1b52a7966..aadfb319be 100644 --- a/src/test/java/org/apache/commons/fileupload2/jaksrvlt/JakSrvltFileUploadTest.java +++ b/src/test/java/org/apache/commons/fileupload2/jaksrvlt/JakSrvltFileUploadTest.java @@ -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; @@ -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 @@ -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()); @@ -86,11 +87,11 @@ 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(); @@ -98,6 +99,6 @@ public void parseImpliedUtf8() final JakSrvltFileUpload upload = new JakSrvltFileUpload(fileItemFactory); final List 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()); } } diff --git a/src/test/java/org/apache/commons/fileupload2/jaksrvlt/MockJakSrvltHttpRequest.java b/src/test/java/org/apache/commons/fileupload2/jaksrvlt/MockJakSrvltHttpRequest.java index cdab3eba33..ac571e8fcd 100644 --- a/src/test/java/org/apache/commons/fileupload2/jaksrvlt/MockJakSrvltHttpRequest.java +++ b/src/test/java/org/apache/commons/fileupload2/jaksrvlt/MockJakSrvltHttpRequest.java @@ -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; diff --git a/src/test/java/org/apache/commons/fileupload2/portlet/PortletFileUploadTest.java b/src/test/java/org/apache/commons/fileupload2/portlet/PortletFileUploadTest.java index 9cd51b2925..f6eec3f2c2 100644 --- a/src/test/java/org/apache/commons/fileupload2/portlet/PortletFileUploadTest.java +++ b/src/test/java/org/apache/commons/fileupload2/portlet/PortletFileUploadTest.java @@ -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; @@ -69,7 +70,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 ActionRequest request = new MockPortletActionRequest(bytes, Constants.CONTENT_TYPE); final Map> mappedParameters = upload.parseParameterMap(request); diff --git a/src/test/java/org/apache/commons/fileupload2/servlet/ServletFileUploadTest.java b/src/test/java/org/apache/commons/fileupload2/servlet/ServletFileUploadTest.java index c8b57b3e90..d6bf273925 100644 --- a/src/test/java/org/apache/commons/fileupload2/servlet/ServletFileUploadTest.java +++ b/src/test/java/org/apache/commons/fileupload2/servlet/ServletFileUploadTest.java @@ -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; @@ -65,7 +66,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 MockHttpServletRequest(bytes, Constants.CONTENT_TYPE); final ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory()); @@ -88,11 +89,11 @@ 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 MockHttpServletRequest(bytes, Constants.CONTENT_TYPE); final DiskFileItemFactory fileItemFactory = new DiskFileItemFactory(); @@ -100,6 +101,6 @@ public void parseImpliedUtf8() final ServletFileUpload upload = new ServletFileUpload(fileItemFactory); final List 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()); } } diff --git a/src/test/java/org/apache/commons/fileupload2/util/mime/Base64DecoderTestCase.java b/src/test/java/org/apache/commons/fileupload2/util/mime/Base64DecoderTestCase.java index 25180e619c..4124ed8ce9 100644 --- a/src/test/java/org/apache/commons/fileupload2/util/mime/Base64DecoderTestCase.java +++ b/src/test/java/org/apache/commons/fileupload2/util/mime/Base64DecoderTestCase.java @@ -24,6 +24,7 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; import org.apache.commons.fileupload2.util.mime.Base64Decoder; import org.junit.jupiter.api.Test; @@ -135,15 +136,15 @@ public void badLength() throws Exception { // The non-ASCII characters should just be ignored @Test public void nonASCIIcharacter() throws Exception { - assertEncoded("f","Zg=À="); // A-grave + assertEncoded("f","Zg=�="); // A-grave assertEncoded("f","Zg=\u0100="); } private static void assertEncoded(final String clearText, final String encoded) throws Exception { - final byte[] expected = clearText.getBytes(US_ASCII_CHARSET); + final byte[] expected = clearText.getBytes(StandardCharsets.US_ASCII); final ByteArrayOutputStream out = new ByteArrayOutputStream(encoded.length()); - final byte[] encodedData = encoded.getBytes(US_ASCII_CHARSET); + final byte[] encodedData = encoded.getBytes(StandardCharsets.US_ASCII); Base64Decoder.decode(encodedData, out); final byte[] actual = out.toByteArray(); @@ -152,7 +153,7 @@ private static void assertEncoded(final String clearText, final String encoded) private static void assertIOException(final String messageText, final String encoded) throws UnsupportedEncodingException { final ByteArrayOutputStream out = new ByteArrayOutputStream(encoded.length()); - final byte[] encodedData = encoded.getBytes(US_ASCII_CHARSET); + final byte[] encodedData = encoded.getBytes(StandardCharsets.US_ASCII); try { Base64Decoder.decode(encodedData, out); fail("Expected IOException"); diff --git a/src/test/java/org/apache/commons/fileupload2/util/mime/QuotedPrintableDecoderTestCase.java b/src/test/java/org/apache/commons/fileupload2/util/mime/QuotedPrintableDecoderTestCase.java index 5cfa78d9e1..881c3a1ff2 100644 --- a/src/test/java/org/apache/commons/fileupload2/util/mime/QuotedPrintableDecoderTestCase.java +++ b/src/test/java/org/apache/commons/fileupload2/util/mime/QuotedPrintableDecoderTestCase.java @@ -24,6 +24,7 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; import org.apache.commons.fileupload2.util.mime.QuotedPrintableDecoder; import org.junit.jupiter.api.Test; @@ -100,10 +101,10 @@ public void truncatedEscape() throws Exception { } private static void assertEncoded(final String clearText, final String encoded) throws Exception { - final byte[] expected = clearText.getBytes(US_ASCII_CHARSET); + final byte[] expected = clearText.getBytes(StandardCharsets.US_ASCII); final ByteArrayOutputStream out = new ByteArrayOutputStream(encoded.length()); - final byte[] encodedData = encoded.getBytes(US_ASCII_CHARSET); + final byte[] encodedData = encoded.getBytes(StandardCharsets.US_ASCII); QuotedPrintableDecoder.decode(encodedData, out); final byte[] actual = out.toByteArray(); @@ -112,7 +113,7 @@ private static void assertEncoded(final String clearText, final String encoded) private static void assertIOException(final String messageText, final String encoded) throws UnsupportedEncodingException { final ByteArrayOutputStream out = new ByteArrayOutputStream(encoded.length()); - final byte[] encodedData = encoded.getBytes(US_ASCII_CHARSET); + final byte[] encodedData = encoded.getBytes(StandardCharsets.US_ASCII); try { QuotedPrintableDecoder.decode(encodedData, out); fail("Expected IOException");