Skip to content

Commit 17d28c1

Browse files
committed
Sort members
1 parent d2b2bf5 commit 17d28c1

2 files changed

Lines changed: 77 additions & 77 deletions

File tree

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

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,17 @@ public Base64(final boolean urlSafe) {
478478
this(MIME_CHUNK_SIZE, CHUNK_SEPARATOR, urlSafe);
479479
}
480480

481+
/**
482+
* Creates a Base64 codec used for decoding and encoding with non-standard encodeTable-table
483+
*
484+
* @param encodeTable
485+
* The manual encodeTable - a byte array of 64 chars
486+
* @since 1.17.0
487+
*/
488+
public Base64(byte[] encodeTable) {
489+
this(0, CHUNK_SEPARATOR, encodeTable, DECODING_POLICY_DEFAULT);
490+
}
491+
481492
/**
482493
* Creates a Base64 codec used for decoding (all modes) and encoding in URL-unsafe mode.
483494
* <p>
@@ -528,6 +539,7 @@ public Base64(final int lineLength, final byte[] lineSeparator) {
528539
this(lineLength, lineSeparator, false);
529540
}
530541

542+
531543
/**
532544
* Creates a Base64 codec used for decoding (all modes) and encoding in URL-unsafe mode.
533545
* <p>
@@ -559,18 +571,6 @@ public Base64(final int lineLength, final byte[] lineSeparator, final boolean ur
559571
this(lineLength, lineSeparator, urlSafe ? URL_SAFE_ENCODE_TABLE : STANDARD_ENCODE_TABLE, DECODING_POLICY_DEFAULT);
560572
}
561573

562-
563-
/**
564-
* Creates a Base64 codec used for decoding and encoding with non-standard encodeTable-table
565-
*
566-
* @param encodeTable
567-
* The manual encodeTable - a byte array of 64 chars
568-
* @since 1.17.0
569-
*/
570-
public Base64(byte[] encodeTable) {
571-
this(0, CHUNK_SEPARATOR, encodeTable, DECODING_POLICY_DEFAULT);
572-
}
573-
574574
/**
575575
* Creates a Base64 codec used for decoding (all modes) and encoding in URL-unsafe mode.
576576
* <p>
@@ -670,6 +670,21 @@ public Base64(final int lineLength, final byte[] lineSeparator, final byte[] enc
670670

671671
// Implementation of the Encoder Interface
672672

673+
/**
674+
* Calculates a decode table for a given encode table.
675+
*
676+
* @param encodeTable that is used to determine decode lookup table
677+
* @return decodeTable
678+
*/
679+
private byte[] calculateDecodeTable(byte[] encodeTable) {
680+
byte[] decodeTable = new byte[DECODING_TABLE_LENGTH];
681+
Arrays.fill(decodeTable, (byte) -1);
682+
for (int i = 0; i < encodeTable.length; i++) {
683+
decodeTable[encodeTable[i]] = (byte) i;
684+
}
685+
return decodeTable;
686+
}
687+
673688
/**
674689
* <p>
675690
* Decodes all of the provided data, starting at inPos, for inAvail bytes. Should be called at least twice: once
@@ -860,21 +875,6 @@ protected boolean isInAlphabet(final byte octet) {
860875
return octet >= 0 && octet < decodeTable.length && decodeTable[octet] != -1;
861876
}
862877

863-
/**
864-
* Calculates a decode table for a given encode table.
865-
*
866-
* @param encodeTable that is used to determine decode lookup table
867-
* @return decodeTable
868-
*/
869-
private byte[] calculateDecodeTable(byte[] encodeTable) {
870-
byte[] decodeTable = new byte[DECODING_TABLE_LENGTH];
871-
Arrays.fill(decodeTable, (byte) -1);
872-
for (int i = 0; i < encodeTable.length; i++) {
873-
decodeTable[encodeTable[i]] = (byte) i;
874-
}
875-
return decodeTable;
876-
}
877-
878878
/**
879879
* Returns our current encode mode. True if we're URL-SAFE, false otherwise.
880880
*

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

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -167,56 +167,6 @@ public void testBase64() {
167167
assertEquals("Hello World", decodeString, "decode hello world");
168168
}
169169

170-
@Test
171-
public void testCustomEncodingAlphabet_illegal() {
172-
byte[] encodeTable = {
173-
'.', '-', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M'
174-
};
175-
assertThrows(IllegalArgumentException.class, () -> new Base64(encodeTable));
176-
}
177-
178-
@Test
179-
public void testCustomEncodingAlphabet() {
180-
// created a duplicate of STANDARD_ENCODE_TABLE and replaced two chars with
181-
// custom values not already present in table
182-
// A => . B => -
183-
byte[] encodeTable = {
184-
'.', '-', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
185-
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
186-
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
187-
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
188-
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
189-
};
190-
191-
// two instances: one with default table and one with adjusted encoding table
192-
Base64 b64 = new Base64();
193-
Base64 b64customEncoding = new Base64(encodeTable);
194-
195-
final String content = "! Hello World - this §$%";
196-
197-
byte[] encodedBytes = b64.encode(StringUtils.getBytesUtf8(content));
198-
String encodedContent = StringUtils.newStringUtf8(encodedBytes);
199-
200-
byte[] encodedBytesCustom = b64customEncoding.encode(StringUtils.getBytesUtf8(content));
201-
String encodedContentCustom = StringUtils.newStringUtf8(encodedBytesCustom);
202-
203-
assertTrue(
204-
encodedContent.contains("A") && encodedContent.contains("B"), "testing precondition not met - ecodedContent should contain parts of modified table");
205-
206-
assertEquals(
207-
encodedContent
208-
.replaceAll("A", ".").replaceAll("B", "-") // replace alphabet adjustments
209-
.replaceAll("=", "") // remove padding (not default alphabet)
210-
, encodedContentCustom);
211-
212-
213-
// try decode encoded content
214-
final byte[] decode = b64customEncoding.decode(encodedBytesCustom);
215-
final String decodeString = StringUtils.newStringUtf8(decode);
216-
217-
assertEquals(content, decodeString);
218-
}
219-
220170
@Test
221171
public void testBase64AtBufferEnd() {
222172
testBase64InBuffer(100, 0);
@@ -465,6 +415,56 @@ public void testConstructors() {
465415
assertNotNull(base64);
466416
}
467417

418+
@Test
419+
public void testCustomEncodingAlphabet() {
420+
// created a duplicate of STANDARD_ENCODE_TABLE and replaced two chars with
421+
// custom values not already present in table
422+
// A => . B => -
423+
byte[] encodeTable = {
424+
'.', '-', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
425+
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
426+
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
427+
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
428+
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
429+
};
430+
431+
// two instances: one with default table and one with adjusted encoding table
432+
Base64 b64 = new Base64();
433+
Base64 b64customEncoding = new Base64(encodeTable);
434+
435+
final String content = "! Hello World - this §$%";
436+
437+
byte[] encodedBytes = b64.encode(StringUtils.getBytesUtf8(content));
438+
String encodedContent = StringUtils.newStringUtf8(encodedBytes);
439+
440+
byte[] encodedBytesCustom = b64customEncoding.encode(StringUtils.getBytesUtf8(content));
441+
String encodedContentCustom = StringUtils.newStringUtf8(encodedBytesCustom);
442+
443+
assertTrue(
444+
encodedContent.contains("A") && encodedContent.contains("B"), "testing precondition not met - ecodedContent should contain parts of modified table");
445+
446+
assertEquals(
447+
encodedContent
448+
.replaceAll("A", ".").replaceAll("B", "-") // replace alphabet adjustments
449+
.replaceAll("=", "") // remove padding (not default alphabet)
450+
, encodedContentCustom);
451+
452+
453+
// try decode encoded content
454+
final byte[] decode = b64customEncoding.decode(encodedBytesCustom);
455+
final String decodeString = StringUtils.newStringUtf8(decode);
456+
457+
assertEquals(content, decodeString);
458+
}
459+
460+
@Test
461+
public void testCustomEncodingAlphabet_illegal() {
462+
byte[] encodeTable = {
463+
'.', '-', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M'
464+
};
465+
assertThrows(IllegalArgumentException.class, () -> new Base64(encodeTable));
466+
}
467+
468468
private void testDecodeEncode(final String encodedText) {
469469
final String decodedText = StringUtils.newStringUsAscii(Base64.decodeBase64(encodedText));
470470
final String encodedText2 = Base64.encodeBase64String(StringUtils.getBytesUtf8(decodedText));

0 commit comments

Comments
 (0)