1 18 package org.apache.tools.ant.util; 19 20 import java.io.PrintWriter ; 21 import java.io.StringWriter ; 22 import java.util.Vector ; 23 24 28 public final class StringUtils { 29 30 33 private StringUtils() { 34 } 35 36 37 public static final String LINE_SEP = System.getProperty("line.separator"); 38 39 45 public static Vector lineSplit(String data) { 46 return split(data, '\n'); 47 } 48 49 56 public static Vector split(String data, int ch) { 57 Vector elems = new Vector (); 58 int pos = -1; 59 int i = 0; 60 while ((pos = data.indexOf(ch, i)) != -1) { 61 String elem = data.substring(i, pos); 62 elems.addElement(elem); 63 i = pos + 1; 64 } 65 elems.addElement(data.substring(i)); 66 return elems; 67 } 68 69 76 public static String replace(String data, String from, String to) { 77 StringBuffer buf = new StringBuffer (data.length()); 78 int pos = -1; 79 int i = 0; 80 while ((pos = data.indexOf(from, i)) != -1) { 81 buf.append(data.substring(i, pos)).append(to); 82 i = pos + from.length(); 83 } 84 buf.append(data.substring(i)); 85 return buf.toString(); 86 } 87 88 93 public static String getStackTrace(Throwable t) { 94 StringWriter sw = new StringWriter (); 95 PrintWriter pw = new PrintWriter (sw, true); 96 t.printStackTrace(pw); 97 pw.flush(); 98 pw.close(); 99 return sw.toString(); 100 } 101 102 116 public static boolean endsWith(StringBuffer buffer, String suffix) { 117 if (suffix.length() > buffer.length()) { 118 return false; 119 } 120 int endIndex = suffix.length() - 1; 128 int bufferIndex = buffer.length() - 1; 129 while (endIndex >= 0) { 130 if (buffer.charAt(bufferIndex) != suffix.charAt(endIndex)) { 131 return false; 132 } 133 bufferIndex--; 134 endIndex--; 135 } 136 return true; 137 } 138 139 150 public static String resolveBackSlash(String input) { 151 StringBuffer b = new StringBuffer (); 152 boolean backSlashSeen = false; 153 for (int i = 0; i < input.length(); ++i) { 154 char c = input.charAt(i); 155 if (!backSlashSeen) { 156 if (c == '\\') { 157 backSlashSeen = true; 158 } else { 159 b.append(c); 160 } 161 } else { 162 switch (c) { 163 case '\\': 164 b.append((char) '\\'); 165 break; 166 case 'n': 167 b.append((char) '\n'); 168 break; 169 case 'r': 170 b.append((char) '\r'); 171 break; 172 case 't': 173 b.append((char) '\t'); 174 break; 175 case 'f': 176 b.append((char) '\f'); 177 break; 178 case 's': 179 b.append(" \t\n\r\f"); 180 break; 181 default: 182 b.append(c); 183 } 184 backSlashSeen = false; 185 } 186 } 187 return b.toString(); 188 } 189 190 198 public static long parseHumanSizes(String humanSize) throws Exception { 199 final long KILOBYTE = 1024; 200 final long MEGABYTE = KILOBYTE * 1024; 201 final long GIGABYTE = MEGABYTE * 1024; 202 final long TERABYTE = GIGABYTE * 1024; 203 final long PETABYTE = TERABYTE * 1024; 204 if(!Character.isDigit(humanSize.charAt(humanSize.length()-1))) { 206 char c = humanSize.charAt(humanSize.length()-1); 207 long value = Long.valueOf(humanSize.substring(0, humanSize.length()-1)).longValue(); 208 switch (c) { 209 case 'K': 210 return value * KILOBYTE; 211 case 'M': 212 return value * MEGABYTE; 213 case 'G': 214 return value * GIGABYTE; 215 case 'T': 216 return value * TERABYTE; 217 case 'P': 218 return value * PETABYTE; 219 default: 220 return value; 221 } 222 } else { 223 return Long.parseLong(humanSize); 224 } 225 } 226 } 227 | Popular Tags |