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 17d28c19bb872b7cffcb35831e4c15fc27417da1 Author: Gary Gregory <[email protected]> AuthorDate: Mon Apr 15 17:56:39 2024 -0400 Sort members --- .../org/apache/commons/codec/binary/Base64.java | 54 +++++------ .../apache/commons/codec/binary/Base64Test.java | 100 ++++++++++----------- 2 files changed, 77 insertions(+), 77 deletions(-) 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 7fffc9f9..a5383d0f 100644 --- a/src/main/java/org/apache/commons/codec/binary/Base64.java +++ b/src/main/java/org/apache/commons/codec/binary/Base64.java @@ -478,6 +478,17 @@ public class Base64 extends BaseNCodec { this(MIME_CHUNK_SIZE, CHUNK_SEPARATOR, urlSafe); } + /** + * Creates a Base64 codec used for decoding and encoding with non-standard encodeTable-table + * + * @param encodeTable + * The manual encodeTable - a byte array of 64 chars + * @since 1.17.0 + */ + public Base64(byte[] encodeTable) { + this(0, CHUNK_SEPARATOR, encodeTable, DECODING_POLICY_DEFAULT); + } + /** * Creates a Base64 codec used for decoding (all modes) and encoding in URL-unsafe mode. * <p> @@ -528,6 +539,7 @@ public class Base64 extends BaseNCodec { this(lineLength, lineSeparator, false); } + /** * Creates a Base64 codec used for decoding (all modes) and encoding in URL-unsafe mode. * <p> @@ -559,18 +571,6 @@ public class Base64 extends BaseNCodec { this(lineLength, lineSeparator, urlSafe ? URL_SAFE_ENCODE_TABLE : STANDARD_ENCODE_TABLE, DECODING_POLICY_DEFAULT); } - - /** - * Creates a Base64 codec used for decoding and encoding with non-standard encodeTable-table - * - * @param encodeTable - * The manual encodeTable - a byte array of 64 chars - * @since 1.17.0 - */ - public Base64(byte[] encodeTable) { - this(0, CHUNK_SEPARATOR, encodeTable, DECODING_POLICY_DEFAULT); - } - /** * Creates a Base64 codec used for decoding (all modes) and encoding in URL-unsafe mode. * <p> @@ -670,6 +670,21 @@ public class Base64 extends BaseNCodec { // Implementation of the Encoder Interface + /** + * Calculates a decode table for a given encode table. + * + * @param encodeTable that is used to determine decode lookup table + * @return decodeTable + */ + private byte[] calculateDecodeTable(byte[] encodeTable) { + byte[] decodeTable = new byte[DECODING_TABLE_LENGTH]; + Arrays.fill(decodeTable, (byte) -1); + for (int i = 0; i < encodeTable.length; i++) { + decodeTable[encodeTable[i]] = (byte) i; + } + return decodeTable; + } + /** * <p> * Decodes all of the provided data, starting at inPos, for inAvail bytes. Should be called at least twice: once @@ -860,21 +875,6 @@ public class Base64 extends BaseNCodec { return octet >= 0 && octet < decodeTable.length && decodeTable[octet] != -1; } - /** - * Calculates a decode table for a given encode table. - * - * @param encodeTable that is used to determine decode lookup table - * @return decodeTable - */ - private byte[] calculateDecodeTable(byte[] encodeTable) { - byte[] decodeTable = new byte[DECODING_TABLE_LENGTH]; - Arrays.fill(decodeTable, (byte) -1); - for (int i = 0; i < encodeTable.length; i++) { - decodeTable[encodeTable[i]] = (byte) i; - } - return decodeTable; - } - /** * Returns our current encode mode. True if we're URL-SAFE, false otherwise. * 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 6626a918..b4c35d76 100644 --- a/src/test/java/org/apache/commons/codec/binary/Base64Test.java +++ b/src/test/java/org/apache/commons/codec/binary/Base64Test.java @@ -167,56 +167,6 @@ public class Base64Test { assertEquals("Hello World", decodeString, "decode hello world"); } - @Test - public void testCustomEncodingAlphabet_illegal() { - byte[] encodeTable = { - '.', '-', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M' - }; - assertThrows(IllegalArgumentException.class, () -> new Base64(encodeTable)); - } - - @Test - public void testCustomEncodingAlphabet() { - // created a duplicate of STANDARD_ENCODE_TABLE and replaced two chars with - // custom values not already present in table - // A => . B => - - byte[] encodeTable = { - '.', '-', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', - 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', - 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', - 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', - '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' - }; - - // two instances: one with default table and one with adjusted encoding table - Base64 b64 = new Base64(); - Base64 b64customEncoding = new Base64(encodeTable); - - final String content = "! Hello World - this §$%"; - - byte[] encodedBytes = b64.encode(StringUtils.getBytesUtf8(content)); - String encodedContent = StringUtils.newStringUtf8(encodedBytes); - - byte[] encodedBytesCustom = b64customEncoding.encode(StringUtils.getBytesUtf8(content)); - String encodedContentCustom = StringUtils.newStringUtf8(encodedBytesCustom); - - assertTrue( - encodedContent.contains("A") && encodedContent.contains("B"), "testing precondition not met - ecodedContent should contain parts of modified table"); - - assertEquals( - encodedContent - .replaceAll("A", ".").replaceAll("B", "-") // replace alphabet adjustments - .replaceAll("=", "") // remove padding (not default alphabet) - , encodedContentCustom); - - - // try decode encoded content - final byte[] decode = b64customEncoding.decode(encodedBytesCustom); - final String decodeString = StringUtils.newStringUtf8(decode); - - assertEquals(content, decodeString); - } - @Test public void testBase64AtBufferEnd() { testBase64InBuffer(100, 0); @@ -465,6 +415,56 @@ public class Base64Test { assertNotNull(base64); } + @Test + public void testCustomEncodingAlphabet() { + // created a duplicate of STANDARD_ENCODE_TABLE and replaced two chars with + // custom values not already present in table + // A => . B => - + byte[] encodeTable = { + '.', '-', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', + 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', + 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', + 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' + }; + + // two instances: one with default table and one with adjusted encoding table + Base64 b64 = new Base64(); + Base64 b64customEncoding = new Base64(encodeTable); + + final String content = "! Hello World - this §$%"; + + byte[] encodedBytes = b64.encode(StringUtils.getBytesUtf8(content)); + String encodedContent = StringUtils.newStringUtf8(encodedBytes); + + byte[] encodedBytesCustom = b64customEncoding.encode(StringUtils.getBytesUtf8(content)); + String encodedContentCustom = StringUtils.newStringUtf8(encodedBytesCustom); + + assertTrue( + encodedContent.contains("A") && encodedContent.contains("B"), "testing precondition not met - ecodedContent should contain parts of modified table"); + + assertEquals( + encodedContent + .replaceAll("A", ".").replaceAll("B", "-") // replace alphabet adjustments + .replaceAll("=", "") // remove padding (not default alphabet) + , encodedContentCustom); + + + // try decode encoded content + final byte[] decode = b64customEncoding.decode(encodedBytesCustom); + final String decodeString = StringUtils.newStringUtf8(decode); + + assertEquals(content, decodeString); + } + + @Test + public void testCustomEncodingAlphabet_illegal() { + byte[] encodeTable = { + '.', '-', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M' + }; + assertThrows(IllegalArgumentException.class, () -> new Base64(encodeTable)); + } + private void testDecodeEncode(final String encodedText) { final String decodedText = StringUtils.newStringUsAscii(Base64.decodeBase64(encodedText)); final String encodedText2 = Base64.encodeBase64String(StringUtils.getBytesUtf8(decodedText));
