Skip to content

Commit 3d6d334

Browse files
committed
Add Base64 support for a custom padding byte (like Base32)
1 parent 4791d3b commit 3d6d334

3 files changed

Lines changed: 23 additions & 8 deletions

File tree

src/changes/changes.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ The <action> type attribute can be add,update,fix,remove.
4848
<action type="add" dev="ggregory" due-to="Gary Gregory">Add override org.apache.commons.codec.language.bm.Rule.PhonemeExpr.size().</action>
4949
<action type="add" dev="ggregory" due-to="Chris Kocel, Gary Gregory">Add support for Base64 custom alphabets #266.</action>
5050
<action type="add" dev="ggregory" due-to="Gary Gregory">Add Base64.Builder (allows custom alphabets).</action>
51-
<action type="add" dev="ggregory" due-to="Gary Gregory">Add Base32.Builder (allows custom alphabets).</action>
51+
<action type="add" dev="ggregory" due-to="Gary Gregory">Add Base32.Builder (allows custom alphabets).</action>
52+
<action type="add" dev="ggregory" due-to="Gary Gregory">Add Base64 support for a custom padding byte (like Base32).</action>
5253
<!-- FIX -->
5354
<action type="fix" dev="ggregory" due-to="Gary Gregory">Optimize memory allocation in PhoneticEngine.</action>
5455
<action type="fix" dev="ggregory" due-to="Gary Gregory">BCodec and QCodec encode() methods throw UnsupportedCharsetException instead of EncoderException.</action>

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public Builder() {
7272

7373
@Override
7474
public Base64 get() {
75-
return new Base64(getLineLength(), getLineSeparator(), getEncodeTable(), getDecodingPolicy());
75+
return new Base64(getLineLength(), getLineSeparator(), getPadding(), getEncodeTable(), getDecodingPolicy());
7676
}
7777

7878
/**
@@ -597,7 +597,7 @@ public Base64(final int lineLength, final byte[] lineSeparator) {
597597
* @since 1.4
598598
*/
599599
public Base64(final int lineLength, final byte[] lineSeparator, final boolean urlSafe) {
600-
this(lineLength, lineSeparator, toUrlSafeEncodeTable(urlSafe), DECODING_POLICY_DEFAULT);
600+
this(lineLength, lineSeparator, PAD_DEFAULT, toUrlSafeEncodeTable(urlSafe), DECODING_POLICY_DEFAULT);
601601
}
602602

603603
/**
@@ -628,9 +628,8 @@ public Base64(final int lineLength, final byte[] lineSeparator, final boolean ur
628628
* Thrown when the {@code lineSeparator} contains Base64 characters.
629629
* @since 1.15
630630
*/
631-
public Base64(final int lineLength, final byte[] lineSeparator, final boolean urlSafe,
632-
final CodecPolicy decodingPolicy) {
633-
this(lineLength, lineSeparator, toUrlSafeEncodeTable(urlSafe), decodingPolicy);
631+
public Base64(final int lineLength, final byte[] lineSeparator, final boolean urlSafe, final CodecPolicy decodingPolicy) {
632+
this(lineLength, lineSeparator, PAD_DEFAULT, toUrlSafeEncodeTable(urlSafe), decodingPolicy);
634633
}
635634

636635
/**
@@ -652,14 +651,15 @@ public Base64(final int lineLength, final byte[] lineSeparator, final boolean ur
652651
* decoding.
653652
* @param lineSeparator
654653
* Each line of encoded data will end with this sequence of bytes.
654+
* @param padding padding byte.
655655
* @param encodeTable
656656
* The manual encodeTable - a byte array of 64 chars.
657657
* @param decodingPolicy The decoding policy.
658658
* @throws IllegalArgumentException
659659
* Thrown when the {@code lineSeparator} contains Base64 characters.
660660
*/
661-
private Base64(final int lineLength, final byte[] lineSeparator, final byte[] encodeTable, final CodecPolicy decodingPolicy) {
662-
super(BYTES_PER_UNENCODED_BLOCK, BYTES_PER_ENCODED_BLOCK, lineLength, toLength(lineSeparator), PAD_DEFAULT, decodingPolicy);
661+
private Base64(final int lineLength, final byte[] lineSeparator, final byte padding, final byte[] encodeTable, final CodecPolicy decodingPolicy) {
662+
super(BYTES_PER_UNENCODED_BLOCK, BYTES_PER_ENCODED_BLOCK, lineLength, toLength(lineSeparator), padding, decodingPolicy);
663663
this.encodeTable = Objects.requireNonNull(encodeTable, "encodeTable");
664664
if (encodeTable == STANDARD_ENCODE_TABLE || encodeTable == URL_SAFE_ENCODE_TABLE) {
665665
decodeTable = DECODE_TABLE;

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,20 @@ public void testBuilderLineAttributes() {
242242
assertEquals("Zm94\r\n", Base64.builder().setLineLength(4).get().encodeToString("fox".getBytes(CHARSET_UTF8)));
243243
}
244244

245+
@Test
246+
public void testBuilderPadingByte() {
247+
assertNull(Base64.builder().get().getLineSeparator());
248+
assertNull(Base64.builder().setLineSeparator(BaseNCodec.CHUNK_SEPARATOR).get().getLineSeparator());
249+
assertArrayEquals(BaseNCodec.CHUNK_SEPARATOR, Base64.builder().setLineLength(4).setLineSeparator(BaseNCodec.CHUNK_SEPARATOR).get().getLineSeparator());
250+
assertArrayEquals(BaseNCodec.CHUNK_SEPARATOR, Base64.builder().setLineLength(4).setLineSeparator(null).get().getLineSeparator());
251+
assertArrayEquals(BaseNCodec.CHUNK_SEPARATOR, Base64.builder().setLineLength(10).setLineSeparator(null).get().getLineSeparator());
252+
assertNull(Base64.builder().setLineLength(-1).setLineSeparator(null).get().getLineSeparator());
253+
assertNull(Base64.builder().setLineLength(0).setLineSeparator(null).get().getLineSeparator());
254+
assertArrayEquals(new byte[] { 1 }, Base64.builder().setLineLength(4).setLineSeparator((byte) 1).get().getLineSeparator());
255+
assertEquals("VGhlIGJyb3duIGZveA==", Base64.builder().get().encodeToString("The brown fox".getBytes(CHARSET_UTF8)));
256+
assertEquals("VGhlIGJyb3duIGZveA__", Base64.builder().setPadding((byte) '_').get().encodeToString("The brown fox".getBytes(CHARSET_UTF8)));
257+
}
258+
245259
@Test
246260
public void testBuilderUrlSafe() {
247261
assertFalse(Base64.builder().get().isUrlSafe());

0 commit comments

Comments
 (0)