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