Skip to content

Commit 669e6f4

Browse files
committed
open for different alphabets - add test
1 parent 50aa370 commit 669e6f4

2 files changed

Lines changed: 41 additions & 10 deletions

File tree

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public class Base64 extends BaseNCodec {
7979
* Thanks to "commons" project in ws.apache.org for this code.
8080
* http://svn.apache.org/repos/asf/webservices/commons/trunk/modules/util/
8181
*/
82-
private static final byte[] STANDARD_ENCODE_TABLE = {
82+
public static final byte[] STANDARD_ENCODE_TABLE = {
8383
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
8484
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
8585
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
@@ -92,7 +92,7 @@ public class Base64 extends BaseNCodec {
9292
* changed to - and _ to make the encoded Base64 results more URL-SAFE.
9393
* This table is only used when the Base64's mode is set to URL-SAFE.
9494
*/
95-
private static final byte[] URL_SAFE_ENCODE_TABLE = {
95+
public static final byte[] URL_SAFE_ENCODE_TABLE = {
9696
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
9797
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
9898
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
@@ -275,13 +275,13 @@ public Base64(final int lineLength, final byte[] lineSeparator, final boolean ur
275275
}
276276

277277
/**
278-
* Creates a Base64 codec used for decoding and encoding with non-standard encodingTable-table
278+
* Creates a Base64 codec used for decoding and encoding with non-standard encodeTable-table
279279
*
280-
* @param encodingTable
280+
* @param encodeTable
281281
* The manual encodeTable - a byte array of 64 chars
282282
*/
283-
public Base64(byte[] encodingTable) {
284-
this(0, CHUNK_SEPARATOR, encodingTable);
283+
public Base64(byte[] encodeTable) {
284+
this(0, CHUNK_SEPARATOR, encodeTable);
285285
}
286286

287287
/**
@@ -293,10 +293,10 @@ public Base64(byte[] encodingTable) {
293293
* decoding.
294294
* @param lineSeparator
295295
* Each line of encoded data will end with this sequence of bytes.
296-
* @param encodingTable
296+
* @param encodeTable
297297
* The manual encodeTable - a byte array of 64 chars
298298
*/
299-
public Base64(final int lineLength, final byte[] lineSeparator, byte[] encodingTable) {
299+
public Base64(final int lineLength, final byte[] lineSeparator, byte[] encodeTable) {
300300
super(BYTES_PER_UNENCODED_BLOCK, BYTES_PER_ENCODED_BLOCK,
301301
lineLength,
302302
lineSeparator == null ? 0 : lineSeparator.length);
@@ -320,7 +320,7 @@ public Base64(final int lineLength, final byte[] lineSeparator, byte[] encodingT
320320
this.lineSeparator = null;
321321
}
322322
this.decodeSize = this.encodeSize - 1;
323-
this.encodeTable = encodingTable;
323+
this.encodeTable = encodeTable;
324324
}
325325

326326
/**
@@ -339,7 +339,7 @@ public boolean isUrlSafe() {
339339
* the data to encode, and once with inAvail set to "-1" to alert encoder that EOF has been reached, to flush last
340340
* remaining bytes (if not multiple of 3).
341341
* </p>
342-
* <p><b>Note: no padding is added when encoding using the URL-safe alphabet.</b></p>
342+
* <p><b>Note: no padding is added when encoding not using the default alphabet.</b></p>
343343
* <p>
344344
* Thanks to "commons" project in ws.apache.org for the bitwise operations, and general approach.
345345
* http://svn.apache.org/repos/asf/webservices/commons/trunk/modules/util/

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.apache.commons.codec.DecoderException;
3333
import org.apache.commons.codec.EncoderException;
3434
import org.apache.commons.lang3.ArrayUtils;
35+
import org.junit.Assert;
3536
import org.junit.Ignore;
3637
import org.junit.Test;
3738

@@ -115,6 +116,36 @@ public void testBase64() {
115116
assertEquals("decode hello world", "Hello World", decodeString);
116117
}
117118

119+
@Test
120+
public void testCustomEncodingAlphabet() {
121+
// create a duplicate of STANDARD_ENCODE_TABLE and replace two chars with
122+
// custom values not already present in table
123+
byte[] encodeTable = Arrays.copyOf(Base64.STANDARD_ENCODE_TABLE, 64);
124+
encodeTable[0] = '.';
125+
encodeTable[1] = '-';
126+
127+
// two instances: one with default table and one with adjusted encoding table
128+
Base64 b64 = new Base64();
129+
Base64 b64customEncoding = new Base64(encodeTable);
130+
131+
final String content = "Hello World - this ";
132+
133+
byte[] encodedBytes = b64.encode(StringUtils.getBytesUtf8(content));
134+
String encodedContent = StringUtils.newStringUtf8(encodedBytes);
135+
136+
byte[] encodedBytesCustom = b64customEncoding.encode(StringUtils.getBytesUtf8(content));
137+
String encodedContentCustom = StringUtils.newStringUtf8(encodedBytesCustom);
138+
139+
Assert.assertTrue("testing precondition not met - ecodedContent should contain parts of modified table",
140+
encodedContent.contains("A") || encodedContent.contains("B"));
141+
142+
Assert.assertEquals("custom encoding mismatch to expected - " + encodedContentCustom,
143+
encodedContent
144+
.replaceAll("A", ".").replaceAll("B", "-") // replace alphabet adjustments
145+
.replaceAll("=", "") // remove padding (not default alphabet)
146+
, encodedContentCustom);
147+
}
148+
118149
@Test
119150
public void testBase64AtBufferStart() {
120151
testBase64InBuffer(0, 100);

0 commit comments

Comments
 (0)