Skip to content
Snippets Groups Projects
Commit d34f8b52 authored by Matthias Sohn's avatar Matthias Sohn Committed by Luca Milanesio
Browse files

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 0fd76114)
parent e8c414b9
No related branches found
No related tags found
No related merge requests found
......@@ -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 static byte[] encodeASCII(long s) {
* 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;
}
/**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment