This is an automated email from the ASF dual-hosted git repository. markt pushed a commit to branch 1.x in repository https://gitbox.apache.org/repos/asf/commons-fileupload.git
commit 43e2a399d2e302da4775ced561eee0e1fdc92523 Author: Mark Thomas <ma...@apache.org> AuthorDate: Fri Sep 15 21:17:19 2023 +0100 Fix Checkstyle warnings --- .../fileupload/util/mime/RFC2231Utility.java | 82 ++++++++++++++-------- 1 file changed, 52 insertions(+), 30 deletions(-) diff --git a/src/main/java/org/apache/commons/fileupload/util/mime/RFC2231Utility.java b/src/main/java/org/apache/commons/fileupload/util/mime/RFC2231Utility.java index af77227d..42bcc340 100644 --- a/src/main/java/org/apache/commons/fileupload/util/mime/RFC2231Utility.java +++ b/src/main/java/org/apache/commons/fileupload/util/mime/RFC2231Utility.java @@ -22,18 +22,31 @@ import java.io.UnsupportedEncodingException; * Utility class to decode/encode character set on HTTP Header fields based on RFC 2231. * This implementation adheres to RFC 5987 in particular, which was defined for HTTP headers * - * RFC 5987 builds on RFC 2231, but has lesser scope like <a href="https://tools.ietf.org/html/rfc5987#section-3.2>mandatory charset definition</a> - * and <a href="https://tools.ietf.org/html/rfc5987#section-4>no parameter continuation</a> + * RFC 5987 builds on RFC 2231, but has lesser scope like + * <a href="https://tools.ietf.org/html/rfc5987#section-3.2">mandatory charset definition</a> + * and <a href="https://tools.ietf.org/html/rfc5987#section-4">no parameter continuation</a> * * <p> * @see <a href="https://tools.ietf.org/html/rfc2231">RFC 2231</a> * @see <a href="https://tools.ietf.org/html/rfc5987">RFC 5987</a> */ public final class RFC2231Utility { - + /** + * The Hexadecimal values char array. + */ private static final char[] HEX_DIGITS = "0123456789ABCDEF".toCharArray(); - - private static final byte[] HEX_DECODE = new byte[0x80]; + /** + * The Hexadecimal representation of 127. + */ + private static final byte MASK = 0x7f; + /** + * The Hexadecimal representation of 128. + */ + private static final int MASK_128 = 0x80; + /** + * The Hexadecimal decode value. + */ + private static final byte[] HEX_DECODE = new byte[MASK_128]; // create a ASCII decoded array of Hexadecimal values static { @@ -43,29 +56,36 @@ public final class RFC2231Utility { } } + /** + * Private constructor so that no instances can be created. This class + * contains only static utility methods. + */ + private RFC2231Utility() { + } + /** * Checks if Asterisk (*) at the end of parameter name to indicate, - * if it has charset and language information to decode the value - * @param paramName + * if it has charset and language information to decode the value. + * @param paramName The parameter, which is being checked. * @return {@code true}, if encoded as per RFC 2231, {@code false} otherwise */ - public static boolean hasEncodedValue(String paramName) { + public static boolean hasEncodedValue(final String paramName) { if (paramName != null) { - return paramName.lastIndexOf("*") == (paramName.length() - 1); + return paramName.lastIndexOf('*') == (paramName.length() - 1); } return false; } /** - * If {@code paramName} has Asterisk (*) at the end, it will be stripped off, - * else the passed value will be returned - * @param paramName + * If {@code paramName} has Asterisk (*) at the end, it will be stripped off, + * else the passed value will be returned. + * @param paramName The parameter, which is being inspected. * @return stripped {@code paramName} of Asterisk (*), if RFC2231 encoded */ - public static String stripDelimiter(String paramName) { + public static String stripDelimiter(final String paramName) { if (hasEncodedValue(paramName)) { - StringBuilder paramBuilder = new StringBuilder(paramName); - paramBuilder.deleteCharAt(paramName.lastIndexOf("*")); + final StringBuilder paramBuilder = new StringBuilder(paramName); + paramBuilder.deleteCharAt(paramName.lastIndexOf('*')); return paramBuilder.toString(); } return paramName; @@ -84,42 +104,44 @@ public final class RFC2231Utility { * <b>Eg 3.</b> {@code UTF-8''%c2%a3%20and%20%e2%82%ac%20rates} * will be decoded to {@code £ and € rates} * - * @param encodedText - Text to be decoded has a format of {@code <charset>'<language>'<encoded_value>} and ASCII only + * @param encodedText - Text to be decoded has a format of {@code <charset>'<language>'<encoded_value>} + * and ASCII only * @return Decoded text based on charset encoding - * @throws UnsupportedEncodingException + * @throws UnsupportedEncodingException The requested character set wasn't found. */ - public static String decodeText(String encodedText) throws UnsupportedEncodingException { - int langDelimitStart = encodedText.indexOf('\''); + public static String decodeText(final String encodedText) throws UnsupportedEncodingException { + final int langDelimitStart = encodedText.indexOf('\''); if (langDelimitStart == -1) { // missing charset return encodedText; } - String mimeCharset = encodedText.substring(0, langDelimitStart); - int langDelimitEnd = encodedText.indexOf('\'', langDelimitStart + 1); + final String mimeCharset = encodedText.substring(0, langDelimitStart); + final int langDelimitEnd = encodedText.indexOf('\'', langDelimitStart + 1); if (langDelimitEnd == -1) { // missing language return encodedText; } - byte[] bytes = fromHex(encodedText.substring(langDelimitEnd + 1)); + final byte[] bytes = fromHex(encodedText.substring(langDelimitEnd + 1)); return new String(bytes, getJavaCharset(mimeCharset)); } /** - * Convert {@code text} to their corresponding Hex value + * Convert {@code text} to their corresponding Hex value. * @param text - ASCII text input * @return Byte array of characters decoded from ASCII table */ - private static byte[] fromHex(String text) { - ByteArrayOutputStream out = new ByteArrayOutputStream(text.length()); + private static byte[] fromHex(final String text) { + final int shift = 4; + final ByteArrayOutputStream out = new ByteArrayOutputStream(text.length()); for (int i = 0; i < text.length();) { - char c = text.charAt(i++); + final char c = text.charAt(i++); if (c == '%') { if (i > text.length() - 2) { break; // unterminated sequence } - byte b1 = HEX_DECODE[text.charAt(i++) & 0x7f]; - byte b2 = HEX_DECODE[text.charAt(i++) & 0x7f]; - out.write((b1 << 4) | b2); + final byte b1 = HEX_DECODE[text.charAt(i++) & MASK]; + final byte b2 = HEX_DECODE[text.charAt(i++) & MASK]; + out.write((b1 << shift) | b2); } else { out.write((byte) c); } @@ -127,7 +149,7 @@ public final class RFC2231Utility { return out.toByteArray(); } - private static String getJavaCharset(String mimeCharset) { + private static String getJavaCharset(final String mimeCharset) { // good enough for standard values return mimeCharset; }