diff options
author | Matthias Sohn <matthias.sohn@sap.com> | 2024-10-26 19:06:49 +0000 |
---|---|---|
committer | Gerrit Code Review <support@gerrithub.io> | 2024-10-26 19:06:49 +0000 |
commit | e18d779fd94a1dcf13a471ff8fee170809b71fe1 (patch) | |
tree | f48fbb3cdde6c952f6997d9aed9f78fc707428fd /org.eclipse.jgit/src/org | |
parent | 8188c4a773490bbde9b997045cdbcbbf0d480e9b (diff) | |
parent | 0fd76114e3436ac635641d06371fd8833179312d (diff) | |
download | jgit-e18d779fd94a1dcf13a471ff8fee170809b71fe1.tar.gz jgit-e18d779fd94a1dcf13a471ff8fee170809b71fe1.zip |
Merge "Replace custom encoder Constants#encodeASCII by JDK implementation"
Diffstat (limited to 'org.eclipse.jgit/src/org')
-rw-r--r-- | org.eclipse.jgit/src/org/eclipse/jgit/lib/Constants.java | 20 |
1 files changed, 13 insertions, 7 deletions
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/Constants.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/Constants.java index c27047f25d..cb4a5b51fe 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/Constants.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/Constants.java @@ -12,8 +12,13 @@ package org.eclipse.jgit.lib; +import static java.nio.charset.StandardCharsets.US_ASCII; import static java.nio.charset.StandardCharsets.UTF_8; +import java.nio.CharBuffer; +import java.nio.charset.CharacterCodingException; +import java.nio.charset.CharsetEncoder; +import java.nio.charset.CodingErrorAction; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.text.MessageFormat; @@ -714,14 +719,15 @@ public final class Constants { * the 7-bit ASCII character space. */ public static byte[] encodeASCII(String s) { - final byte[] r = new byte[s.length()]; - for (int k = r.length - 1; k >= 0; k--) { - final char c = s.charAt(k); - if (c > 127) - throw new IllegalArgumentException(MessageFormat.format(JGitText.get().notASCIIString, s)); - r[k] = (byte) c; + try { + CharsetEncoder encoder = US_ASCII.newEncoder() + .onUnmappableCharacter(CodingErrorAction.REPORT) + .onMalformedInput(CodingErrorAction.REPORT); + return encoder.encode(CharBuffer.wrap(s)).array(); + } catch (CharacterCodingException e) { + throw new IllegalArgumentException( + MessageFormat.format(JGitText.get().notASCIIString, s), e); } - return r; } /** |