1 21 22 package org.apache.derbyTesting.functionTests.util; 23 24 25 public class Formatters { 26 27 static final char[] hexDigits = { '0', '1', '2', '3', 28 '4', '5', '6', '7', 29 '8', '9', 'A', 'B', 30 'C', 'D', 'E', 'F' }; 31 32 37 public static String format(String in) { 38 if (in == null) 39 return null; 40 41 StringBuffer out = new StringBuffer (in.length()); 42 char hexValue[] = new char[4]; 43 44 for (int i = 0; i < in.length(); i++) { 45 char inChar = in.charAt(i); 46 47 if (inChar < 128) { 48 out.append(inChar); 49 } else { 50 out.append("\\u"); 51 52 int number = (int) inChar; 53 54 int digit = number % 16; 55 56 hexValue[3] = hexDigits[digit]; 57 58 number /= 16; 59 60 digit = number % 16; 61 62 hexValue[2] = hexDigits[digit]; 63 64 number /= 16; 65 66 digit = number %16; 67 68 hexValue[1] = hexDigits[digit]; 69 70 number /= 16; 71 72 digit = number % 16; 73 74 hexValue[0] = hexDigits[digit]; 75 76 out.append(hexValue); 77 } 78 } 79 80 return out.toString(); 81 } 82 83 84 92 public static String repeatChar(String c, int repeatCount) 93 { 94 char ch = c.charAt(0); 95 96 char[] chArray = new char[repeatCount]; 97 for (int i = 0; i < repeatCount; i++) 98 { 99 chArray[i] = ch; 100 } 101 102 return new String (chArray); 103 104 } 105 106 113 public static String padString(String oldValue, int size) 114 { 115 String newValue = oldValue; 116 if (newValue != null) 117 { 118 char [] newCharArr = new char[size]; 119 oldValue.getChars(0,oldValue.length(),newCharArr,0); 120 java.util.Arrays.fill(newCharArr,oldValue.length(), 121 newCharArr.length -1, ' '); 122 newValue = new String (newCharArr); 123 } 124 125 return newValue; 126 } 127 128 } 129 | Popular Tags |