KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > util > StringUtils


1 /*
2  * $Id: StringUtils.java 3798 2006-11-04 04:07:14Z aperepel $
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 MuleSource MPL
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 org.apache.commons.lang.CharUtils;
14
15 /**
16  * <code>StringUtils</code> contains useful methods for manipulating Strings.
17  */

18 // @ThreadSafe
19
public class StringUtils extends org.apache.commons.lang.StringUtils
20 {
21
22     /**
23      * Like {@link org.apache.commons.lang.StringUtils#split(String, String)}, but
24      * additionally trims whitespace from the result tokens.
25      */

26     public static String JavaDoc[] splitAndTrim(String JavaDoc string, String JavaDoc delim)
27     {
28         if (string == null)
29         {
30             return null;
31         }
32
33         if (isEmpty(string))
34         {
35             return ArrayUtils.EMPTY_STRING_ARRAY;
36         }
37
38         String JavaDoc[] tokens = split(string, delim);
39         if (tokens != null)
40         {
41             for (int i = 0; i < tokens.length; i++)
42             {
43                 tokens[i] = trim(tokens[i]);
44             }
45         }
46
47         return tokens;
48     }
49
50     /**
51      * Convert a hexadecimal string into its byte representation.
52      *
53      * @param hex The hexadecimal string.
54      * @return The converted bytes or <code>null</code> if the hex String is null.
55      */

56     public static byte[] hexStringToByteArray(String JavaDoc hex)
57     {
58         if (hex == null)
59         {
60             return null;
61         }
62
63         int stringLength = hex.length();
64         if (stringLength % 2 != 0)
65         {
66             throw new IllegalArgumentException JavaDoc("Hex String must have even number of characters!");
67         }
68
69         byte[] result = new byte[stringLength / 2];
70
71         int j = 0;
72         for (int i = 0; i < result.length; i++)
73         {
74             char hi = Character.toLowerCase(hex.charAt(j++));
75             char lo = Character.toLowerCase(hex.charAt(j++));
76             result[i] = (byte)((Character.digit(hi, 16) << 4) | Character.digit(lo, 16));
77         }
78
79         return result;
80     }
81
82     /**
83      * Like {@link #repeat(String, int)} but with a single character as argument.
84      */

85     public static String JavaDoc repeat(char c, int len)
86     {
87         return repeat(CharUtils.toString(c), len);
88     }
89
90     /**
91      * @see #toHexString(byte[])
92      */

93     public static String JavaDoc toHexString(byte[] bytes)
94     {
95         return StringUtils.toHexString(bytes, false);
96     }
97
98     /**
99      * Convert a byte array to a hexadecimal string.
100      *
101      * @param bytes The bytes to format.
102      * @param uppercase When <code>true</code> creates uppercase hex characters
103      * instead of lowercase (the default).
104      * @return A hexadecimal representation of the specified bytes.
105      */

106     public static String JavaDoc toHexString(byte[] bytes, boolean uppercase)
107     {
108         if (bytes == null)
109         {
110             return null;
111         }
112
113         int numBytes = bytes.length;
114         StringBuffer JavaDoc str = new StringBuffer JavaDoc(numBytes * 2);
115
116         String JavaDoc table = (uppercase ? HEX_CHARACTERS_UC : HEX_CHARACTERS);
117
118         for (int i = 0; i < numBytes; i++)
119         {
120             str.append(table.charAt(bytes[i] >>> 4 & 0x0f));
121             str.append(table.charAt(bytes[i] & 0x0f));
122         }
123
124         return str.toString();
125     }
126
127     // lookup tables needed for toHexString(byte[], boolean)
128
private static final String JavaDoc HEX_CHARACTERS = "0123456789abcdef";
129     private static final String JavaDoc HEX_CHARACTERS_UC = HEX_CHARACTERS.toUpperCase();
130
131 }
132
Popular Tags