@@ -60,6 +60,10 @@ public final class Base64Util {
6060
6161 private static Base64 .Decoder decoder ;
6262
63+ private static Base64 .Encoder urlEncoder ;
64+
65+ private static Base64 .Decoder urlDecoder ;
66+
6367 /**
6468 * Ensure that there is only one Base64 Encoder.
6569 *
@@ -84,6 +88,20 @@ private static Base64.Decoder getDecoder() {
8488 return decoder ;
8589 }
8690
91+ private static Base64 .Encoder getUrlEncoder () {
92+ if (Objects .isNull (urlEncoder )) {
93+ urlEncoder = Base64 .getUrlEncoder ();
94+ }
95+ return urlEncoder ;
96+ }
97+
98+ public static Base64 .Decoder getUrlDecoder () {
99+ if (Objects .isNull (urlDecoder )) {
100+ urlDecoder = Base64 .getUrlDecoder ();
101+ }
102+ return urlDecoder ;
103+ }
104+
87105 /**
88106 * Private constructor to prevent instantiation of the class.
89107 */
@@ -136,4 +154,50 @@ public static String decode(String value) {
136154 return decode (value , StandardCharsets .UTF_8 );
137155 }
138156
157+ /**
158+ * Encodes the given string using the specified charset.
159+ *
160+ * @param value the string to be encoded
161+ * @param charset the charset to be used for encoding
162+ * @return the Base64 encoded string
163+ */
164+ public static String encodeUrlComponents (String value , Charset charset ) {
165+ var encoded = getUrlEncoder ().encode (value .getBytes (charset ));
166+
167+ return new String (encoded );
168+ }
169+
170+ /**
171+ * Encodes the given string using the default UTF-8 charset.
172+ *
173+ * @param value the string to be encoded
174+ * @return the Base64 encoded string
175+ */
176+ public static String encodeUrlComponents (String value ) {
177+ return encodeUrlComponents (value , StandardCharsets .UTF_8 );
178+ }
179+
180+ /**
181+ * Decodes the given Base64 encoded string using the specified charset.
182+ *
183+ * @param value the Base64 encoded string to be decoded
184+ * @param charset the charset to be used for decoding
185+ * @return the decoded string
186+ */
187+ public static String decodeUrlComponents (String value , Charset charset ) {
188+ var decoded = getUrlDecoder ().decode (value .getBytes (charset ));
189+
190+ return new String (decoded );
191+ }
192+
193+ /**
194+ * Decodes the given Base64 encoded string using the default UTF-8 charset.
195+ *
196+ * @param value the Base64 encoded string to be decoded
197+ * @return the decoded string
198+ */
199+ public static String decodeUrlComponents (String value ) {
200+ return decodeUrlComponents (value , StandardCharsets .UTF_8 );
201+ }
202+
139203}
0 commit comments