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 | 0 | 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 | 0 | if (string == null) |
29 | |
{ |
30 | 0 | return null; |
31 | |
} |
32 | |
|
33 | 0 | if (isEmpty(string)) |
34 | |
{ |
35 | 0 | return ArrayUtils.EMPTY_STRING_ARRAY; |
36 | |
} |
37 | |
|
38 | 0 | String[] tokens = split(string, delim); |
39 | 0 | if (tokens != null) |
40 | |
{ |
41 | 0 | for (int i = 0; i < tokens.length; i++) |
42 | |
{ |
43 | 0 | tokens[i] = trim(tokens[i]); |
44 | |
} |
45 | |
} |
46 | |
|
47 | 0 | return tokens; |
48 | |
} |
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
public static byte[] hexStringToByteArray(String hex) |
57 | |
{ |
58 | 0 | if (hex == null) |
59 | |
{ |
60 | 0 | return null; |
61 | |
} |
62 | |
|
63 | 0 | int stringLength = hex.length(); |
64 | 0 | if (stringLength % 2 != 0) |
65 | |
{ |
66 | 0 | throw new IllegalArgumentException("Hex String must have even number of characters!"); |
67 | |
} |
68 | |
|
69 | 0 | byte[] result = new byte[stringLength / 2]; |
70 | |
|
71 | 0 | int j = 0; |
72 | 0 | for (int i = 0; i < result.length; i++) |
73 | |
{ |
74 | 0 | char hi = Character.toLowerCase(hex.charAt(j++)); |
75 | 0 | char lo = Character.toLowerCase(hex.charAt(j++)); |
76 | 0 | result[i] = (byte) ((Character.digit(hi, 16) << 4) | Character.digit(lo, 16)); |
77 | |
} |
78 | |
|
79 | 0 | return result; |
80 | |
} |
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
public static String repeat(char c, int len) |
86 | |
{ |
87 | 0 | return repeat(CharUtils.toString(c), len); |
88 | |
} |
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
public static String toHexString(byte[] bytes) |
94 | |
{ |
95 | 0 | 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 | 0 | if (bytes == null) |
109 | |
{ |
110 | 0 | return null; |
111 | |
} |
112 | |
|
113 | 0 | int numBytes = bytes.length; |
114 | 0 | StringBuffer str = new StringBuffer(numBytes * 2); |
115 | |
|
116 | 0 | String table = (uppercase ? HEX_CHARACTERS_UC : HEX_CHARACTERS); |
117 | |
|
118 | 0 | for (int i = 0; i < numBytes; i++) |
119 | |
{ |
120 | 0 | str.append(table.charAt(bytes[i] >>> 4 & 0x0f)); |
121 | 0 | str.append(table.charAt(bytes[i] & 0x0f)); |
122 | |
} |
123 | |
|
124 | 0 | return str.toString(); |
125 | |
} |
126 | |
|
127 | |
|
128 | |
private static final String HEX_CHARACTERS = "0123456789abcdef"; |
129 | 0 | private static final String HEX_CHARACTERS_UC = HEX_CHARACTERS.toUpperCase(); |
130 | |
|
131 | |
} |