Author: markt Date: Tue Mar 19 09:29:16 2013 New Revision: 1458187 URL: http://svn.apache.org/r1458187 Log: Syncing with Commons Fileupload trunk
Modified: tomcat/trunk/java/org/apache/tomcat/util/http/fileupload/ (props changed) tomcat/trunk/java/org/apache/tomcat/util/http/fileupload/util/mime/Base64Decoder.java tomcat/trunk/java/org/apache/tomcat/util/http/fileupload/util/mime/MimeUtility.java Propchange: tomcat/trunk/java/org/apache/tomcat/util/http/fileupload/ ------------------------------------------------------------------------------ Merged /commons/proper/fileupload/trunk/src/main/java/org/apache/commons/fileupload:r1456913-1456917,1456919-1457004 Modified: tomcat/trunk/java/org/apache/tomcat/util/http/fileupload/util/mime/Base64Decoder.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/util/http/fileupload/util/mime/Base64Decoder.java?rev=1458187&r1=1458186&r2=1458187&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/tomcat/util/http/fileupload/util/mime/Base64Decoder.java (original) +++ tomcat/trunk/java/org/apache/tomcat/util/http/fileupload/util/mime/Base64Decoder.java Tue Mar 19 09:29:16 2013 @@ -25,7 +25,27 @@ import java.io.OutputStream; final class Base64Decoder { /** - * set up the encoding table. + * Bytes per undecoded block. + */ + private static final int BYTES_PER_UNENCODED_BLOCK = 3; + + /** + * 2 bits mask. + */ + private static final int MASK_2BITS = 2; + + /** + * 4 bits mask. + */ + private static final int MASK_4BITS = 4; + + /** + * 6 bits mask. + */ + private static final int MASK_6BITS = 6; + + /** + * Set up the encoding table. */ private static final byte[] ENCODING_TABLE = { (byte) 'A', (byte) 'B', (byte) 'C', (byte) 'D', (byte) 'E', (byte) 'F', (byte) 'G', @@ -48,12 +68,12 @@ final class Base64Decoder { private static final byte PADDING = (byte) '='; /** - * the decoding table size. + * The decoding table size. */ private static final int DECODING_TABLE_SIZE = 256; /** - * set up the decoding table. + * Set up the decoding table. */ private static final byte[] DECODING_TABLE = new byte[DECODING_TABLE_SIZE]; @@ -76,23 +96,17 @@ final class Base64Decoder { * @param c the char has to be checked. * @return true, if the input char has to be checked, false otherwise. */ - private static boolean ignore( - char c) { + private static boolean ignore(char c) { return (c == '\n' || c == '\r' || c == '\t' || c == ' '); } /** - * decode the base 64 encoded byte data writing it to the given output stream, + * Decode the base 64 encoded byte data writing it to the given output stream, * whitespace characters will be ignored. * * @return the number of bytes produced. */ - public static int decode( - byte[] data, - int off, - int length, - OutputStream out) - throws IOException { + public static int decode(byte[] data, int off, int length, OutputStream out) throws IOException { byte b1, b2, b3, b4; int outLen = 0; @@ -107,7 +121,7 @@ final class Base64Decoder { } int i = off; - int finish = end - 4; + int finish = end - MASK_4BITS; while (i < finish) { while ((i < finish) && ignore((char) data[i])) { @@ -134,40 +148,40 @@ final class Base64Decoder { b4 = DECODING_TABLE[data[i++]]; - out.write((b1 << 2) | (b2 >> 4)); - out.write((b2 << 4) | (b3 >> 2)); - out.write((b3 << 6) | b4); + out.write((b1 << MASK_2BITS) | (b2 >> MASK_4BITS)); + out.write((b2 << MASK_4BITS) | (b3 >> MASK_2BITS)); + out.write((b3 << MASK_6BITS) | b4); - outLen += 3; + outLen += BYTES_PER_UNENCODED_BLOCK; } - if (data[end - 2] == PADDING) { - b1 = DECODING_TABLE[data[end - 4]]; - b2 = DECODING_TABLE[data[end - 3]]; + if (data[end - MASK_2BITS] == PADDING) { + b1 = DECODING_TABLE[data[end - MASK_4BITS]]; + b2 = DECODING_TABLE[data[end - BYTES_PER_UNENCODED_BLOCK]]; - out.write((b1 << 2) | (b2 >> 4)); + out.write((b1 << MASK_2BITS) | (b2 >> MASK_4BITS)); outLen += 1; } else if (data[end - 1] == PADDING) { - b1 = DECODING_TABLE[data[end - 4]]; - b2 = DECODING_TABLE[data[end - 3]]; - b3 = DECODING_TABLE[data[end - 2]]; + b1 = DECODING_TABLE[data[end - MASK_4BITS]]; + b2 = DECODING_TABLE[data[end - BYTES_PER_UNENCODED_BLOCK]]; + b3 = DECODING_TABLE[data[end - MASK_2BITS]]; - out.write((b1 << 2) | (b2 >> 4)); - out.write((b2 << 4) | (b3 >> 2)); + out.write((b1 << MASK_2BITS) | (b2 >> MASK_4BITS)); + out.write((b2 << MASK_4BITS) | (b3 >> MASK_2BITS)); - outLen += 2; + outLen += MASK_2BITS; } else { - b1 = DECODING_TABLE[data[end - 4]]; - b2 = DECODING_TABLE[data[end - 3]]; - b3 = DECODING_TABLE[data[end - 2]]; + b1 = DECODING_TABLE[data[end - MASK_4BITS]]; + b2 = DECODING_TABLE[data[end - BYTES_PER_UNENCODED_BLOCK]]; + b3 = DECODING_TABLE[data[end - MASK_2BITS]]; b4 = DECODING_TABLE[data[end - 1]]; - out.write((b1 << 2) | (b2 >> 4)); - out.write((b2 << 4) | (b3 >> 2)); - out.write((b3 << 6) | b4); + out.write((b1 << MASK_2BITS) | (b2 >> MASK_4BITS)); + out.write((b2 << MASK_4BITS) | (b3 >> MASK_2BITS)); + out.write((b3 << MASK_6BITS) | b4); - outLen += 3; + outLen += BYTES_PER_UNENCODED_BLOCK; } return outLen; Modified: tomcat/trunk/java/org/apache/tomcat/util/http/fileupload/util/mime/MimeUtility.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/util/http/fileupload/util/mime/MimeUtility.java?rev=1458187&r1=1458186&r2=1458187&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/tomcat/util/http/fileupload/util/mime/MimeUtility.java (original) +++ tomcat/trunk/java/org/apache/tomcat/util/http/fileupload/util/mime/MimeUtility.java Tue Mar 19 09:29:16 2013 @@ -31,6 +31,11 @@ import java.util.Map; public final class MimeUtility { /** + * If the text contains any encoded tokens, those tokens will be marked with "=?". + */ + private static final String ENCODED_TOKEN_MARKER = "=?"; + + /** * The linear whitespace chars sequence. */ private static final String LINEAR_WHITESPACE = " \t\r\n"; @@ -74,7 +79,7 @@ public final class MimeUtility { public static String decodeText(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("=?") < 0) { + if (text.indexOf(ENCODED_TOKEN_MARKER) < 0) { return text; } @@ -84,7 +89,7 @@ public final class MimeUtility { int startWhiteSpace = -1; int endWhiteSpace = -1; - StringBuffer decodedText = new StringBuffer(text.length()); + StringBuilder decodedText = new StringBuilder(text.length()); boolean previousTokenEncoded = false; @@ -124,7 +129,7 @@ public final class MimeUtility { // pull out the word token. String word = text.substring(wordStart, offset); // is the token encoded? decode the word - if (word.startsWith("=?")) { + if (word.startsWith(ENCODED_TOKEN_MARKER)) { try { // if this gives a parsing failure, treat it like a non-encoded word. String decodedWord = decodeWord(word); @@ -177,7 +182,7 @@ public final class MimeUtility { // encoded words start with the characters "=?". If this not an encoded word, we throw a // ParseException for the caller. - if (!word.startsWith("=?")) { + if (!word.startsWith(ENCODED_TOKEN_MARKER)) { throw new ParseException("Invalid RFC 2047 encoded-word: " + word); } --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org