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