You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by si...@apache.org on 2013/03/15 10:49:34 UTC

svn commit: r1456843 - in /commons/proper/fileupload/trunk/src/main/java/org/apache/commons/fileupload/util/mime: Base64Decoder.java MimeUtility.java

Author: simonetripodi
Date: Fri Mar 15 09:49:34 2013
New Revision: 1456843

URL: http://svn.apache.org/r1456843
Log:
minor Base64Decoder refactoring to reflect QuotedPrintableDecoder structure

Modified:
    commons/proper/fileupload/trunk/src/main/java/org/apache/commons/fileupload/util/mime/Base64Decoder.java
    commons/proper/fileupload/trunk/src/main/java/org/apache/commons/fileupload/util/mime/MimeUtility.java

Modified: commons/proper/fileupload/trunk/src/main/java/org/apache/commons/fileupload/util/mime/Base64Decoder.java
URL: http://svn.apache.org/viewvc/commons/proper/fileupload/trunk/src/main/java/org/apache/commons/fileupload/util/mime/Base64Decoder.java?rev=1456843&r1=1456842&r2=1456843&view=diff
==============================================================================
--- commons/proper/fileupload/trunk/src/main/java/org/apache/commons/fileupload/util/mime/Base64Decoder.java (original)
+++ commons/proper/fileupload/trunk/src/main/java/org/apache/commons/fileupload/util/mime/Base64Decoder.java Fri Mar 15 09:49:34 2013
@@ -24,7 +24,7 @@ import java.io.OutputStream;
  */
 final class Base64Decoder {
 
-    private final byte[] encodingTable = {
+    private static final byte[] ENCODING_TABLE = {
         (byte) 'A', (byte) 'B', (byte) 'C', (byte) 'D', (byte) 'E', (byte) 'F', (byte) 'G',
         (byte) 'H', (byte) 'I', (byte) 'J', (byte) 'K', (byte) 'L', (byte) 'M', (byte) 'N',
         (byte) 'O', (byte) 'P', (byte) 'Q', (byte) 'R', (byte) 'S', (byte) 'T', (byte) 'U',
@@ -39,24 +39,24 @@ final class Base64Decoder {
         (byte) '+', (byte) '/'
     };
 
-    private byte padding = (byte) '=';
+    private static final byte PADDING = (byte) '=';
 
     /*
      * set up the decoding table.
      */
-    private final byte[] decodingTable = new byte[256];
+    private static final byte[] DECODING_TABLE = new byte[256];
 
-    protected void initialiseDecodingTable() {
-        for (int i = 0; i < encodingTable.length; i++) {
-            decodingTable[encodingTable[i]] = (byte) i;
+    static {
+        for (int i = 0; i < ENCODING_TABLE.length; i++) {
+            DECODING_TABLE[ENCODING_TABLE[i]] = (byte) i;
         }
     }
 
-    public Base64Decoder() {
-        initialiseDecodingTable();
+    private Base64Decoder() {
+        // do nothing
     }
 
-    private boolean ignore(
+    private static boolean ignore(
         char    c) {
         return (c == '\n' || c == '\r' || c == '\t' || c == ' ');
     }
@@ -67,7 +67,7 @@ final class Base64Decoder {
      *
      * @return the number of bytes produced.
      */
-    public int decode(
+    public static int decode(
         byte[]                data,
         int                    off,
         int                    length,
@@ -94,25 +94,25 @@ final class Base64Decoder {
                 i++;
             }
 
-            b1 = decodingTable[data[i++]];
+            b1 = DECODING_TABLE[data[i++]];
 
             while ((i < finish) && ignore((char) data[i])) {
                 i++;
             }
 
-            b2 = decodingTable[data[i++]];
+            b2 = DECODING_TABLE[data[i++]];
 
             while ((i < finish) && ignore((char) data[i])) {
                 i++;
             }
 
-            b3 = decodingTable[data[i++]];
+            b3 = DECODING_TABLE[data[i++]];
 
             while ((i < finish) && ignore((char) data[i])) {
                 i++;
             }
 
-            b4 = decodingTable[data[i++]];
+            b4 = DECODING_TABLE[data[i++]];
 
             out.write((b1 << 2) | (b2 >> 4));
             out.write((b2 << 4) | (b3 >> 2));
@@ -121,27 +121,27 @@ final class Base64Decoder {
             outLen += 3;
         }
 
-        if (data[end - 2] == padding) {
-            b1 = decodingTable[data[end - 4]];
-            b2 = decodingTable[data[end - 3]];
+        if (data[end - 2] == PADDING) {
+            b1 = DECODING_TABLE[data[end - 4]];
+            b2 = DECODING_TABLE[data[end - 3]];
 
             out.write((b1 << 2) | (b2 >> 4));
 
             outLen += 1;
-        } else if (data[end - 1] == padding) {
-            b1 = decodingTable[data[end - 4]];
-            b2 = decodingTable[data[end - 3]];
-            b3 = decodingTable[data[end - 2]];
+        } else if (data[end - 1] == PADDING) {
+            b1 = DECODING_TABLE[data[end - 4]];
+            b2 = DECODING_TABLE[data[end - 3]];
+            b3 = DECODING_TABLE[data[end - 2]];
 
             out.write((b1 << 2) | (b2 >> 4));
             out.write((b2 << 4) | (b3 >> 2));
 
             outLen += 2;
         } else {
-            b1 = decodingTable[data[end - 4]];
-            b2 = decodingTable[data[end - 3]];
-            b3 = decodingTable[data[end - 2]];
-            b4 = decodingTable[data[end - 1]];
+            b1 = DECODING_TABLE[data[end - 4]];
+            b2 = DECODING_TABLE[data[end - 3]];
+            b3 = DECODING_TABLE[data[end - 2]];
+            b4 = DECODING_TABLE[data[end - 1]];
 
             out.write((b1 << 2) | (b2 >> 4));
             out.write((b2 << 4) | (b3 >> 2));

Modified: commons/proper/fileupload/trunk/src/main/java/org/apache/commons/fileupload/util/mime/MimeUtility.java
URL: http://svn.apache.org/viewvc/commons/proper/fileupload/trunk/src/main/java/org/apache/commons/fileupload/util/mime/MimeUtility.java?rev=1456843&r1=1456842&r2=1456843&view=diff
==============================================================================
--- commons/proper/fileupload/trunk/src/main/java/org/apache/commons/fileupload/util/mime/MimeUtility.java (original)
+++ commons/proper/fileupload/trunk/src/main/java/org/apache/commons/fileupload/util/mime/MimeUtility.java Fri Mar 15 09:49:34 2013
@@ -40,11 +40,6 @@ public final class MimeUtility {
      */
     private static final Map<String, String> MIME2JAVA = new HashMap<String, String>();
 
-    /**
-     * The Base64 decoder.
-     */
-    private static final Base64Decoder BASE64_DECODER = new Base64Decoder();
-
     static {
         MIME2JAVA.put("iso-2022-cn", "ISO2022CN");
         MIME2JAVA.put("iso-2022-kr", "ISO2022KR");
@@ -223,7 +218,7 @@ public final class MimeUtility {
 
             // Base64 encoded?
             if (encoding.equals("B")) {
-                BASE64_DECODER.decode(encodedData, 0, encodedData.length, out);
+                Base64Decoder.decode(encodedData, 0, encodedData.length, out);
             } else if (encoding.equals("Q")) { // maybe quoted printable.
                 QuotedPrintableDecoder.decodeWord(encodedData, 0, encodedData.length, out);
             } else {