Skip to content

Commit 00da592

Browse files
committed
Use constants in code for MIME and PEM chunk size instead of magic numbers.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/codec/trunk@928121 13f79535-47bb-0310-9956-ffa450edef68
1 parent 9535a94 commit 00da592

4 files changed

Lines changed: 68 additions & 21 deletions

File tree

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public class Base64 implements BinaryEncoder, BinaryDecoder {
5656
private static final int DEFAULT_BUFFER_SIZE = 8192;
5757

5858
/**
59-
* Chunk size per RFC 2045 section 6.8.
59+
* MIME chunk size per RFC 2045 section 6.8.
6060
*
6161
* <p>
6262
* The {@value} character limit does not count the trailing CRLF, but counts all other characters, including any
@@ -65,7 +65,19 @@ public class Base64 implements BinaryEncoder, BinaryDecoder {
6565
*
6666
* @see <a href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045 section 6.8</a>
6767
*/
68-
static final int CHUNK_SIZE = 76;
68+
public static final int MIME_CHUNK_SIZE = 76;
69+
70+
/**
71+
* PEM chunk size per RFC 1421 section 4.3.2.4.
72+
*
73+
* <p>
74+
* The {@value} character limit does not count the trailing CRLF, but counts all other characters, including any
75+
* equal signs.
76+
* </p>
77+
*
78+
* @see <a href="http://tools.ietf.org/html/rfc1421">RFC 1421 section 4.3.2.4</a>
79+
*/
80+
public static final int PEM_CHUNK_SIZE = 64;
6981

7082
/**
7183
* Chunk separator per RFC 2045 section 2.1.
@@ -241,7 +253,7 @@ public Base64() {
241253
* @since 1.4
242254
*/
243255
public Base64(boolean urlSafe) {
244-
this(CHUNK_SIZE, CHUNK_SEPARATOR, urlSafe);
256+
this(MIME_CHUNK_SIZE, CHUNK_SEPARATOR, urlSafe);
245257
}
246258

247259
/**
@@ -806,7 +818,7 @@ public static byte[] encodeBase64(byte[] binaryData, boolean isChunked, boolean
806818
return binaryData;
807819
}
808820

809-
long len = getEncodeLength(binaryData, CHUNK_SIZE, CHUNK_SEPARATOR);
821+
long len = getEncodeLength(binaryData, MIME_CHUNK_SIZE, CHUNK_SEPARATOR);
810822
if (len > maxResultSize) {
811823
throw new IllegalArgumentException("Input array too big, the output array would be bigger (" +
812824
len +

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

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,25 @@ public Base64InputStreamTest(String name) {
5252
* @throws Exception
5353
* for some failure scenarios.
5454
*/
55-
public void testBase64EmptyInputStream() throws Exception {
55+
public void testBase64EmptyInputStreamMimeChuckSize() throws Exception {
56+
testBase64EmptyInputStream(Base64.MIME_CHUNK_SIZE);
57+
}
58+
59+
/**
60+
* Tests the Base64InputStream implementation against empty input.
61+
*
62+
* @throws Exception
63+
* for some failure scenarios.
64+
*/
65+
public void testBase64EmptyInputStreamPemChuckSize() throws Exception {
66+
testBase64EmptyInputStream(Base64.PEM_CHUNK_SIZE);
67+
}
68+
69+
private void testBase64EmptyInputStream(int chuckSize) throws Exception {
5670
byte[] emptyEncoded = new byte[0];
5771
byte[] emptyDecoded = new byte[0];
58-
testByteByByte(emptyEncoded, emptyDecoded, 76, CRLF);
59-
testByChunk(emptyEncoded, emptyDecoded, 76, CRLF);
72+
testByteByByte(emptyEncoded, emptyDecoded, chuckSize, CRLF);
73+
testByChunk(emptyEncoded, emptyDecoded, chuckSize, CRLF);
6074
}
6175

6276
/**
@@ -69,17 +83,17 @@ public void testBase64InputStreamByChunk() throws Exception {
6983
// Hello World test.
7084
byte[] encoded = StringUtils.getBytesUtf8("SGVsbG8gV29ybGQ=\r\n");
7185
byte[] decoded = StringUtils.getBytesUtf8(STRING_FIXTURE);
72-
testByChunk(encoded, decoded, 76, CRLF);
86+
testByChunk(encoded, decoded, Base64.MIME_CHUNK_SIZE, CRLF);
7387

7488
// Single Byte test.
7589
encoded = StringUtils.getBytesUtf8("AA==\r\n");
7690
decoded = new byte[]{(byte) 0};
77-
testByChunk(encoded, decoded, 76, CRLF);
91+
testByChunk(encoded, decoded, Base64.MIME_CHUNK_SIZE, CRLF);
7892

7993
// OpenSSL interop test.
8094
encoded = StringUtils.getBytesUtf8(Base64TestData.ENCODED_64_CHARS_PER_LINE);
8195
decoded = Base64TestData.DECODED;
82-
testByChunk(encoded, decoded, 64, LF);
96+
testByChunk(encoded, decoded, Base64.PEM_CHUNK_SIZE, LF);
8397

8498
// Single Line test.
8599
String singleLine = Base64TestData.ENCODED_64_CHARS_PER_LINE.replaceAll("\n", "");
@@ -106,17 +120,17 @@ public void testBase64InputStreamByteByByte() throws Exception {
106120
// Hello World test.
107121
byte[] encoded = StringUtils.getBytesUtf8("SGVsbG8gV29ybGQ=\r\n");
108122
byte[] decoded = StringUtils.getBytesUtf8(STRING_FIXTURE);
109-
testByteByByte(encoded, decoded, 76, CRLF);
123+
testByteByByte(encoded, decoded, Base64.MIME_CHUNK_SIZE, CRLF);
110124

111125
// Single Byte test.
112126
encoded = StringUtils.getBytesUtf8("AA==\r\n");
113127
decoded = new byte[]{(byte) 0};
114-
testByteByByte(encoded, decoded, 76, CRLF);
128+
testByteByByte(encoded, decoded, Base64.MIME_CHUNK_SIZE, CRLF);
115129

116130
// OpenSSL interop test.
117131
encoded = StringUtils.getBytesUtf8(Base64TestData.ENCODED_64_CHARS_PER_LINE);
118132
decoded = Base64TestData.DECODED;
119-
testByteByByte(encoded, decoded, 64, LF);
133+
testByteByByte(encoded, decoded, Base64.PEM_CHUNK_SIZE, LF);
120134

121135
// Single Line test.
122136
String singleLine = Base64TestData.ENCODED_64_CHARS_PER_LINE.replaceAll("\n", "");

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

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,25 @@ public Base64OutputStreamTest(String name) {
5252
* @throws Exception
5353
* for some failure scenarios.
5454
*/
55-
public void testBase64EmptyOutputStream() throws Exception {
55+
public void testBase64EmptyOutputStreamMimeChunkSize() throws Exception {
56+
testBase64EmptyOutputStream(Base64.MIME_CHUNK_SIZE);
57+
}
58+
59+
/**
60+
* Test the Base64OutputStream implementation against empty input.
61+
*
62+
* @throws Exception
63+
* for some failure scenarios.
64+
*/
65+
public void testBase64EmptyOutputStreamPemChunkSize() throws Exception {
66+
testBase64EmptyOutputStream(Base64.PEM_CHUNK_SIZE);
67+
}
68+
69+
private void testBase64EmptyOutputStream(int chunkSize) throws Exception {
5670
byte[] emptyEncoded = new byte[0];
5771
byte[] emptyDecoded = new byte[0];
58-
testByteByByte(emptyEncoded, emptyDecoded, 76, CRLF);
59-
testByChunk(emptyEncoded, emptyDecoded, 76, CRLF);
72+
testByteByByte(emptyEncoded, emptyDecoded, chunkSize, CRLF);
73+
testByChunk(emptyEncoded, emptyDecoded, chunkSize, CRLF);
6074
}
6175

6276
/**
@@ -69,17 +83,17 @@ public void testBase64OutputStreamByChunk() throws Exception {
6983
// Hello World test.
7084
byte[] encoded = StringUtils.getBytesUtf8("SGVsbG8gV29ybGQ=\r\n");
7185
byte[] decoded = StringUtils.getBytesUtf8(STRING_FIXTURE);
72-
testByChunk(encoded, decoded, 76, CRLF);
86+
testByChunk(encoded, decoded, Base64.MIME_CHUNK_SIZE, CRLF);
7387

7488
// Single Byte test.
7589
encoded = StringUtils.getBytesUtf8("AA==\r\n");
7690
decoded = new byte[]{(byte) 0};
77-
testByChunk(encoded, decoded, 76, CRLF);
91+
testByChunk(encoded, decoded, Base64.MIME_CHUNK_SIZE, CRLF);
7892

7993
// OpenSSL interop test.
8094
encoded = StringUtils.getBytesUtf8(Base64TestData.ENCODED_64_CHARS_PER_LINE);
8195
decoded = Base64TestData.DECODED;
82-
testByChunk(encoded, decoded, 64, LF);
96+
testByChunk(encoded, decoded, Base64.PEM_CHUNK_SIZE, LF);
8397

8498
// Single Line test.
8599
String singleLine = Base64TestData.ENCODED_64_CHARS_PER_LINE.replaceAll("\n", "");

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public void testBase64() {
6464
encodedContent = StringUtils.newStringUtf8(encodedBytes);
6565
assertTrue("encoding hello world", encodedContent.equals("SGVsbG8gV29ybGQ="));
6666

67-
Base64 b64 = new Base64(76, null); // null lineSeparator same as saying no-chunking
67+
Base64 b64 = new Base64(Base64.MIME_CHUNK_SIZE, null); // null lineSeparator same as saying no-chunking
6868
encodedBytes = b64.encode(StringUtils.getBytesUtf8(content));
6969
encodedContent = StringUtils.newStringUtf8(encodedBytes);
7070
assertTrue("encoding hello world", encodedContent.equals("SGVsbG8gV29ybGQ="));
@@ -507,7 +507,14 @@ public void testRfc2045Section2Dot1CrLfDefinition() {
507507
* Tests RFC 2045 section 6.8 chuck size definition.
508508
*/
509509
public void testRfc2045Section6Dot8ChunkSizeDefinition() {
510-
assertEquals(76, Base64.CHUNK_SIZE);
510+
assertEquals(76, Base64.MIME_CHUNK_SIZE);
511+
}
512+
513+
/**
514+
* Tests RFC 1421 section 4.3.2.4 chuck size definition.
515+
*/
516+
public void testRfc1421Section6Dot8ChunkSizeDefinition() {
517+
assertEquals(64, Base64.PEM_CHUNK_SIZE);
511518
}
512519

513520
public void testSingletons() {

0 commit comments

Comments
 (0)