View Javadoc

1   /*
2    * $Id: StringUtils.java 9970 2007-12-04 10:13:54Z holger $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
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   * <code>StringUtils</code> contains useful methods for manipulating Strings.
20   */
21  // @ThreadSafe
22  public class StringUtils extends org.apache.commons.lang.StringUtils
23  {
24  
25      /**
26       * Like {@link org.mule.util.StringUtils#split(String, String)}, but
27       * additionally trims whitespace from the result tokens.
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       * Convert a hexadecimal string into its byte representation.
60       * 
61       * @param hex The hexadecimal string.
62       * @return The converted bytes or <code>null</code> if the hex String is null.
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       * Like {@link #repeat(String, int)} but with a single character as argument.
92       */
93      public static String repeat(char c, int len)
94      {
95          return repeat(CharUtils.toString(c), len);
96      }
97  
98      /**
99       * @see #toHexString(byte[])
100      */
101     public static String toHexString(byte[] bytes)
102     {
103         return StringUtils.toHexString(bytes, false);
104     }
105 
106     /**
107      * Convert a byte array to a hexadecimal string.
108      * 
109      * @param bytes The bytes to format.
110      * @param uppercase When <code>true</code> creates uppercase hex characters
111      *            instead of lowercase (the default).
112      * @return A hexadecimal representation of the specified bytes.
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     // lookup tables needed for toHexString(byte[], boolean)
136     private static final String HEX_CHARACTERS = "0123456789abcdef";
137     private static final String HEX_CHARACTERS_UC = HEX_CHARACTERS.toUpperCase();
138 
139 }