Coverage Report - org.mule.util.StringUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
StringUtils
0%
0/37
0%
0/22
4
 
 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  0
 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  0
         if (string == null)
 28  
         {
 29  0
             return null;
 30  
         }
 31  
 
 32  0
         if (isEmpty(string))
 33  
         {
 34  0
             return ArrayUtils.EMPTY_STRING_ARRAY;
 35  
         }
 36  
 
 37  0
         String[] rawTokens = split(string, delim);
 38  0
         List tokens = new ArrayList();
 39  
         String token;
 40  0
         if (rawTokens != null)
 41  
         {
 42  0
             for (int i = 0; i < rawTokens.length; i++)
 43  
             {
 44  0
                 token = trim(rawTokens[i]);
 45  0
                 if (isNotEmpty(token))
 46  
                 {
 47  0
                     tokens.add(token);
 48  
                 }
 49  
             }
 50  
         }
 51  0
         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  0
         if (hex == null)
 63  
         {
 64  0
             return null;
 65  
         }
 66  
 
 67  0
         int stringLength = hex.length();
 68  0
         if (stringLength % 2 != 0)
 69  
         {
 70  0
             throw new IllegalArgumentException("Hex String must have even number of characters!");
 71  
         }
 72  
 
 73  0
         byte[] result = new byte[stringLength / 2];
 74  
 
 75  0
         int j = 0;
 76  0
         for (int i = 0; i < result.length; i++)
 77  
         {
 78  0
             char hi = Character.toLowerCase(hex.charAt(j++));
 79  0
             char lo = Character.toLowerCase(hex.charAt(j++));
 80  0
             result[i] = (byte) ((Character.digit(hi, 16) << 4) | Character.digit(lo, 16));
 81  
         }
 82  
 
 83  0
         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  0
         return repeat(CharUtils.toString(c), len);
 92  
     }
 93  
 
 94  
     /**
 95  
      * @see #toHexString(byte[])
 96  
      */
 97  
     public static String toHexString(byte[] bytes)
 98  
     {
 99  0
         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  0
         if (bytes == null)
 113  
         {
 114  0
             return null;
 115  
         }
 116  
 
 117  0
         int numBytes = bytes.length;
 118  0
         StringBuffer str = new StringBuffer(numBytes * 2);
 119  
 
 120  0
         String table = (uppercase ? HEX_CHARACTERS_UC : HEX_CHARACTERS);
 121  
 
 122  0
         for (int i = 0; i < numBytes; i++)
 123  
         {
 124  0
             str.append(table.charAt(bytes[i] >>> 4 & 0x0f));
 125  0
             str.append(table.charAt(bytes[i] & 0x0f));
 126  
         }
 127  
 
 128  0
         return str.toString();
 129  
     }
 130  
 
 131  
     // lookup tables needed for toHexString(byte[], boolean)
 132  
     private static final String HEX_CHARACTERS = "0123456789abcdef";
 133  0
     private static final String HEX_CHARACTERS_UC = HEX_CHARACTERS.toUpperCase();
 134  
 
 135  
 }