You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2009/08/01 10:48:05 UTC
svn commit: r799815 - in /commons/proper/codec/trunk/src:
java/org/apache/commons/codec/binary/Base64.java
test/org/apache/commons/codec/binary/Base64Test.java
Author: ggregory
Date: Sat Aug 1 08:48:04 2009
New Revision: 799815
URL: http://svn.apache.org/viewvc?rev=799815&view=rev
Log:
[CODEC-78] Base64: Improve Code Coverage. Patch applied: https://issues.apache.org/jira/secure/attachment/12415223/codec78-evenMoreCoverage.patch
Modified:
commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/Base64.java
commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base64Test.java
Modified: commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/Base64.java
URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/Base64.java?rev=799815&r1=799814&r2=799815&view=diff
==============================================================================
--- commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/Base64.java (original)
+++ commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/Base64.java Sat Aug 1 08:48:04 2009
@@ -314,6 +314,10 @@
* @since 1.4
*/
public Base64(int lineLength, byte[] lineSeparator, boolean urlSafe) {
+ if (lineSeparator == null) {
+ lineLength = 0; // disable chunk-separating
+ lineSeparator = CHUNK_SEPARATOR; // this just gets ignored
+ }
this.lineLength = lineLength > 0 ? (lineLength / 4) * 4 : 0;
this.lineSeparator = new byte[lineSeparator.length];
System.arraycopy(lineSeparator, 0, this.lineSeparator, 0, lineSeparator.length);
@@ -957,7 +961,7 @@
if (mod != 0) {
len += 4 - mod;
}
- if (chunkSize > 0 && chunkSeparator != null) {
+ if (chunkSize > 0) {
boolean lenChunksPerfectly = len % chunkSize == 0;
len += (len / chunkSize) * chunkSeparator.length;
if (!lenChunksPerfectly) {
Modified: commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base64Test.java
URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base64Test.java?rev=799815&r1=799814&r2=799815&view=diff
==============================================================================
--- commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base64Test.java (original)
+++ commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base64Test.java Sat Aug 1 08:48:04 2009
@@ -63,6 +63,21 @@
byte[] encodedBytes = Base64.encodeBase64(StringUtils.getBytesUtf8(content));
encodedContent = StringUtils.newStringUtf8(encodedBytes);
assertTrue("encoding hello world", encodedContent.equals("SGVsbG8gV29ybGQ="));
+
+ Base64 b64 = new Base64(76, null); // null lineSeparator same as saying no-chunking
+ encodedBytes = b64.encode(StringUtils.getBytesUtf8(content));
+ encodedContent = StringUtils.newStringUtf8(encodedBytes);
+ assertTrue("encoding hello world", encodedContent.equals("SGVsbG8gV29ybGQ="));
+
+ b64 = new Base64(0, null); // null lineSeparator same as saying no-chunking
+ encodedBytes = b64.encode(StringUtils.getBytesUtf8(content));
+ encodedContent = StringUtils.newStringUtf8(encodedBytes);
+ assertTrue("encoding hello world", encodedContent.equals("SGVsbG8gV29ybGQ="));
+
+ // bogus characters to decode (to skip actually)
+ byte[] decode = b64.decode("SGVsbG{éééééé}8gV29ybGQ=");
+ String decodeString = StringUtils.newStringUtf8(decode);
+ assertTrue("decode hello world", decodeString.equals("Hello World"));
}
/**