Coverage Report - org.mule.util.StringUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
StringUtils
97%
36/37
95%
21/22
4
 
 1  
 /*
 2  
  * $Id: StringUtils.java 9968 2007-12-03 21:40:42Z tcarlson $
 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  0
 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  466
         if (string == null)
 32  
         {
 33  4
             return null;
 34  
         }
 35  
 
 36  462
         if (isEmpty(string))
 37  
         {
 38  2
             return ArrayUtils.EMPTY_STRING_ARRAY;
 39  
         }
 40  
 
 41  460
         String[] rawTokens = split(string, delim);
 42  460
         List tokens = new ArrayList();
 43  
         String token;
 44  460
         if (rawTokens != null)
 45  
         {
 46  4378
             for (int i = 0; i < rawTokens.length; i++)
 47  
             {
 48  3918
                 token = trim(rawTokens[i]);
 49  3918
                 if (isNotEmpty(token))
 50  
                 {
 51  3916
                     tokens.add(token);
 52  
                 }
 53  
             }
 54  
         }
 55  460
         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  30
         if (hex == null)
 67  
         {
 68  2
             return null;
 69  
         }
 70  
 
 71  28
         int stringLength = hex.length();
 72  28
         if (stringLength % 2 != 0)
 73  
         {
 74  2
             throw new IllegalArgumentException("Hex String must have even number of characters!");
 75  
         }
 76  
 
 77  26
         byte[] result = new byte[stringLength / 2];
 78  
 
 79  26
         int j = 0;
 80  96
         for (int i = 0; i < result.length; i++)
 81  
         {
 82  70
             char hi = Character.toLowerCase(hex.charAt(j++));
 83  70
             char lo = Character.toLowerCase(hex.charAt(j++));
 84  70
             result[i] = (byte) ((Character.digit(hi, 16) << 4) | Character.digit(lo, 16));
 85  
         }
 86  
 
 87  26
         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  4250
         return repeat(CharUtils.toString(c), len);
 96  
     }
 97  
 
 98  
     /**
 99  
      * @see #toHexString(byte[])
 100  
      */
 101  
     public static String toHexString(byte[] bytes)
 102  
     {
 103  12
         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  38
         if (bytes == null)
 117  
         {
 118  2
             return null;
 119  
         }
 120  
 
 121  36
         int numBytes = bytes.length;
 122  36
         StringBuffer str = new StringBuffer(numBytes * 2);
 123  
 
 124  36
         String table = (uppercase ? HEX_CHARACTERS_UC : HEX_CHARACTERS);
 125  
 
 126  146
         for (int i = 0; i < numBytes; i++)
 127  
         {
 128  110
             str.append(table.charAt(bytes[i] >>> 4 & 0x0f));
 129  110
             str.append(table.charAt(bytes[i] & 0x0f));
 130  
         }
 131  
 
 132  36
         return str.toString();
 133  
     }
 134  
 
 135  
     // lookup tables needed for toHexString(byte[], boolean)
 136  
     private static final String HEX_CHARACTERS = "0123456789abcdef";
 137  2
     private static final String HEX_CHARACTERS_UC = HEX_CHARACTERS.toUpperCase();
 138  
 
 139  
 }