View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
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   * <code>StringUtils</code> contains useful methods for manipulating Strings.
16   */
17  // @ThreadSafe
18  public class StringUtils extends org.apache.commons.lang.StringUtils
19  {
20  
21      /**
22       * Like {@link org.mule.util.StringUtils#split(String, String)}, but
23       * additionally trims whitespace from the result tokens.
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       * Convert a hexadecimal string into its byte representation.
56       * 
57       * @param hex The hexadecimal string.
58       * @return The converted bytes or <code>null</code> if the hex String is null.
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       * Like {@link #repeat(String, int)} but with a single character as argument.
88       */
89      public static String repeat(char c, int len)
90      {
91          return repeat(CharUtils.toString(c), len);
92      }
93  
94      /**
95       * @see #toHexString(byte[])
96       */
97      public static String toHexString(byte[] bytes)
98      {
99          return StringUtils.toHexString(bytes, false);
100     }
101 
102     /**
103      * Convert a byte array to a hexadecimal string.
104      * 
105      * @param bytes The bytes to format.
106      * @param uppercase When <code>true</code> creates uppercase hex characters
107      *            instead of lowercase (the default).
108      * @return A hexadecimal representation of the specified bytes.
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     // lookup tables needed for toHexString(byte[], boolean)
132     private static final String HEX_CHARACTERS = "0123456789abcdef";
133     private static final String HEX_CHARACTERS_UC = HEX_CHARACTERS.toUpperCase();
134 
135 }