|
1 | 1 | /*
|
2 |
| - * Copyright 2005-2010 the original author or authors. |
| 2 | + * Copyright 2005-2021 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
16 | 16 |
|
17 | 17 | package org.springframework.ldap.support;
|
18 | 18 |
|
| 19 | +import java.util.Base64; |
| 20 | + |
19 | 21 | import org.springframework.ldap.BadLdapGrammarException;
|
20 | 22 | import org.springframework.util.Assert;
|
| 23 | +import org.springframework.util.ClassUtils; |
21 | 24 |
|
22 | 25 | import javax.xml.bind.DatatypeConverter;
|
23 | 26 |
|
@@ -248,7 +251,7 @@ public static String printBase64Binary(byte[] val) {
|
248 | 251 |
|
249 | 252 | Assert.notNull(val, "val must not be null!");
|
250 | 253 |
|
251 |
| - String encoded = DatatypeConverter.printBase64Binary(val); |
| 254 | + String encoded = encode(val); |
252 | 255 |
|
253 | 256 | int length = encoded.length();
|
254 | 257 | StringBuilder sb = new StringBuilder(length + length / RFC2849_MAX_BASE64_CHARS_PER_LINE);
|
@@ -293,6 +296,32 @@ public static byte[] parseBase64Binary(String val) {
|
293 | 296 | sb.append(c);
|
294 | 297 | }
|
295 | 298 |
|
296 |
| - return DatatypeConverter.parseBase64Binary(sb.toString()); |
| 299 | + return decode(sb.toString()); |
| 300 | + } |
| 301 | + |
| 302 | + private static String encode(byte[] decoded) { |
| 303 | + if (ClassUtils.isPresent("java.util.Base64", null)) { |
| 304 | + return java.util.Base64.getEncoder().encodeToString(decoded); |
| 305 | + } else { |
| 306 | + return Base64Converter.encode(decoded); |
| 307 | + } |
| 308 | + } |
| 309 | + |
| 310 | + private static byte[] decode(String encoded) { |
| 311 | + if (ClassUtils.isPresent("java.util.Base64", null)) { |
| 312 | + return java.util.Base64.getDecoder().decode(encoded); |
| 313 | + } else { |
| 314 | + return Base64Converter.decode(encoded); |
| 315 | + } |
| 316 | + } |
| 317 | + |
| 318 | + private static class Base64Converter { |
| 319 | + static byte[] decode(String string) { |
| 320 | + return DatatypeConverter.parseBase64Binary(string); |
| 321 | + } |
| 322 | + |
| 323 | + static String encode(byte[] bytes) { |
| 324 | + return DatatypeConverter.printBase64Binary(bytes); |
| 325 | + } |
297 | 326 | }
|
298 | 327 | }
|
0 commit comments