summaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit
diff options
context:
space:
mode:
authorMatthias Sohn <matthias.sohn@sap.com>2024-09-06 13:42:27 +0200
committerLuca Milanesio <luca.milanesio@gmail.com>2024-11-09 23:20:01 +0000
commitd34f8b523638fc95b2e7006d02c9f6a756cbba85 (patch)
tree3274814ac377e76db18ee77baa6f2aa9cdad023d /org.eclipse.jgit
parente8c414b9cc72cd0e4f8c66b836de4c43f134b102 (diff)
downloadjgit-d34f8b523638fc95b2e7006d02c9f6a756cbba85.tar.gz
jgit-d34f8b523638fc95b2e7006d02c9f6a756cbba85.zip
Replace custom encoder Constants#encodeASCII by JDK implementation
Ensure that the method still throws an IllegalArgumentException for malformed input or if the String contains unmappable characters. Change-Id: I6a340aa1af60c315272ff13b6bf2041ba30c94ca (cherry picked from commit 0fd76114e3436ac635641d06371fd8833179312d)
Diffstat (limited to 'org.eclipse.jgit')
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/lib/Constants.java20
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 5d7eacb4b8..1a2f735622 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/Constants.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/Constants.java
@@ -12,9 +12,14 @@
package org.eclipse.jgit.lib;
+import static java.nio.charset.StandardCharsets.US_ASCII;
import static java.nio.charset.StandardCharsets.UTF_8;
import java.nio.charset.Charset;
+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;
@@ -667,14 +672,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;
}
/**