KEYCLOAK-12207 Use StandartCharsets for class org.keycloak.saml.common.util.Base64 from module "saml-core-api"

This commit is contained in:
Andrei Arlou 2019-11-22 23:12:30 +03:00 committed by Hynek Mlnařík
parent 06e746fc42
commit 97d2ea8317

View file

@ -16,6 +16,7 @@
*/ */
package org.keycloak.saml.common.util; package org.keycloak.saml.common.util;
import java.nio.charset.StandardCharsets;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -100,9 +101,6 @@ public class Base64 {
/** The new line character (\n) as a byte. */ /** The new line character (\n) as a byte. */
private static final byte NEW_LINE = (byte) '\n'; private static final byte NEW_LINE = (byte) '\n';
/** Preferred encoding. */
private static final String PREFERRED_ENCODING = "UTF-8";
/** The 64 valid Base64 values. */ /** The 64 valid Base64 values. */
private static final byte[] ALPHABET; private static final byte[] ALPHABET;
private static final byte[] _NATIVE_ALPHABET = /* May be something funny like EBCDIC */ private static final byte[] _NATIVE_ALPHABET = /* May be something funny like EBCDIC */
@ -118,7 +116,7 @@ public class Base64 {
static { static {
byte[] __bytes; byte[] __bytes;
try { try {
__bytes = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".getBytes(PREFERRED_ENCODING); __bytes = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".getBytes(StandardCharsets.UTF_8.name());
} // end try } // end try
catch (java.io.UnsupportedEncodingException use) { catch (java.io.UnsupportedEncodingException use) {
__bytes = _NATIVE_ALPHABET; // Fall back to native encoding __bytes = _NATIVE_ALPHABET; // Fall back to native encoding
@ -330,12 +328,7 @@ public class Base64 {
} // end finally } // end finally
// Return value according to relevant encoding. // Return value according to relevant encoding.
try { return new String(baos.toByteArray(), StandardCharsets.UTF_8);
return new String(baos.toByteArray(), PREFERRED_ENCODING);
} // end try
catch (java.io.UnsupportedEncodingException uue) {
return new String(baos.toByteArray());
} // end catch
} // end encode } // end encode
@ -455,12 +448,7 @@ public class Base64 {
} // end finally } // end finally
// Return value according to relevant encoding. // Return value according to relevant encoding.
try { return new String(baos.toByteArray(), StandardCharsets.UTF_8);
return new String(baos.toByteArray(), PREFERRED_ENCODING);
} // end try
catch (java.io.UnsupportedEncodingException uue) {
return new String(baos.toByteArray());
} // end catch
} // end if: compress } // end if: compress
// Else, don't compress. Better not to use streams at all then. // Else, don't compress. Better not to use streams at all then.
@ -493,12 +481,7 @@ public class Base64 {
} // end if: some padding needed } // end if: some padding needed
// Return value according to relevant encoding. // Return value according to relevant encoding.
try { return new String(outBuff, 0, e, StandardCharsets.UTF_8);
return new String(outBuff, 0, e, PREFERRED_ENCODING);
} // end try
catch (java.io.UnsupportedEncodingException uue) {
return new String(outBuff, 0, e);
} // end catch
} // end else: don't compress } // end else: don't compress
@ -632,14 +615,7 @@ public class Base64 {
* @since 1.4 * @since 1.4
*/ */
public static byte[] decode(String s) { public static byte[] decode(String s) {
byte[] bytes; byte[] bytes = s.getBytes(StandardCharsets.UTF_8);
try {
bytes = s.getBytes(PREFERRED_ENCODING);
} // end try
catch (java.io.UnsupportedEncodingException uee) {
bytes = s.getBytes();
} // end catch
// </change>
// Decode // Decode
bytes = decode(bytes, 0, bytes.length); bytes = decode(bytes, 0, bytes.length);