There are two methods which does not work properly :
String Base64.encode(String data) and
String Base64.encode(String data, String encoding)
The current implementation uses character encoding provided by the OutputStreamWriter, which is an overhead. I guess we can safely use String.getBytes(encoding), so I made the modification.
I found it useful to have the similar methods for decoding, so I added
String decode(String data) and
String decode(String data, String encoding)
I also modified the test case to use the methods that take String as parameter.
Cheers,
-iulian
Index: src/java/org/apache/commons/codec/base64/Base64.java =================================================================== RCS file: /home/cvspublic/jakarta-commons-sandbox/codec/src/java/org/apache/commons/codec/base64/Base64.java,v retrieving revision 1.4 diff -u -r1.4 Base64.java --- src/java/org/apache/commons/codec/base64/Base64.java 30 May 2002 16:14:30 -0000 1.4 +++ src/java/org/apache/commons/codec/base64/Base64.java 21 Jan 2003 00:04:09 +-0000 @@ -75,7 +75,9 @@ * Part One: Format of Internet Message Bodies. Reference * 1996. Available at: http://www.ietf.org/rfc/rfc2045.txt * </p> + * * @author Jeffrey Rodriguez + * @author Iulian Musat ( decode(String,String) ) * @version $Revision: 1.4 $ $Date: 2002/05/30 16:14:30 $ */ public final class Base64 { @@ -264,31 +266,9 @@ 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); + //fix by Iulian Musat: avoid using OutputStreamWriter + return new String( encode( data.getBytes(charEncoding) ), + charEncoding ); } /** @@ -352,6 +332,49 @@ encodedIndex += 3; } return decodedData; + } + + + /** + * Returns the base64 decoded String. First the String is + * converted to byte[], using the character encoding of + * <code>ISO-8859-1</code>. + * + * @param data String of data to decode + * @return Base64-decoded String + */ + public static String decode(String data) { + try { + return decode(data, DEFAULT_CHAR_ENCODING); + } + catch (UnsupportedEncodingException uee) { + throw new IllegalStateException(uee.toString()); + } + } + + + /** + * Returns the base64 decoded 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 decode + * @param charEncoding the character encoding to use when converting + * a String to a byte[] + * @return Base64-decoded String + */ + public static String decode(String data, String charEncoding) + throws UnsupportedEncodingException { + + // Check arguments + if (data == null) + data = ""; + if (charEncoding == null) + charEncoding = DEFAULT_CHAR_ENCODING; + + return new String( decode( data.getBytes(charEncoding) ), + charEncoding ); } }
Index: src/test/org/apache/commons/codec/base64/Base64TestCase.java =================================================================== RCS file: /home/cvspublic/jakarta-commons-sandbox/codec/src/test/org/apache/commons/codec/base64/Base64TestCase.java,v retrieving revision 1.3 diff -u -r1.3 Base64TestCase.java --- src/test/org/apache/commons/codec/base64/Base64TestCase.java 18 Nov 2002 12:41:24 -0000 1.3 +++ src/test/org/apache/commons/codec/base64/Base64TestCase.java 20 Jan 2003 +23:30:46 -0000 @@ -111,7 +111,7 @@ public void testBase64() { String content = "Hello World"; String encodedContent; - encodedContent = new String(Base64.encode(content.getBytes())); + encodedContent = Base64.encode(content); assertTrue("encoding hello world", encodedContent.equals("SGVsbG8gV29ybGQ=")); } @@ -315,21 +315,33 @@ } public void testKnownEncodings() { - assertEquals("VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wZWQgb3ZlciB0aGUgbGF6eSBkb2dzLg==",new String(Base64.encode("The quick brown fox jumped over the lazy dogs.".getBytes()))); - assertEquals("SXQgd2FzIHRoZSBiZXN0IG9mIHRpbWVzLCBpdCB3YXMgdGhlIHdvcnN0IG9mIHRpbWVzLg==",new String(Base64.encode("It was the best of times, it was the worst of times.".getBytes()))); - assertEquals("aHR0cDovL2pha2FydGEuYXBhY2hlLm9yZy9jb21tbW9ucw==",new String(Base64.encode("http://jakarta.apache.org/commmons".getBytes()))); - assertEquals("QWFCYkNjRGRFZUZmR2dIaElpSmpLa0xsTW1Obk9vUHBRcVJyU3NUdFV1VnZXd1h4WXlaeg==",new String(Base64.encode("AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz".getBytes()))); - assertEquals("eyAwLCAxLCAyLCAzLCA0LCA1LCA2LCA3LCA4LCA5IH0=",new String(Base64.encode("{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }".getBytes()))); - assertEquals("eHl6enkh",new String(Base64.encode("xyzzy!".getBytes()))); + +assertEquals("VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wZWQgb3ZlciB0aGUgbGF6eSBkb2dzLg==", + Base64.encode("The quick brown fox jumped over the lazy dogs.")); + +assertEquals("SXQgd2FzIHRoZSBiZXN0IG9mIHRpbWVzLCBpdCB3YXMgdGhlIHdvcnN0IG9mIHRpbWVzLg==", + Base64.encode("It was the best of times, it was the worst of +times.")); + assertEquals("aHR0cDovL2pha2FydGEuYXBhY2hlLm9yZy9jb21tbW9ucw==", + Base64.encode("http://jakarta.apache.org/commmons")); + +assertEquals("QWFCYkNjRGRFZUZmR2dIaElpSmpLa0xsTW1Obk9vUHBRcVJyU3NUdFV1VnZXd1h4WXlaeg==", + +Base64.encode("AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz")); + assertEquals("eyAwLCAxLCAyLCAzLCA0LCA1LCA2LCA3LCA4LCA5IH0=", + Base64.encode("{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }")); + assertEquals("eHl6enkh", + Base64.encode("xyzzy!")); } public void testKnownDecodings() { - assertEquals("The quick brown fox jumped over the lazy dogs.",new String(Base64.decode("VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wZWQgb3ZlciB0aGUgbGF6eSBkb2dzLg==".getBytes()))); - assertEquals("It was the best of times, it was the worst of times.",new String(Base64.decode("SXQgd2FzIHRoZSBiZXN0IG9mIHRpbWVzLCBpdCB3YXMgdGhlIHdvcnN0IG9mIHRpbWVzLg==".getBytes()))); - assertEquals("http://jakarta.apache.org/commmons",new String(Base64.decode("aHR0cDovL2pha2FydGEuYXBhY2hlLm9yZy9jb21tbW9ucw==".getBytes()))); - assertEquals("AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz",new String(Base64.decode("QWFCYkNjRGRFZUZmR2dIaElpSmpLa0xsTW1Obk9vUHBRcVJyU3NUdFV1VnZXd1h4WXlaeg==".getBytes()))); - assertEquals("{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }",new String(Base64.decode("eyAwLCAxLCAyLCAzLCA0LCA1LCA2LCA3LCA4LCA5IH0=".getBytes()))); - assertEquals("xyzzy!",new String(Base64.decode("eHl6enkh".getBytes()))); + assertEquals("The quick brown fox jumped over the lazy dogs.", + +Base64.decode("VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wZWQgb3ZlciB0aGUgbGF6eSBkb2dzLg==")); + assertEquals("It was the best of times, it was the worst of times.", + +Base64.decode("SXQgd2FzIHRoZSBiZXN0IG9mIHRpbWVzLCBpdCB3YXMgdGhlIHdvcnN0IG9mIHRpbWVzLg==")); + assertEquals("http://jakarta.apache.org/commmons", + +Base64.decode("aHR0cDovL2pha2FydGEuYXBhY2hlLm9yZy9jb21tbW9ucw==")); + assertEquals("AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz", + +Base64.decode("QWFCYkNjRGRFZUZmR2dIaElpSmpLa0xsTW1Obk9vUHBRcVJyU3NUdFV1VnZXd1h4WXlaeg==")); + assertEquals("{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }", + Base64.decode("eyAwLCAxLCAyLCAzLCA0LCA1LCA2LCA3LCA4LCA5IH0=")); + assertEquals("xyzzy!", + Base64.decode("eHl6enkh")); } // -------------------------------------------------------- Private Methods
-- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>