[portal-commits] JBoss Portal SVN: r11962 - modules/common/branches/JBP_COMMON_BRANCH_1_1/common/src/main/java/org/jboss/portal/common/util.

portal-commits at lists.jboss.org portal-commits at lists.jboss.org
Wed Sep 24 08:15:39 EDT 2008


Author: thomas.heute at jboss.com
Date: 2008-09-24 08:15:39 -0400 (Wed, 24 Sep 2008)
New Revision: 11962

Modified:
   modules/common/branches/JBP_COMMON_BRANCH_1_1/common/src/main/java/org/jboss/portal/common/util/Base64.java
Log:
Add option to not add a carriage return


Modified: modules/common/branches/JBP_COMMON_BRANCH_1_1/common/src/main/java/org/jboss/portal/common/util/Base64.java
===================================================================
--- modules/common/branches/JBP_COMMON_BRANCH_1_1/common/src/main/java/org/jboss/portal/common/util/Base64.java	2008-09-24 12:11:35 UTC (rev 11961)
+++ modules/common/branches/JBP_COMMON_BRANCH_1_1/common/src/main/java/org/jboss/portal/common/util/Base64.java	2008-09-24 12:15:39 UTC (rev 11962)
@@ -49,7 +49,6 @@
    /** Maximum line length (76) of Base64 output. */
    private final static int MAX_LINE_LENGTH = 76;
 
-
    /** The equals sign (=) as a byte. */
    private final static byte EQUALS_SIGN = (byte)'=';
 
@@ -59,14 +58,13 @@
    /** The character to be used as a padding character in the encoded Strings. */
    private byte PADDING_CHAR;
 
-
    /** The new line character (\n) as a byte. */
    private final static byte NEW_LINE = (byte)'\n';
 
-
    /** Preferred encoding. */
    private final static String PREFERRED_ENCODING = "UTF-8";
 
+   public static enum EncodingOption { USEURLSAFEENCODING, NOCARRIAGERETURN, STANDARD};
 
    /** The 64 valid Base64 values. */
    private byte[] ALPHABET;
@@ -212,9 +210,9 @@
       initAlphabets(useURLSafeEncoding);
    }
 
-   private static Base64 getBase64(boolean useURLSafeEncoding)
+   private static Base64 getBase64(EncodingOption option)
    {
-      return useURLSafeEncoding ? URL_SAFE_BASE64 : STANDARD_BASE64;
+      return EncodingOption.USEURLSAFEENCODING.equals(option) ? URL_SAFE_BASE64 : STANDARD_BASE64;
    }
 
 /* ********  E N C O D I N G   M E T H O D S  ******** */
@@ -293,41 +291,38 @@
     */
    public static String encodeBytes(byte[] source)
    {
-      return encodeBytes(source, 0, source.length, false);
+      return encodeBytes(source, EncodingOption.STANDARD);
    }   // end encodeBytes
 
-
+   
    /**
     * Encodes a byte array into Base64 notation.
     *
     * @param source             The data to convert
-    * @param useURLSafeEncoding <code>true</code> to use '-', '_' instead of '+', '/' in the alphabet and '*' instead
-    *                           of '=' for padding to generate a URL-safe encoding. <i>Note: Technically, this makes
-    *                           your encoding non-compliant.</i>
+    * @param option             Encoding option
     */
-   public static String encodeBytes(byte[] source, boolean useURLSafeEncoding)
+   public static String encodeBytes(byte[] source, EncodingOption option)
    {
-      return encodeBytes(source, 0, source.length, useURLSafeEncoding);
+      return encodeBytes(source, 0, source.length, option);
    }   // end encodeBytes
 
+
    /**
     * Encodes a byte array into Base64 notation.
     *
     * @param source             The data to convert
     * @param off                Offset in array where conversion should begin
     * @param len                Length of data to convert
-    * @param useURLSafeEncoding <code>true</code> to use '-', '_' instead of '+', '/' in the alphabet and '*' instead
-    *                           of '=' for padding to generate a URL-safe encoding. <i>Note: Technically, this makes
-    *                           your encoding non-compliant.</i>
+    * @param option             Encoding option
     */
-   public static String encodeBytes(byte[] source, int off, int len, boolean useURLSafeEncoding)
+   public static String encodeBytes(byte[] source, int off, int len, EncodingOption option)
    {
-      Base64 b64 = getBase64(useURLSafeEncoding);
+      Base64 b64 = getBase64(option);
 
       int len43 = len * 4 / 3;
       byte[] outBuff = new byte[(len43)                      // Main 4:3
          + ((len % 3) > 0 ? 4 : 0)      // Account for padding
-         + ((!useURLSafeEncoding) ? (len43 / MAX_LINE_LENGTH) : 0)]; // New lines
+         + ((EncodingOption.STANDARD.equals(option)) ? (len43 / MAX_LINE_LENGTH) : 0)]; // New lines
       int d = 0;
       int e = 0;
       int len2 = len - 2;
@@ -337,7 +332,7 @@
          b64.encode3to4(source, d + off, 3, outBuff, e);
 
          lineLength += 4;
-         if (!useURLSafeEncoding && lineLength == MAX_LINE_LENGTH)
+         if (EncodingOption.STANDARD.equals(option) && lineLength == MAX_LINE_LENGTH)
          {
             outBuff[e + 4] = NEW_LINE;
             e++;
@@ -513,12 +508,12 @@
     * @param source                 The Base64 encoded data
     * @param off                    The offset of where to begin decoding
     * @param len                    The length of characters to decode
-    * @param urlSafeEncodingWasUsed <code>true</code> if the URL-safe was used to encode the data to be decoded
+    * @param optionThatWasUsed      EncodingOption used during the encoding
     * @return decoded data
     */
-   public static byte[] decode(byte[] source, int off, int len, boolean urlSafeEncodingWasUsed)
+   public static byte[] decode(byte[] source, int off, int len, EncodingOption optionThatWasUsed)
    {
-      return getBase64(urlSafeEncodingWasUsed).decode(len, off, source);
+      return getBase64(optionThatWasUsed).decode(len, off, source);
    }   // end decode
 
    /**
@@ -526,9 +521,10 @@
     *
     * @param s                      the string to decode
     * @param urlSafeEncodingWasUsed <code>true</code> if the URL-safe was used to encode the data to be decoded
+    * @param optionThatWasUsed      EncodingOption used during the encoding
     * @return the decoded data
     */
-   public static byte[] decode(String s, boolean urlSafeEncodingWasUsed)
+   public static byte[] decode(String s, EncodingOption optionThatWasUsed)
    {
       byte[] bytes;
       try
@@ -541,7 +537,7 @@
       }   // end catch
 
       // Decode
-      bytes = decode(bytes, 0, bytes.length, urlSafeEncodingWasUsed);
+      bytes = decode(bytes, 0, bytes.length, optionThatWasUsed);
 
       return bytes;
    }   // end decode
@@ -554,7 +550,85 @@
     */
    public static byte[] decode(String s)
    {
-      return decode(s, false);
+      return decode(s, EncodingOption.STANDARD);
    }   // end decode
 
+   // Deprecated methods
+   
+   /**
+    * Encodes a byte array into Base64 notation using the standard Base64 encoding.
+    *
+    * @deprecated
+    * @param source The data to convert
+    * @param useURLSafeEncoding <code>true</code> to use '-', '_' instead of '+', '/' in the alphabet and '*' instead
+    *                           of '=' for padding to generate a URL-safe encoding. <i>Note: Technically, this makes
+    *                           your encoding non-compliant.</i>
+    */
+   public static String encodeBytes(byte[] source, boolean useURLSafeEncoding)
+   {
+      if (useURLSafeEncoding)
+      {
+         return encodeBytes(source, EncodingOption.USEURLSAFEENCODING);
+      }
+      else
+      {
+         return encodeBytes(source, EncodingOption.STANDARD);
+      }
+   }   // end encodeBytes
+
+   /**
+    * Encodes a byte array into Base64 notation.
+    *
+    * @deprecated
+    * @param source             The data to convert
+    * @param off                Offset in array where conversion should begin
+    * @param len                Length of data to convert
+    * @param useURLSafeEncoding <code>true</code> to use '-', '_' instead of '+', '/' in the alphabet and '*' instead
+    *                           of '=' for padding to generate a URL-safe encoding. <i>Note: Technically, this makes
+    *                           your encoding non-compliant.</i>
+    */
+   public static String encodeBytes(byte[] source, int off, int len, boolean useURLSafeEncoding)
+   {
+      return (useURLSafeEncoding) ? encodeBytes(source, off, len, EncodingOption.USEURLSAFEENCODING) : encodeBytes(source, off, len, EncodingOption.STANDARD);
+   }
+   
+   /**
+    * Very low-level access to decoding ASCII characters in
+    * the form of a byte array.
+    *
+    * @deprecated
+    * @param source                 The Base64 encoded data
+    * @param off                    The offset of where to begin decoding
+    * @param len                    The length of characters to decode
+    * @param urlSafeEncodingWasUsed <code>true</code> if the URL-safe was used to encode the data to be decoded
+    * @return decoded data
+    */
+   public static byte[] decode(byte[] source, int off, int len, boolean urlSafeEncodingWasUsed)
+   {
+      EncodingOption option = EncodingOption.STANDARD;
+      if (urlSafeEncodingWasUsed)
+      {
+         option = EncodingOption.USEURLSAFEENCODING;
+      }
+      return decode(source, off, len, option);
+   }   // end decode
+
+   /**
+    * Decodes data from Base64 notation
+    *
+    * @deprecated
+    * @param s                      the string to decode
+    * @param urlSafeEncodingWasUsed <code>true</code> if the URL-safe was used to encode the data to be decoded
+    * @return the decoded data
+    */
+   public static byte[] decode(String s, boolean urlSafeEncodingWasUsed)
+   {
+      EncodingOption option = EncodingOption.STANDARD;
+      if (urlSafeEncodingWasUsed)
+      {
+         option = EncodingOption.USEURLSAFEENCODING;
+      }
+      return decode(s, option);
+   }   // end decode
+   
 }   // end class Base64




More information about the portal-commits mailing list