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 2010/03/27 02:59:25 UTC
svn commit: r928121 - in /commons/proper/codec/trunk/src:
java/org/apache/commons/codec/binary/ test/org/apache/commons/codec/binary/
Author: ggregory
Date: Sat Mar 27 01:59:24 2010
New Revision: 928121
URL: http://svn.apache.org/viewvc?rev=928121&view=rev
Log:
Use constants in code for MIME and PEM chunk size instead of magic numbers.
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/Base64InputStreamTest.java
commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base64OutputStreamTest.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=928121&r1=928120&r2=928121&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 Mar 27 01:59:24 2010
@@ -56,7 +56,7 @@ public class Base64 implements BinaryEnc
private static final int DEFAULT_BUFFER_SIZE = 8192;
/**
- * Chunk size per RFC 2045 section 6.8.
+ * MIME chunk size per RFC 2045 section 6.8.
*
* <p>
* 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 BinaryEnc
*
* @see <a href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045 section 6.8</a>
*/
- static final int CHUNK_SIZE = 76;
+ public static final int MIME_CHUNK_SIZE = 76;
+
+ /**
+ * PEM chunk size per RFC 1421 section 4.3.2.4.
+ *
+ * <p>
+ * The {@value} character limit does not count the trailing CRLF, but counts all other characters, including any
+ * equal signs.
+ * </p>
+ *
+ * @see <a href="http://tools.ietf.org/html/rfc1421">RFC 1421 section 4.3.2.4</a>
+ */
+ public static final int PEM_CHUNK_SIZE = 64;
/**
* Chunk separator per RFC 2045 section 2.1.
@@ -241,7 +253,7 @@ public class Base64 implements BinaryEnc
* @since 1.4
*/
public Base64(boolean urlSafe) {
- this(CHUNK_SIZE, CHUNK_SEPARATOR, urlSafe);
+ this(MIME_CHUNK_SIZE, CHUNK_SEPARATOR, urlSafe);
}
/**
@@ -806,7 +818,7 @@ public class Base64 implements BinaryEnc
return binaryData;
}
- long len = getEncodeLength(binaryData, CHUNK_SIZE, CHUNK_SEPARATOR);
+ long len = getEncodeLength(binaryData, MIME_CHUNK_SIZE, CHUNK_SEPARATOR);
if (len > maxResultSize) {
throw new IllegalArgumentException("Input array too big, the output array would be bigger (" +
len +
Modified: commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base64InputStreamTest.java
URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base64InputStreamTest.java?rev=928121&r1=928120&r2=928121&view=diff
==============================================================================
--- commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base64InputStreamTest.java (original)
+++ commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base64InputStreamTest.java Sat Mar 27 01:59:24 2010
@@ -52,11 +52,25 @@ public class Base64InputStreamTest exten
* @throws Exception
* for some failure scenarios.
*/
- public void testBase64EmptyInputStream() throws Exception {
+ public void testBase64EmptyInputStreamMimeChuckSize() throws Exception {
+ testBase64EmptyInputStream(Base64.MIME_CHUNK_SIZE);
+ }
+
+ /**
+ * Tests the Base64InputStream implementation against empty input.
+ *
+ * @throws Exception
+ * for some failure scenarios.
+ */
+ public void testBase64EmptyInputStreamPemChuckSize() throws Exception {
+ testBase64EmptyInputStream(Base64.PEM_CHUNK_SIZE);
+ }
+
+ private void testBase64EmptyInputStream(int chuckSize) throws Exception {
byte[] emptyEncoded = new byte[0];
byte[] emptyDecoded = new byte[0];
- testByteByByte(emptyEncoded, emptyDecoded, 76, CRLF);
- testByChunk(emptyEncoded, emptyDecoded, 76, CRLF);
+ testByteByByte(emptyEncoded, emptyDecoded, chuckSize, CRLF);
+ testByChunk(emptyEncoded, emptyDecoded, chuckSize, CRLF);
}
/**
@@ -69,17 +83,17 @@ public class Base64InputStreamTest exten
// Hello World test.
byte[] encoded = StringUtils.getBytesUtf8("SGVsbG8gV29ybGQ=\r\n");
byte[] decoded = StringUtils.getBytesUtf8(STRING_FIXTURE);
- testByChunk(encoded, decoded, 76, CRLF);
+ testByChunk(encoded, decoded, Base64.MIME_CHUNK_SIZE, CRLF);
// Single Byte test.
encoded = StringUtils.getBytesUtf8("AA==\r\n");
decoded = new byte[]{(byte) 0};
- testByChunk(encoded, decoded, 76, CRLF);
+ testByChunk(encoded, decoded, Base64.MIME_CHUNK_SIZE, CRLF);
// OpenSSL interop test.
encoded = StringUtils.getBytesUtf8(Base64TestData.ENCODED_64_CHARS_PER_LINE);
decoded = Base64TestData.DECODED;
- testByChunk(encoded, decoded, 64, LF);
+ testByChunk(encoded, decoded, Base64.PEM_CHUNK_SIZE, LF);
// Single Line test.
String singleLine = Base64TestData.ENCODED_64_CHARS_PER_LINE.replaceAll("\n", "");
@@ -106,17 +120,17 @@ public class Base64InputStreamTest exten
// Hello World test.
byte[] encoded = StringUtils.getBytesUtf8("SGVsbG8gV29ybGQ=\r\n");
byte[] decoded = StringUtils.getBytesUtf8(STRING_FIXTURE);
- testByteByByte(encoded, decoded, 76, CRLF);
+ testByteByByte(encoded, decoded, Base64.MIME_CHUNK_SIZE, CRLF);
// Single Byte test.
encoded = StringUtils.getBytesUtf8("AA==\r\n");
decoded = new byte[]{(byte) 0};
- testByteByByte(encoded, decoded, 76, CRLF);
+ testByteByByte(encoded, decoded, Base64.MIME_CHUNK_SIZE, CRLF);
// OpenSSL interop test.
encoded = StringUtils.getBytesUtf8(Base64TestData.ENCODED_64_CHARS_PER_LINE);
decoded = Base64TestData.DECODED;
- testByteByByte(encoded, decoded, 64, LF);
+ testByteByByte(encoded, decoded, Base64.PEM_CHUNK_SIZE, LF);
// Single Line test.
String singleLine = Base64TestData.ENCODED_64_CHARS_PER_LINE.replaceAll("\n", "");
Modified: commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base64OutputStreamTest.java
URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base64OutputStreamTest.java?rev=928121&r1=928120&r2=928121&view=diff
==============================================================================
--- commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base64OutputStreamTest.java (original)
+++ commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base64OutputStreamTest.java Sat Mar 27 01:59:24 2010
@@ -52,11 +52,25 @@ public class Base64OutputStreamTest exte
* @throws Exception
* for some failure scenarios.
*/
- public void testBase64EmptyOutputStream() throws Exception {
+ public void testBase64EmptyOutputStreamMimeChunkSize() throws Exception {
+ testBase64EmptyOutputStream(Base64.MIME_CHUNK_SIZE);
+ }
+
+ /**
+ * Test the Base64OutputStream implementation against empty input.
+ *
+ * @throws Exception
+ * for some failure scenarios.
+ */
+ public void testBase64EmptyOutputStreamPemChunkSize() throws Exception {
+ testBase64EmptyOutputStream(Base64.PEM_CHUNK_SIZE);
+ }
+
+ private void testBase64EmptyOutputStream(int chunkSize) throws Exception {
byte[] emptyEncoded = new byte[0];
byte[] emptyDecoded = new byte[0];
- testByteByByte(emptyEncoded, emptyDecoded, 76, CRLF);
- testByChunk(emptyEncoded, emptyDecoded, 76, CRLF);
+ testByteByByte(emptyEncoded, emptyDecoded, chunkSize, CRLF);
+ testByChunk(emptyEncoded, emptyDecoded, chunkSize, CRLF);
}
/**
@@ -69,17 +83,17 @@ public class Base64OutputStreamTest exte
// Hello World test.
byte[] encoded = StringUtils.getBytesUtf8("SGVsbG8gV29ybGQ=\r\n");
byte[] decoded = StringUtils.getBytesUtf8(STRING_FIXTURE);
- testByChunk(encoded, decoded, 76, CRLF);
+ testByChunk(encoded, decoded, Base64.MIME_CHUNK_SIZE, CRLF);
// Single Byte test.
encoded = StringUtils.getBytesUtf8("AA==\r\n");
decoded = new byte[]{(byte) 0};
- testByChunk(encoded, decoded, 76, CRLF);
+ testByChunk(encoded, decoded, Base64.MIME_CHUNK_SIZE, CRLF);
// OpenSSL interop test.
encoded = StringUtils.getBytesUtf8(Base64TestData.ENCODED_64_CHARS_PER_LINE);
decoded = Base64TestData.DECODED;
- testByChunk(encoded, decoded, 64, LF);
+ testByChunk(encoded, decoded, Base64.PEM_CHUNK_SIZE, LF);
// Single Line test.
String singleLine = Base64TestData.ENCODED_64_CHARS_PER_LINE.replaceAll("\n", "");
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=928121&r1=928120&r2=928121&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 Mar 27 01:59:24 2010
@@ -64,7 +64,7 @@ public class Base64Test extends TestCase
encodedContent = StringUtils.newStringUtf8(encodedBytes);
assertTrue("encoding hello world", encodedContent.equals("SGVsbG8gV29ybGQ="));
- Base64 b64 = new Base64(76, null); // null lineSeparator same as saying no-chunking
+ Base64 b64 = new Base64(Base64.MIME_CHUNK_SIZE, 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="));
@@ -507,7 +507,14 @@ public class Base64Test extends TestCase
* Tests RFC 2045 section 6.8 chuck size definition.
*/
public void testRfc2045Section6Dot8ChunkSizeDefinition() {
- assertEquals(76, Base64.CHUNK_SIZE);
+ assertEquals(76, Base64.MIME_CHUNK_SIZE);
+ }
+
+ /**
+ * Tests RFC 1421 section 4.3.2.4 chuck size definition.
+ */
+ public void testRfc1421Section6Dot8ChunkSizeDefinition() {
+ assertEquals(64, Base64.PEM_CHUNK_SIZE);
}
public void testSingletons() {