You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by bu...@apache.org on 2020/02/26 13:24:38 UTC
[cxf] branch master updated: cxf-core: ensure that
StringUtils.toHexString result is multiple of two
This is an automated email from the ASF dual-hosted git repository.
buhhunyx pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cxf.git
The following commit(s) were added to refs/heads/master by this push:
new 0b1c413 cxf-core: ensure that StringUtils.toHexString result is multiple of two
0b1c413 is described below
commit 0b1c413071a7c4c933667d6ab21e2bb51252163c
Author: Alexey Markevich <bu...@gmail.com>
AuthorDate: Wed Feb 26 16:23:58 2020 +0300
cxf-core: ensure that StringUtils.toHexString result is multiple of two
---
.../java/org/apache/cxf/common/util/StringUtils.java | 15 +++++++++++----
.../java/org/apache/cxf/common/util/URIParserUtil.java | 16 +++++++---------
.../java/org/apache/cxf/common/util/StringUtilsTest.java | 6 ++++++
3 files changed, 24 insertions(+), 13 deletions(-)
diff --git a/core/src/main/java/org/apache/cxf/common/util/StringUtils.java b/core/src/main/java/org/apache/cxf/common/util/StringUtils.java
index 47a6d81..2da80a9 100644
--- a/core/src/main/java/org/apache/cxf/common/util/StringUtils.java
+++ b/core/src/main/java/org/apache/cxf/common/util/StringUtils.java
@@ -32,6 +32,8 @@ public final class StringUtils {
private static final Predicate<String> NOT_EMPTY = (String s) -> !s.isEmpty();
+ private static final char[] HEX = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
+
private StringUtils() {
}
@@ -147,10 +149,15 @@ public final class StringUtils {
}
public static String toHexString(byte[] bytes) {
- StringBuilder hexString = new StringBuilder();
- for (int i = 0; i < bytes.length; i++) {
- hexString.append(Integer.toHexString(0xFF & bytes[i]));
+ final StringBuilder sb = new StringBuilder(bytes.length * 2);
+ for (byte b : bytes) {
+ byteToHex(b, sb);
}
- return hexString.toString();
+ return sb.toString();
+ }
+
+ static void byteToHex(byte b, StringBuilder sb) {
+ sb.append(HEX[(0xF0 & b) >> 4]);
+ sb.append(HEX[0x0F & b]);
}
}
diff --git a/core/src/main/java/org/apache/cxf/common/util/URIParserUtil.java b/core/src/main/java/org/apache/cxf/common/util/URIParserUtil.java
index d6813b0..c5cde76 100644
--- a/core/src/main/java/org/apache/cxf/common/util/URIParserUtil.java
+++ b/core/src/main/java/org/apache/cxf/common/util/URIParserUtil.java
@@ -31,7 +31,6 @@ import org.apache.cxf.common.classloader.ClassLoaderUtils;
public final class URIParserUtil {
private static final String EXCLUDED_CHARS = "<>\"{}|\\^`";
- private static final String HEX_DIGITS = "0123456789abcdef";
private URIParserUtil() {
// complete
@@ -66,29 +65,28 @@ public final class URIParserUtil {
}
public static String escapeChars(String s) {
- StringBuilder b = new StringBuilder(s.length());
+ StringBuilder sb = new StringBuilder(s.length());
for (int x = 0; x < s.length(); x++) {
char ch = s.charAt(x);
if (isExcluded(ch)) {
byte[] bytes = Character.toString(ch).getBytes(StandardCharsets.UTF_8);
- for (int y = 0; y < bytes.length; y++) {
- b.append('%');
- b.append(HEX_DIGITS.charAt((bytes[y] & 0xFF) >> 4));
- b.append(HEX_DIGITS.charAt(bytes[y] & 0x0F));
+ for (byte b : bytes) {
+ sb.append('%');
+ StringUtils.byteToHex(b, sb);
}
} else {
- b.append(ch);
+ sb.append(ch);
}
}
- return b.toString();
+ return sb.toString();
}
public static String normalize(final String uri) {
URL url = null;
String result = null;
try {
url = new URL(uri);
- result = escapeChars(url.toURI().normalize().toString().replace("\\", "/"));
+ result = escapeChars(url.toURI().normalize().toString().replace('\\', '/'));
} catch (MalformedURLException e1) {
try {
if (uri.startsWith("classpath:")) {
diff --git a/core/src/test/java/org/apache/cxf/common/util/StringUtilsTest.java b/core/src/test/java/org/apache/cxf/common/util/StringUtilsTest.java
index 607c197..e699dbe 100644
--- a/core/src/test/java/org/apache/cxf/common/util/StringUtilsTest.java
+++ b/core/src/test/java/org/apache/cxf/common/util/StringUtilsTest.java
@@ -79,4 +79,10 @@ public class StringUtilsTest {
assertEquals("aA", StringUtils.uncapitalize("AA"));
}
+ @Test
+ public void testToHexString() {
+ byte[] bytes = new byte[] {Byte.MIN_VALUE, 0x20, Byte.MAX_VALUE, 0x00, (byte) 0xFF};
+ String hexString = StringUtils.toHexString(bytes);
+ assertEquals(bytes.length * 2, hexString.length());
+ }
}
\ No newline at end of file