Skip to content

Commit ffd08a4

Browse files
committed
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/codec/trunk@799815 13f79535-47bb-0310-9956-ffa450edef68
1 parent 59ebd4d commit ffd08a4

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

src/java/org/apache/commons/codec/binary/Base64.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,10 @@ public Base64(int lineLength, byte[] lineSeparator) {
314314
* @since 1.4
315315
*/
316316
public Base64(int lineLength, byte[] lineSeparator, boolean urlSafe) {
317+
if (lineSeparator == null) {
318+
lineLength = 0; // disable chunk-separating
319+
lineSeparator = CHUNK_SEPARATOR; // this just gets ignored
320+
}
317321
this.lineLength = lineLength > 0 ? (lineLength / 4) * 4 : 0;
318322
this.lineSeparator = new byte[lineSeparator.length];
319323
System.arraycopy(lineSeparator, 0, this.lineSeparator, 0, lineSeparator.length);
@@ -957,7 +961,7 @@ private static long getEncodeLength(byte[] pArray, int chunkSize, byte[] chunkSe
957961
if (mod != 0) {
958962
len += 4 - mod;
959963
}
960-
if (chunkSize > 0 && chunkSeparator != null) {
964+
if (chunkSize > 0) {
961965
boolean lenChunksPerfectly = len % chunkSize == 0;
962966
len += (len / chunkSize) * chunkSeparator.length;
963967
if (!lenChunksPerfectly) {

src/test/org/apache/commons/codec/binary/Base64Test.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,21 @@ public void testBase64() {
6363
byte[] encodedBytes = Base64.encodeBase64(StringUtils.getBytesUtf8(content));
6464
encodedContent = StringUtils.newStringUtf8(encodedBytes);
6565
assertTrue("encoding hello world", encodedContent.equals("SGVsbG8gV29ybGQ="));
66+
67+
Base64 b64 = new Base64(76, null); // null lineSeparator same as saying no-chunking
68+
encodedBytes = b64.encode(StringUtils.getBytesUtf8(content));
69+
encodedContent = StringUtils.newStringUtf8(encodedBytes);
70+
assertTrue("encoding hello world", encodedContent.equals("SGVsbG8gV29ybGQ="));
71+
72+
b64 = new Base64(0, null); // null lineSeparator same as saying no-chunking
73+
encodedBytes = b64.encode(StringUtils.getBytesUtf8(content));
74+
encodedContent = StringUtils.newStringUtf8(encodedBytes);
75+
assertTrue("encoding hello world", encodedContent.equals("SGVsbG8gV29ybGQ="));
76+
77+
// bogus characters to decode (to skip actually)
78+
byte[] decode = b64.decode("SGVsbG{éééééé}8gV29ybGQ=");
79+
String decodeString = StringUtils.newStringUtf8(decode);
80+
assertTrue("decode hello world", decodeString.equals("Hello World"));
6681
}
6782

6883
/**

0 commit comments

Comments
 (0)