Coverage Report - org.mule.util.StringUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
StringUtils
0%
0/34
0%
0/10
3.8
 
 1  
 /*
 2  
  * $Id: StringUtils.java 7976 2007-08-21 14:26:13Z dirk.olmes $
 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 org.apache.commons.lang.CharUtils;
 14  
 
 15  
 /**
 16  
  * <code>StringUtils</code> contains useful methods for manipulating Strings.
 17  
  */
 18  
 // @ThreadSafe
 19  0
 public class StringUtils extends org.apache.commons.lang.StringUtils
 20  
 {
 21  
 
 22  
     /**
 23  
      * Like {@link org.mule.util.StringUtils#split(String, String)}, but
 24  
      * additionally trims whitespace from the result tokens.
 25  
      */
 26  
     public static String[] splitAndTrim(String string, String delim)
 27  
     {
 28  0
         if (string == null)
 29  
         {
 30  0
             return null;
 31  
         }
 32  
 
 33  0
         if (isEmpty(string))
 34  
         {
 35  0
             return ArrayUtils.EMPTY_STRING_ARRAY;
 36  
         }
 37  
 
 38  0
         String[] tokens = split(string, delim);
 39  0
         if (tokens != null)
 40  
         {
 41  0
             for (int i = 0; i < tokens.length; i++)
 42  
             {
 43  0
                 tokens[i] = trim(tokens[i]);
 44  
             }
 45  
         }
 46  
 
 47  0
         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 hex)
 57  
     {
 58  0
         if (hex == null)
 59  
         {
 60  0
             return null;
 61  
         }
 62  
 
 63  0
         int stringLength = hex.length();
 64  0
         if (stringLength % 2 != 0)
 65  
         {
 66  0
             throw new IllegalArgumentException("Hex String must have even number of characters!");
 67  
         }
 68  
 
 69  0
         byte[] result = new byte[stringLength / 2];
 70  
 
 71  0
         int j = 0;
 72  0
         for (int i = 0; i < result.length; i++)
 73  
         {
 74  0
             char hi = Character.toLowerCase(hex.charAt(j++));
 75  0
             char lo = Character.toLowerCase(hex.charAt(j++));
 76  0
             result[i] = (byte) ((Character.digit(hi, 16) << 4) | Character.digit(lo, 16));
 77  
         }
 78  
 
 79  0
         return result;
 80  
     }
 81  
 
 82  
     /**
 83  
      * Like {@link #repeat(String, int)} but with a single character as argument.
 84  
      */
 85  
     public static String repeat(char c, int len)
 86  
     {
 87  0
         return repeat(CharUtils.toString(c), len);
 88  
     }
 89  
 
 90  
     /**
 91  
      * @see #toHexString(byte[])
 92  
      */
 93  
     public static String toHexString(byte[] bytes)
 94  
     {
 95  0
         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 toHexString(byte[] bytes, boolean uppercase)
 107  
     {
 108  0
         if (bytes == null)
 109  
         {
 110  0
             return null;
 111  
         }
 112  
 
 113  0
         int numBytes = bytes.length;
 114  0
         StringBuffer str = new StringBuffer(numBytes * 2);
 115  
 
 116  0
         String table = (uppercase ? HEX_CHARACTERS_UC : HEX_CHARACTERS);
 117  
 
 118  0
         for (int i = 0; i < numBytes; i++)
 119  
         {
 120  0
             str.append(table.charAt(bytes[i] >>> 4 & 0x0f));
 121  0
             str.append(table.charAt(bytes[i] & 0x0f));
 122  
         }
 123  
 
 124  0
         return str.toString();
 125  
     }
 126  
 
 127  
     // lookup tables needed for toHexString(byte[], boolean)
 128  
     private static final String HEX_CHARACTERS = "0123456789abcdef";
 129  0
     private static final String HEX_CHARACTERS_UC = HEX_CHARACTERS.toUpperCase();
 130  
 
 131  
 }