1 package org.enhydra.shark.utilities; 2 3 import java.io.*; 4 import java.lang.reflect.Method ; 5 import java.security.MessageDigest ; 6 import java.util.*; 7 8 import org.enhydra.shark.api.RootException; 9 10 16 public class MiscUtilities { 17 18 public static void traverse(File f,Collection c,FileFilter filter) { 20 if (!f.exists()) { 21 return; 22 } 23 if (f.isDirectory()) { 24 File[] children = f.listFiles(filter); 25 for (int i=0; i<children.length; i++) { 26 traverse(children[i],c,filter); 27 } 28 } else { 29 c.add(f); 30 } 31 } 32 33 34 public static byte[] serialize(Object obj) throws IOException { 35 ByteArrayOutputStream bout = new ByteArrayOutputStream(); 37 ObjectOutputStream oout = new ObjectOutputStream(bout); 38 oout.writeObject(obj); 39 oout.flush(); 40 byte array[] = bout.toByteArray(); 41 oout.close(); 42 bout.close(); 43 return array; 45 } 46 47 public static Object deserialize(byte[]array) throws Throwable { 48 ObjectInputStream rin = new ObjectInputStream 50 (new ByteArrayInputStream(array)); 51 Object obj = rin.readObject(); 52 rin.close(); 53 return obj; 55 } 56 57 public static long getAbsoluteTimeInUTCFormat () { 59 return System.currentTimeMillis(); 60 } 61 62 68 public static String [] tokenize(String input,String boundary) { 69 if (input==null) input=""; 70 Vector v = new Vector(); 71 StringTokenizer t = new StringTokenizer(input,boundary); 72 String cmd[]; 73 74 while (t.hasMoreTokens()) 75 v.addElement(t.nextToken()); 76 cmd = new String [v.size()]; 77 for (int i = 0; i < cmd.length; i++) 78 cmd[i] = (String )v.elementAt(i); 79 80 return cmd; 81 } 82 83 public static String getTimeDiff (long tStart,long tEnd) { 84 long sec=1000; 85 long min=sec*60; 86 long hour=min*60; 87 long day=hour*24; 88 long month=day*30; 89 long year=365*day; 90 91 long diffInMills=tEnd-tStart; 93 if (diffInMills<min) { 94 return String.valueOf(diffInMills/sec)+" [s]"; 95 } else if (diffInMills<hour) { 96 long m=diffInMills/min; 97 long s=(diffInMills-m*min)/sec; 98 return String.valueOf(m)+" [min] "+String.valueOf(s)+" [s]"; 99 } else if (diffInMills<day) { 100 long h=diffInMills/hour; 101 long m=(diffInMills-h*hour)/min; 102 long s=(diffInMills-h*hour-m*min)/sec; 103 return String.valueOf(h)+" [h] "+String.valueOf(m)+" [min] "+ 104 String.valueOf(s)+" [s]"; 105 } else if (diffInMills<month) { 106 long d=diffInMills/day; 107 long h=(diffInMills-d*day)/hour; 108 long m=(diffInMills-d*day-h*hour)/min; 109 long s=(diffInMills-d*day-h*hour-m*min)/sec; 110 return String.valueOf(d)+" [d] "+String.valueOf(h)+" [h] "+ 111 String.valueOf(m)+" [min] "+String.valueOf(s)+" [s]"; 112 } else if (diffInMills<year) { 113 long mn=diffInMills/month; 114 long d=(diffInMills-mn*month)/day; 115 long h=(diffInMills-mn*month-d*day)/hour; 116 long m=(diffInMills-mn*month-d*day-h*hour)/min; 117 long s=(diffInMills-mn*month-d*day-h*hour-m*min)/sec; 118 return String.valueOf(mn)+" [m] "+String.valueOf(d)+" [d] "+ 119 String.valueOf(h)+" [h] "+String.valueOf(m)+ 120 " [min] "+String.valueOf(s)+" [s]"; 121 } else { long y=diffInMills/year; 123 long mn=(diffInMills-y*year)/month; 124 long d=(diffInMills-y*year-mn*month)/day; 125 long h=(diffInMills-y*year-mn*month-d*day)/hour; 126 long m=(diffInMills-y*year-mn*month-d*day-h*hour)/min; 127 long s=(diffInMills-y*year-mn*month-d*day-h*hour-m*min)/sec; 128 return String.valueOf(y)+" [y] "+String.valueOf(mn)+" [m] "+ 129 String.valueOf(d)+" [d] "+String.valueOf(h)+" [h] "+ 130 String.valueOf(m)+" [min] "+String.valueOf(s)+" [s]"; 131 } 132 } 133 134 135 public static void copyFile(String src,String dest) throws IOException { 136 FileInputStream in=new FileInputStream(src); 137 FileOutputStream out=new FileOutputStream(dest); 138 byte buffer[] = new byte[1024]; 139 int read = -1; 140 while ((read = in.read(buffer, 0, 1024)) != -1) { 141 out.write(buffer, 0, read); 142 } 143 out.flush(); 144 out.close(); 145 in.close(); 146 } 147 148 149 153 public static String convertMillisecondsToDateAndTimeString (long cdt) { 154 String dateSeparator="-"; 155 Calendar cal=new GregorianCalendar(); 156 cal.setTime(new Date(cdt)); 157 159 int YYYY=cal.get(Calendar.YEAR); 160 int MM=cal.get(Calendar.MONTH)+1; 161 int DD=cal.get(Calendar.DAY_OF_MONTH); 162 int HH=cal.get(Calendar.HOUR_OF_DAY); 163 int mm=cal.get(Calendar.MINUTE); 164 int ss=cal.get(Calendar.SECOND); 165 int mmmm=cal.get(Calendar.MILLISECOND); 166 167 String dateTime=""; 168 169 dateTime=dateTime+String.valueOf(YYYY)+dateSeparator; 170 if (MM<10) { 171 dateTime=dateTime+"0"; 172 } 173 dateTime=dateTime+String.valueOf(MM)+dateSeparator; 174 if (DD<10) { 175 dateTime=dateTime+"0"; 176 } 177 dateTime=dateTime+String.valueOf(DD)+dateSeparator; 178 179 if (cal.get(Calendar.AM_PM)==Calendar.PM && HH<12) { 180 HH+=12; 181 } 182 if (HH<10) { 183 dateTime=dateTime+"0"; 184 } 185 dateTime=dateTime+String.valueOf(HH)+dateSeparator; 186 187 if (mm<10) { 188 dateTime=dateTime+"0"; 189 } 190 dateTime=dateTime+String.valueOf(mm)+dateSeparator; 191 192 if (ss<10) { 193 dateTime=dateTime+"0"; 194 } 195 dateTime=dateTime+String.valueOf(ss)+dateSeparator; 196 197 if (mmmm<10) { 198 dateTime=dateTime+"000"; 199 } else if (mmmm<100) { 200 dateTime=dateTime+"00"; 201 } else { 202 dateTime=dateTime+"0"; 203 } 204 dateTime=dateTime+String.valueOf(mmmm); 205 206 return dateTime; 207 } 208 209 212 public static long convertDateAndTimeStringToMilliseconds (String dateTime) { 213 String dateSeparator="-"; 214 String [] dts=tokenize(dateTime,dateSeparator); 215 if (dts==null || dts.length!=7) return -1; 216 217 int YYYY=Integer.parseInt(dts[0]); 218 int MM=Integer.parseInt(dts[1]); 219 int DD=Integer.parseInt(dts[2]); 220 int HH=Integer.parseInt(dts[3]); 221 int mm=Integer.parseInt(dts[4]); 222 int ss=Integer.parseInt(dts[5]); 223 int mmmm=Integer.parseInt(dts[6]); 224 225 Calendar cal=new GregorianCalendar(YYYY,MM,DD,HH,mm,ss); 226 long time=cal.getTime().getTime()+mmmm; 228 229 return time; 230 } 231 232 public static boolean isEmptyString (String str) { 233 return (str==null || str.trim().length()==0); 234 } 235 236 239 public static final boolean isComplexWRD (Object wrd) { 240 return !((wrd instanceof Long ) || (wrd instanceof Double ) || (wrd instanceof Boolean ) || (wrd instanceof Date) 241 || (wrd instanceof String )); 242 } 243 244 253 public static Object cloneWRD (Object wrd) throws Throwable { 254 if (!isComplexWRD(wrd)) { 255 return wrd; 256 } 257 if (wrd==null) { 258 return null; 259 } 260 261 Object ret=null; 262 263 if (wrd instanceof Cloneable ) { 264 Class cls=wrd.getClass(); 266 try { 267 Method mth=cls.getMethod("clone",null); 268 ret=mth.invoke(wrd,null); 269 } catch (Throwable ex) { 271 ex.printStackTrace(); 272 } 273 } 274 275 if (ret==null) { 276 byte[] serVal=serialize(wrd); 278 ret=deserialize(serVal); 279 } 280 281 return ret; 282 } 283 284 public static String passwordDigest(String password) throws RootException { 285 try { 286 MessageDigest md = MessageDigest.getInstance("SHA-1"); 287 byte[] passwd = md.digest(password.getBytes()); 288 StringBuffer buffer = new StringBuffer (); 289 for (int i =0; i < passwd.length; ++i) { 290 byte current = passwd[i]; 291 for (int j = 0; j < 2; ++j) { 292 int nibble = 0xF &((1 == j)? current: current >> 4); 293 buffer.append(Character.forDigit(nibble, 16)); 294 } 295 } 296 return buffer.toString(); 297 } catch (Exception e) { 298 throw new RootException(e); 299 } 300 } 301 302 309 public static String replaceAll(String input, 310 String forReplace, 311 String replaceWith) { 312 if( input == null ) 313 return null; 314 StringBuffer result = new StringBuffer (); 315 boolean hasMore = true; 316 while (hasMore) { 317 int start = input.indexOf(forReplace); 318 int end = start + forReplace.length(); 319 if (start != -1) { 320 result.append(input.substring(0, start) + replaceWith); 321 input = input.substring(end); 322 } else { 323 hasMore = false; 324 result.append(input); 325 } 326 } 327 if (result.toString().equals("")) 328 return input; else 330 return result.toString(); 331 } 332 } 333 | Popular Tags |