1 7 package com.inversoft.util; 8 9 10 18 public class StringTools { 19 20 23 public static boolean isEmpty(String str) { 24 return (str == null || str.length() == 0); 25 } 26 27 30 public static boolean isTrimmedEmpty(String str) { 31 return (str == null || str.trim().length() == 0); 32 } 33 34 38 public static boolean isValidBoolean(String str) { 39 return 40 (str != null && 41 (str.equalsIgnoreCase(StringConstants.TRUE_STRING) || 42 str.equalsIgnoreCase(StringConstants.FALSE_STRING)) 43 ); 44 } 45 46 50 public static String cleanString(String str) { 51 return (str == null) ? "" : str; 52 } 53 54 72 public static String [] convertEmpty(String [] values) { 73 if (values != null && values.length == 1 && StringTools.isEmpty(values[0])) { 74 values = null; 75 } 76 77 return values; 78 } 79 80 89 public static String toHex(int[] bytes) { 90 int dataLength = (bytes.length * 2); 91 StringBuffer buf = new StringBuffer (dataLength); 92 for (int i = 0; i < bytes.length; i++) { 93 if (bytes[i] < 0 || bytes[i] > 255) { 94 throw new IllegalArgumentException ("Invalid byte value " + bytes[i]); 95 } 96 97 if (bytes[i] >= 16) { 98 buf.append(Integer.toHexString(bytes[i] / 16)); 99 buf.append(Integer.toHexString(bytes[i] - ((bytes[i] / 16) * 16))); 100 } else { 101 buf.append(Integer.toHexString(bytes[i])); 102 } 103 } 104 105 return buf.toString(); 106 } 107 108 116 public static char toHex(byte hexValue) { 117 if (hexValue < 0 || hexValue >= 16) { 118 throw new IllegalArgumentException ("Invalid hex value"); 119 } 120 121 String str = Integer.toHexString(hexValue); 122 if (str.length() > 1) { 123 throw new IllegalStateException ("JVM toHexString method incorrect"); 124 } 125 126 return str.charAt(0); 127 } 128 129 138 public static byte[] fromHex(String hexString) { 139 int length = hexString.length(); 140 141 if ((length & 0x01) != 0) { 142 throw new IllegalArgumentException ("odd number of characters."); 143 } 144 145 byte[] out = new byte[length >> 1]; 146 147 for (int i = 0, j = 0; j < length; i++) { 149 int f = Character.digit(hexString.charAt(j++), 16) << 4; 150 f = f | Character.digit(hexString.charAt(j++), 16); 151 out[i] = (byte) (f & 0xFF); 152 } 153 154 return out; 155 } 156 157 165 public static byte fromHex(char hexValue) { 166 byte b = (byte) Character.digit(hexValue, 16); 167 if (b == -1) { 168 throw new IllegalArgumentException ("Invalid hex character"); 169 } 170 171 return b; 172 } 173 } | Popular Tags |