1
2
3
4
5
6
7
8
9
10
11 package org.mule.util;
12
13 import org.apache.commons.lang.CharUtils;
14
15
16
17
18
19 public class StringUtils extends org.apache.commons.lang.StringUtils
20 {
21
22
23
24
25
26 public static String[] splitAndTrim(String string, String delim)
27 {
28 if (string == null)
29 {
30 return null;
31 }
32
33 if (isEmpty(string))
34 {
35 return ArrayUtils.EMPTY_STRING_ARRAY;
36 }
37
38 String[] tokens = split(string, delim);
39 if (tokens != null)
40 {
41 for (int i = 0; i < tokens.length; i++)
42 {
43 tokens[i] = trim(tokens[i]);
44 }
45 }
46
47 return tokens;
48 }
49
50
51
52
53
54
55
56 public static byte[] hexStringToByteArray(String hex)
57 {
58 if (hex == null)
59 {
60 return null;
61 }
62
63 int stringLength = hex.length();
64 if (stringLength % 2 != 0)
65 {
66 throw new IllegalArgumentException("Hex String must have even number of characters!");
67 }
68
69 byte[] result = new byte[stringLength / 2];
70
71 int j = 0;
72 for (int i = 0; i < result.length; i++)
73 {
74 char hi = Character.toLowerCase(hex.charAt(j++));
75 char lo = Character.toLowerCase(hex.charAt(j++));
76 result[i] = (byte) ((Character.digit(hi, 16) << 4) | Character.digit(lo, 16));
77 }
78
79 return result;
80 }
81
82
83
84
85 public static String repeat(char c, int len)
86 {
87 return repeat(CharUtils.toString(c), len);
88 }
89
90
91
92
93 public static String toHexString(byte[] bytes)
94 {
95 return StringUtils.toHexString(bytes, false);
96 }
97
98
99
100
101
102
103
104
105
106 public static String toHexString(byte[] bytes, boolean uppercase)
107 {
108 if (bytes == null)
109 {
110 return null;
111 }
112
113 int numBytes = bytes.length;
114 StringBuffer str = new StringBuffer(numBytes * 2);
115
116 String table = (uppercase ? HEX_CHARACTERS_UC : HEX_CHARACTERS);
117
118 for (int i = 0; i < numBytes; i++)
119 {
120 str.append(table.charAt(bytes[i] >>> 4 & 0x0f));
121 str.append(table.charAt(bytes[i] & 0x0f));
122 }
123
124 return str.toString();
125 }
126
127
128 private static final String HEX_CHARACTERS = "0123456789abcdef";
129 private static final String HEX_CHARACTERS_UC = HEX_CHARACTERS.toUpperCase();
130
131 }