1 14 15 package org.quickserver.util; 16 17 import java.io.*; 18 19 23 public class MyString { 24 private static Runtime runtime = Runtime.getRuntime(); 25 private static java.text.DecimalFormat doublePrcNum = 26 new java.text.DecimalFormat ("#,##0.00"); 27 28 public static String replace(String source, String key, 29 String with) { 30 if(source==null) 31 throw new NullPointerException ("Parameter -> source was null"); 32 if(key==null) 33 throw new NullPointerException ("Parameter -> key was null"); 34 if(with==null) 35 throw new NullPointerException ("Parameter -> with was null"); 36 37 int start=0; 38 int end=0; 39 String result=""; 40 41 start=source.indexOf(key); 42 end=start+key.length(); 43 44 if(start==-1) 45 return null; 46 47 result=source.substring(0,start); 48 result+=with; 49 result+=source.substring(end,source.length()); 50 51 return result; 52 } 53 54 public static String replaceAll(String source, String key, 55 String with) { 56 if(source==null) 57 throw new NullPointerException ("Parameter -> source was null"); 58 if(key==null) 59 throw new NullPointerException ("Parameter -> key was null"); 60 if(with==null) 61 throw new NullPointerException ("Parameter -> with was null"); 62 63 String temp=""; 64 65 while(true) { 66 temp=""; 67 temp=replace(source,key,with); 68 if(temp==null) 69 break; 70 else 71 source=temp; 72 } 73 74 return source; 75 } 76 77 78 public static int replaceCount(String source, 79 String key) { 80 if(source==null) 81 throw new NullPointerException ("Parameter -> source was null"); 82 if(key==null) 83 throw new NullPointerException ("Parameter -> key was null"); 84 85 int count=0; 86 String result=""; 87 String temp=""; 88 89 result=source; 90 while(true) { 91 temp=""; 92 temp=replace(result,key,""); 93 if(temp==null) { 94 break; 95 } 96 else { 97 result=temp; 98 count++; 99 } 100 } 101 102 return count; 103 } 104 105 public static String replaceAllNo(String source, 106 String with) { 107 if(source==null) 108 throw new NullPointerException ("One of parameter -> source was null"); 109 if(with==null) 110 throw new NullPointerException ("One of parameter -> with was null"); 111 112 for(int i=0;i<10;i++) 113 source=replaceAll(source,""+i,with); 114 115 return source; 116 } 117 118 119 public static String removeAllHtmlSpChar(String source) { 120 String temp = source; 121 temp = replaceAll(temp," "," "); 122 temp = replaceAll(temp,"<","<"); 123 temp = replaceAll(temp,">",">"); 124 temp = replaceAll(temp,"&","&"); 125 temp = replaceAll(temp,""","\""); 126 return temp; 127 } 128 129 public static String replaceTags(String source, String with) { 132 if(source==null) 133 throw new NullPointerException ("One of parameter -> source was null"); 134 if(with==null) 135 throw new NullPointerException ("One of parameter -> with was null"); 136 137 int start=0; 138 int end=0; 139 int error=0; 140 String result=""; 141 142 start=source.indexOf("<"); 143 end=source.indexOf(">",start+1); 144 145 error=source.indexOf("<",start+1); 146 if(error!=-1 && error<end) 147 throw new IllegalArgumentException ("< found before >"); 148 149 if(start==-1 || end==-1) 150 return null; 151 152 result=source.substring(0,start); 153 result+=with; 154 result+=source.substring(end+1,source.length()); 155 156 return result; 157 } 158 159 public static String replaceAllTags(String source, String with) { 160 if(source==null) 161 throw new NullPointerException ("One of parameter -> source was null"); 162 if(with==null) 163 throw new NullPointerException ("One of parameter -> with was null"); 164 165 String temp=""; 166 167 while(true) { 168 temp=""; 169 temp=replaceTags(source,with); 170 if(temp==null) 171 break; 172 else 173 source=temp; 174 } 175 176 return source; 177 } 178 179 183 public static String getStackTrace(Throwable e) { 184 StringWriter writer = new StringWriter(1024); 185 e.printStackTrace(new PrintWriter(writer)); 186 return writer.toString(); 187 } 188 189 193 public static String getMemInfo(float bytes) { 194 if(bytes<1024) { 195 return doublePrcNum.format(bytes)+" B"; 196 } 197 198 bytes = bytes/1024; 199 if(bytes<1024) { 200 return doublePrcNum.format(bytes)+" KB"; 201 } 202 203 bytes = bytes/1024; 204 if(bytes<1024) { 205 return doublePrcNum.format(bytes)+" MB"; 206 } 207 208 bytes = bytes/1024; 209 return doublePrcNum.format(bytes)+" GB"; 210 } 211 212 216 public static String getSystemInfo(String version) { 217 StringBuffer sb = new StringBuffer (); 218 sb.append("---- System Info Start ---"); 219 sb.append("\r\n"); 220 221 sb.append("QuickServer v"); 222 sb.append(version); 223 sb.append(" is being used."); 224 sb.append("\r\n"); 225 226 sb.append("Java VM v"); 227 sb.append(System.getProperty("java.version")); 228 sb.append(" is being used."); 229 sb.append("\r\n"); 230 231 sb.append("Operating System: "); 232 sb.append(System.getProperty("os.name")); 233 sb.append(" "); 234 sb.append(System.getProperty("os.version")); 235 sb.append("\r\n"); 236 237 sb.append("Current working directory: "); 238 sb.append(System.getProperty("user.dir")); 239 sb.append("\r\n"); 240 241 sb.append("Class/s loaded from: "); 242 sb.append(new MyString().getClass().getProtectionDomain().getCodeSource().getLocation()); 243 sb.append("\r\n"); 244 245 sb.append("Total memory currently available: "); 246 sb.append(MyString.getMemInfo(runtime.totalMemory())); 247 sb.append("\r\n"); 248 sb.append("Memory currently in use: "); 249 sb.append(MyString.getMemInfo(runtime.totalMemory()-runtime.freeMemory())); 250 sb.append("\r\n"); 251 sb.append("Maximum memory available: "); 252 sb.append(MyString.getMemInfo(runtime.maxMemory())); 253 sb.append("\r\n"); 254 255 256 sb.append("---- System Info End ---"); 257 258 return sb.toString(); 259 } 260 261 public static String alignRight(String data, int len) { 262 StringBuffer sb = new StringBuffer (data); 263 while(sb.length()<len) { 264 sb.insert(0, ' '); 265 } 266 return sb.toString(); 267 } 268 269 public static String alignLeft(String data, int len) { 270 StringBuffer sb = new StringBuffer (data); 271 while(sb.length()<len) { 272 sb.append( ' '); 273 } 274 return sb.toString(); 275 } 276 } 277 | Popular Tags |