This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-codec.git

commit 3d6d334705a6a90899b4646aa888087643ef7c9a
Author: Gary Gregory <[email protected]>
AuthorDate: Tue Apr 16 19:54:23 2024 -0400

    Add Base64 support for a custom padding byte (like Base32)
---
 src/changes/changes.xml                                    |  3 ++-
 src/main/java/org/apache/commons/codec/binary/Base64.java  | 14 +++++++-------
 .../java/org/apache/commons/codec/binary/Base64Test.java   | 14 ++++++++++++++
 3 files changed, 23 insertions(+), 8 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index d8f530dd..024f3d92 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -48,7 +48,8 @@ The <action> type attribute can be add,update,fix,remove.
       <action type="add" dev="ggregory" due-to="Gary Gregory">Add override 
org.apache.commons.codec.language.bm.Rule.PhonemeExpr.size().</action>
       <action type="add" dev="ggregory" due-to="Chris Kocel, Gary Gregory">Add 
support for Base64 custom alphabets #266.</action>
       <action type="add" dev="ggregory" due-to="Gary Gregory">Add 
Base64.Builder (allows custom alphabets).</action> 
-      <action type="add" dev="ggregory" due-to="Gary Gregory">Add 
Base32.Builder (allows custom alphabets).</action> 
+      <action type="add" dev="ggregory" due-to="Gary Gregory">Add 
Base32.Builder (allows custom alphabets).</action>
+      <action type="add" dev="ggregory" due-to="Gary Gregory">Add Base64 
support for a custom padding byte (like Base32).</action> 
       <!-- FIX -->
       <action type="fix" dev="ggregory" due-to="Gary Gregory">Optimize memory 
allocation in PhoneticEngine.</action>
       <action type="fix" dev="ggregory" due-to="Gary Gregory">BCodec and 
QCodec encode() methods throw UnsupportedCharsetException instead of 
EncoderException.</action>
diff --git a/src/main/java/org/apache/commons/codec/binary/Base64.java 
b/src/main/java/org/apache/commons/codec/binary/Base64.java
index cd55201a..57a65053 100644
--- a/src/main/java/org/apache/commons/codec/binary/Base64.java
+++ b/src/main/java/org/apache/commons/codec/binary/Base64.java
@@ -72,7 +72,7 @@ public class Base64 extends BaseNCodec {
 
         @Override
         public Base64 get() {
-            return new Base64(getLineLength(), getLineSeparator(), 
getEncodeTable(), getDecodingPolicy());
+            return new Base64(getLineLength(), getLineSeparator(), 
getPadding(), getEncodeTable(), getDecodingPolicy());
         }
 
         /**
@@ -597,7 +597,7 @@ public class Base64 extends BaseNCodec {
      * @since 1.4
      */
     public Base64(final int lineLength, final byte[] lineSeparator, final 
boolean urlSafe) {
-        this(lineLength, lineSeparator, toUrlSafeEncodeTable(urlSafe), 
DECODING_POLICY_DEFAULT);
+        this(lineLength, lineSeparator, PAD_DEFAULT, 
toUrlSafeEncodeTable(urlSafe), DECODING_POLICY_DEFAULT);
     }
 
     /**
@@ -628,9 +628,8 @@ public class Base64 extends BaseNCodec {
      *             Thrown when the {@code lineSeparator} contains Base64 
characters.
      * @since 1.15
      */
-    public Base64(final int lineLength, final byte[] lineSeparator, final 
boolean urlSafe,
-                  final CodecPolicy decodingPolicy) {
-        this(lineLength, lineSeparator, toUrlSafeEncodeTable(urlSafe), 
decodingPolicy);
+    public Base64(final int lineLength, final byte[] lineSeparator, final 
boolean urlSafe, final CodecPolicy decodingPolicy) {
+        this(lineLength, lineSeparator, PAD_DEFAULT, 
toUrlSafeEncodeTable(urlSafe), decodingPolicy);
     }
 
     /**
@@ -652,14 +651,15 @@ public class Base64 extends BaseNCodec {
      *            decoding.
      * @param lineSeparator
      *            Each line of encoded data will end with this sequence of 
bytes.
+     * @param padding padding byte.
      * @param encodeTable
      *            The manual encodeTable - a byte array of 64 chars.
      * @param decodingPolicy The decoding policy.
      * @throws IllegalArgumentException
      *             Thrown when the {@code lineSeparator} contains Base64 
characters.
      */
-    private Base64(final int lineLength, final byte[] lineSeparator, final 
byte[] encodeTable, final CodecPolicy decodingPolicy) {
-        super(BYTES_PER_UNENCODED_BLOCK, BYTES_PER_ENCODED_BLOCK, lineLength, 
toLength(lineSeparator), PAD_DEFAULT, decodingPolicy);
+    private Base64(final int lineLength, final byte[] lineSeparator, final 
byte padding, final byte[] encodeTable, final CodecPolicy decodingPolicy) {
+        super(BYTES_PER_UNENCODED_BLOCK, BYTES_PER_ENCODED_BLOCK, lineLength, 
toLength(lineSeparator), padding, decodingPolicy);
         this.encodeTable = Objects.requireNonNull(encodeTable, "encodeTable");
         if (encodeTable == STANDARD_ENCODE_TABLE || encodeTable == 
URL_SAFE_ENCODE_TABLE) {
             decodeTable = DECODE_TABLE;
diff --git a/src/test/java/org/apache/commons/codec/binary/Base64Test.java 
b/src/test/java/org/apache/commons/codec/binary/Base64Test.java
index 37100824..e801c919 100644
--- a/src/test/java/org/apache/commons/codec/binary/Base64Test.java
+++ b/src/test/java/org/apache/commons/codec/binary/Base64Test.java
@@ -242,6 +242,20 @@ public class Base64Test {
         assertEquals("Zm94\r\n", 
Base64.builder().setLineLength(4).get().encodeToString("fox".getBytes(CHARSET_UTF8)));
     }
 
+    @Test
+    public void testBuilderPadingByte() {
+        assertNull(Base64.builder().get().getLineSeparator());
+        
assertNull(Base64.builder().setLineSeparator(BaseNCodec.CHUNK_SEPARATOR).get().getLineSeparator());
+        assertArrayEquals(BaseNCodec.CHUNK_SEPARATOR, 
Base64.builder().setLineLength(4).setLineSeparator(BaseNCodec.CHUNK_SEPARATOR).get().getLineSeparator());
+        assertArrayEquals(BaseNCodec.CHUNK_SEPARATOR, 
Base64.builder().setLineLength(4).setLineSeparator(null).get().getLineSeparator());
+        assertArrayEquals(BaseNCodec.CHUNK_SEPARATOR, 
Base64.builder().setLineLength(10).setLineSeparator(null).get().getLineSeparator());
+        
assertNull(Base64.builder().setLineLength(-1).setLineSeparator(null).get().getLineSeparator());
+        
assertNull(Base64.builder().setLineLength(0).setLineSeparator(null).get().getLineSeparator());
+        assertArrayEquals(new byte[] { 1 }, 
Base64.builder().setLineLength(4).setLineSeparator((byte) 
1).get().getLineSeparator());
+        assertEquals("VGhlIGJyb3duIGZveA==", 
Base64.builder().get().encodeToString("The brown fox".getBytes(CHARSET_UTF8)));
+        assertEquals("VGhlIGJyb3duIGZveA__", 
Base64.builder().setPadding((byte) '_').get().encodeToString("The brown 
fox".getBytes(CHARSET_UTF8)));
+    }
+
     @Test
     public void testBuilderUrlSafe() {
         assertFalse(Base64.builder().get().isUrlSafe());

Reply via email to