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