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