commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From d..@apache.org
Subject cvs commit: jakarta-commons-sandbox/codec/src/java/org/apache/commons/codec/base64 Base64.java
Date Thu, 30 May 2002 16:14:30 GMT
dlr         02/05/30 09:14:30

  Modified:    codec/src/java/org/apache/commons/codec/base64 Base64.java
  Log:
  Integrated base64 encoding methods patch from Gidado-Yisa Immanuel
  <avm3@cdc.gov>:
  
  "Adds 2 new methods for encoding strings plus a little refactoring."
  
  Revision  Changes    Path
  1.4       +80 -6     jakarta-commons-sandbox/codec/src/java/org/apache/commons/codec/base64/Base64.java
  
  Index: Base64.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/codec/src/java/org/apache/commons/codec/base64/Base64.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -u -r1.3 -r1.4
  --- Base64.java	7 Mar 2002 22:38:19 -0000	1.3
  +++ Base64.java	30 May 2002 16:14:30 -0000	1.4
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-commons-sandbox/codec/src/java/org/apache/commons/codec/base64/Base64.java,v
1.3 2002/03/07 22:38:19 dlr Exp $
  - * $Revision: 1.3 $
  - * $Date: 2002/03/07 22:38:19 $
  + * $Header: /home/cvs/jakarta-commons-sandbox/codec/src/java/org/apache/commons/codec/base64/Base64.java,v
1.4 2002/05/30 16:14:30 dlr Exp $
  + * $Revision: 1.4 $
  + * $Date: 2002/05/30 16:14:30 $
    * ====================================================================
    *
    * The Apache Software License, Version 1.1
  @@ -60,6 +60,11 @@
   
   package org.apache.commons.codec.base64;
   
  +import java.io.ByteArrayOutputStream;
  +import java.io.OutputStreamWriter;
  +import java.io.IOException;
  +import java.io.UnsupportedEncodingException;
  +
   /**
    * <p>Base64 encoder and decoder.</p>
    * <p>
  @@ -71,10 +76,11 @@
    * 1996. Available at: http://www.ietf.org/rfc/rfc2045.txt
    * </p>
    * @author Jeffrey Rodriguez
  - * @version $Revision: 1.3 $ $Date: 2002/03/07 22:38:19 $
  + * @version $Revision: 1.4 $ $Date: 2002/05/30 16:14:30 $
    */
   public final class Base64 {
   
  +    static protected final String DEFAULT_CHAR_ENCODING = "ISO-8859-1";
       static private final int BASELENGTH = 255;
       static private final int LOOKUPLENGTH = 64;
       static private final int TWENTYFOURBITGROUP = 24;
  @@ -84,6 +90,7 @@
       static private final int FOURBYTE = 4;
       static private final int SIGN = -128;
       static private final byte PAD = (byte) '=';
  +    static private final byte[] EMPTY_BYTE_ARRAY = new byte[0];
       static private byte[] base64Alphabet = new byte[BASELENGTH];
       static private byte[] lookUpBase64Alphabet = new byte[LOOKUPLENGTH];
   
  @@ -149,6 +156,8 @@
        * @return Base64-encoded array
        */
       public static byte[] encode(byte[] binaryData) {
  +        if (binaryData == null)
  +            binaryData = EMPTY_BYTE_ARRAY;
   
           int lengthDataBits = binaryData.length * EIGHTBIT;
           int fewerThan24bits = lengthDataBits % TWENTYFOURBITGROUP;
  @@ -218,6 +227,71 @@
   
   
       /**
  +     * Returns the base64 encoding of String.  First the String is
  +     * converted to byte[], using the character encoding of
  +     * <code>ISO-8859-1</code>.
  +     *
  +     * @param data String of data to convert
  +     * @return Base64-encoded String
  +     */
  +    public static String encode(String data) {
  +         try {
  +             return encode(data, DEFAULT_CHAR_ENCODING);
  +         }
  +         catch (UnsupportedEncodingException uee) {
  +             throw new IllegalStateException(uee.toString());
  +         }
  +     }
  +
  +
  +    /**
  +     * Returns the base64 encoding of String (by first converting to
  +     * byte[], using the specified <code>charEncoding</code>).  The
  +     * return value is also a String.  The Default
  +     * <code>codeEncoding</chode> is <code>ISO-8859-1</code>.
  +     *
  +     * @param data String of data to convert
  +     * @param charEncoding the character encoding to use when converting
  +     * a String to a byte[]
  +     * @return Base64-encoded String
  +     */
  +    public static String encode(String data, String charEncoding)
  +        throws UnsupportedEncodingException {
  +
  +        // Check arguments
  +        if (data == null)
  +            data = "";
  +        if (charEncoding == null)
  +            charEncoding = DEFAULT_CHAR_ENCODING;
  +
  +        // Convert to byte[]
  +        ByteArrayOutputStream bos = new ByteArrayOutputStream();
  +        OutputStreamWriter osw = new OutputStreamWriter(bos, charEncoding);
  +        try {
  +            osw.write(data);
  +        }
  +        catch (IOException ioe) {
  +            throw new RuntimeException(ioe.toString());
  +        }
  +
  +        // Encode
  +        byte[] encodedData = encode(bos.toByteArray());
  +
  +        // Convert to String
  +        if (encodedData==null)
  +            return "";
  +        bos = new ByteArrayOutputStream(encodedData.length);
  +        try {
  +            bos.write(encodedData);
  +        }
  +        catch (IOException ioe) {
  +            throw new RuntimeException(ioe.toString());
  +        }
  +
  +        return bos.toString(charEncoding);
  +    }
  +
  +    /**
        * Decodes Base64 data into octects
        *
        * @param binaryData Byte array containing Base64 data
  @@ -228,7 +302,7 @@
   
           // handle the edge case, so we don't have to worry about it later
           if (base64Data.length == 0) {
  -            return new byte[0];
  +            return EMPTY_BYTE_ARRAY;
           }
   
           int numberQuadruple = base64Data.length / FOURBYTE;
  @@ -243,7 +317,7 @@
               // ignore the '=' padding
               while (base64Data[lastData - 1] == PAD) {
                   if (--lastData == 0) {
  -                    return new byte[0];
  +                    return EMPTY_BYTE_ARRAY;
                   }
               }
               decodedData = new byte[lastData - numberQuadruple];
  
  
  

--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message