Skip to content

Commit 09cb2ce

Browse files
committed
Add encodeBase64() supporting padding-less encoding
1 parent c8f4270 commit 09cb2ce

File tree

3 files changed

+46
-4
lines changed

3 files changed

+46
-4
lines changed

CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
* add `append()` method supporting multiple byte arrays #26
66
* add `toCharArray()` method which decodes internal byte array to char[] #27
7+
* add `encodeBase64()` supporting padding-less encoding
78

89
## v0.8.0
910

src/main/java/at/favre/lib/bytes/Bytes.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,8 @@ public static Bytes parseBase36(CharSequence base36String) {
631631
}
632632

633633
/**
634-
* Parsing of base64 encoded byte arrays. Supporting RFC 3548 normal and url safe encoding.
634+
* Parsing of base64 encoded byte arrays.
635+
* Supporting RFC 4648 normal and url safe encoding, with or without padding.
635636
*
636637
* @param base64String the encoded string
637638
* @return decoded instance
@@ -1624,27 +1625,46 @@ public String encodeBase36() {
16241625

16251626
/**
16261627
* Base64 representation with padding. This is *NOT* the url safe variation. This encoding has a space efficiency of 75%.
1628+
*
1629+
* This encoding is <a href="https://tools.ietf.org/html/rfc4648">RFC 4648</a> compatible.
16271630
* <p>
16281631
* Example: <code>SpT9/x6v7Q==</code>
16291632
*
16301633
* @return base64 string
16311634
* @see <a href="https://en.wikipedia.org/wiki/Base64">Base64</a>
16321635
*/
16331636
public String encodeBase64() {
1634-
return encode(new BinaryToTextEncoding.Base64Encoding(false, true));
1637+
return encodeBase64(false, true);
16351638
}
16361639

16371640
/**
1638-
* Base64 representation with padding. This is the url safe variation subsitution '+' and '/' with '-' and '_'
1641+
* Base64 representation with padding. This is the url safe variation substitution '+' and '/' with '-' and '_'
16391642
* respectively. This encoding has a space efficiency of 75%.
1643+
*
1644+
* This encoding is <a href="https://tools.ietf.org/html/rfc4648">RFC 4648</a> compatible.
16401645
* <p>
16411646
* Example: <code>SpT9_x6v7Q==</code>
16421647
*
16431648
* @return base64 url safe string
16441649
* @see <a href="https://en.wikipedia.org/wiki/Base64">Base64</a>
16451650
*/
16461651
public String encodeBase64Url() {
1647-
return encode(new BinaryToTextEncoding.Base64Encoding(true, true));
1652+
return encodeBase64(true, true);
1653+
}
1654+
1655+
/**
1656+
* Base64 representation with either padding or without and with or without URL and filename safe alphabet.
1657+
* This encoding is <a href="https://tools.ietf.org/html/rfc4648">RFC 4648</a> compatible.
1658+
* <p>
1659+
* Example: <code>SpT9/x6v7Q==</code>
1660+
*
1661+
* @param urlSafe if true will substitute '+' and '/' with '-' and '_'
1662+
* @param withPadding if true will add padding the next full byte with '='
1663+
* @return base64 url safe string
1664+
* @see <a href="https://en.wikipedia.org/wiki/Base64">Base64</a>
1665+
*/
1666+
public String encodeBase64(boolean urlSafe, boolean withPadding) {
1667+
return encode(new BinaryToTextEncoding.Base64Encoding(urlSafe, withPadding));
16481668
}
16491669

16501670
/**

src/test/java/at/favre/lib/bytes/BytesParseAndEncodingTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ public void encodeHex() {
6161
@Test
6262
public void parseBase64() {
6363
assertArrayEquals(encodingExample, Bytes.parseBase64("SpT9/x6v7Q==").array());
64+
assertArrayEquals(encodingExample, Bytes.parseBase64("SpT9/x6v7Q").array());
6465
assertArrayEquals(encodingExample, Bytes.parseBase64("SpT9_x6v7Q==").array());
66+
assertArrayEquals(encodingExample, Bytes.parseBase64("SpT9_x6v7Q").array());
6567
}
6668

6769
@Test(expected = IllegalArgumentException.class)
@@ -83,6 +85,25 @@ public void encodeBase64Url() {
8385
assertEquals("SpT9_x6v7Q==", Bytes.from(encodingExample).encodeBase64Url());
8486
}
8587

88+
@Test
89+
public void encodeBase64WithConfig() {
90+
assertEquals("", Bytes.from(new byte[0]).encodeBase64(true, true));
91+
assertEquals("AA==", Bytes.from(new byte[1]).encodeBase64(true, true));
92+
assertEquals("SpT9_x6v7Q==", Bytes.from(encodingExample).encodeBase64(true, true));
93+
94+
assertEquals("", Bytes.from(new byte[0]).encodeBase64(true, false));
95+
assertEquals("AA", Bytes.from(new byte[1]).encodeBase64(true, false));
96+
assertEquals("SpT9_x6v7Q", Bytes.from(encodingExample).encodeBase64(true, false));
97+
98+
assertEquals("", Bytes.from(new byte[0]).encodeBase64(false, true));
99+
assertEquals("AA==", Bytes.from(new byte[1]).encodeBase64(false, true));
100+
assertEquals("SpT9/x6v7Q==", Bytes.from(encodingExample).encodeBase64(false, true));
101+
102+
assertEquals("", Bytes.from(new byte[0]).encodeBase64(false, false));
103+
assertEquals("AA", Bytes.from(new byte[1]).encodeBase64(false, false));
104+
assertEquals("SpT9/x6v7Q", Bytes.from(encodingExample).encodeBase64(false, false));
105+
}
106+
86107
@Test
87108
public void parseBase32() {
88109
assertArrayEquals(encodingExample, Bytes.parseBase32("JKKP37Y6V7WQ====").array());

0 commit comments

Comments
 (0)