1 package jpl.util; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileNotFoundException; 6 import java.io.FileOutputStream; 7 import java.io.IOException; 8 import java.util.NoSuchElementException; 9 import java.util.Random; 10 import java.util.StringTokenizer; 11 12 public class Util 13 { 14 private static Random r = new Random(); 15 dir_to_members( String dir)16 public static String[] dir_to_members( String dir) 17 { 18 String[] ns = (new File(dir)).list(); 19 int len = ns.length; 20 String[] ps = new String[len]; 21 for ( int i=0 ; i<len ; i++ ) 22 { 23 try { 24 ps[i] = (new File(dir,ns[i])).getCanonicalPath().toString(); 25 } 26 catch ( IOException e ) 27 { 28 ps[i] = ""; 29 } 30 } 31 return ps; 32 } 33 dir_to_member_paths( String dir)34 public static String[] dir_to_member_paths( String dir) 35 { 36 String[] ns = (new File(dir)).list(); 37 int len = ns.length; 38 String[] ps = new String[len]; 39 for ( int i=0 ; i<len ; i++ ) 40 { 41 try { 42 ps[i] = (new File(dir,ns[i])).getCanonicalPath(); 43 } 44 catch ( IOException e ) 45 { 46 ps[i] = ""; 47 } 48 } 49 return ps; 50 } 51 dir_to_member_names( String dir)52 public static String[] dir_to_member_names( String dir) 53 { 54 String[] ns = (new File(dir)).list(); 55 int len = ns.length; 56 String[] ps = new String[len]; 57 for ( int i=0 ; i<len ; i++ ) 58 { 59 ps[i] = (new File(dir,ns[i])).getName(); 60 } 61 return ps; 62 } 63 spawn_in_out_err( String command, String infile, String outfile, String errfile)64 public static int spawn_in_out_err( String command, String infile, String outfile, String errfile) 65 { 66 Process p; 67 68 try { 69 p = Runtime.getRuntime().exec(command); 70 } 71 catch ( IOException e) 72 { 73 return -1; 74 } 75 76 try { 77 if ( infile != null ) 78 { 79 ( new Xfer( new FileInputStream(infile), 80 p.getOutputStream() 81 )).start(); 82 } 83 if ( outfile != null ) 84 { 85 ( new Xfer( p.getInputStream(), 86 new FileOutputStream(outfile) 87 )).start(); 88 } 89 if ( errfile != null ) 90 { 91 ( new Xfer( p.getErrorStream(), 92 new FileOutputStream(errfile) 93 )).start(); 94 } 95 return p.waitFor(); 96 } 97 catch ( FileNotFoundException e ) 98 { 99 return -2; 100 } 101 catch ( InterruptedException e ) 102 { 103 return -4; 104 } 105 } 106 spawn_in_out_err_wait( String command, String infile, String outfile, String errfile, boolean wait)107 public static int spawn_in_out_err_wait( 108 String command, 109 String infile, 110 String outfile, 111 String errfile, 112 boolean wait) 113 { 114 Process p; 115 116 try { 117 p = Runtime.getRuntime().exec(command); 118 } 119 catch ( IOException e) 120 { 121 return -1; 122 } 123 124 try { 125 if ( infile != null ) 126 { 127 ( new Xfer( new FileInputStream(infile), 128 p.getOutputStream() 129 )).start(); 130 } 131 if ( outfile != null ) 132 { 133 ( new Xfer( p.getInputStream(), 134 new FileOutputStream(outfile) 135 )).start(); 136 } 137 if ( errfile != null ) 138 { 139 ( new Xfer( p.getErrorStream(), 140 new FileOutputStream(errfile) 141 )).start(); 142 } 143 if ( wait ) 144 return p.waitFor(); 145 else 146 return 0; 147 } 148 catch ( FileNotFoundException e ) 149 { 150 return -2; 151 } 152 catch ( InterruptedException e ) 153 { 154 return -4; 155 } 156 } 157 spawn( String command)158 public static int spawn( String command) 159 { 160 161 return spawn_wait( command, true); 162 } 163 spawn_wait( String command, boolean wait)164 public static int spawn_wait( String command, boolean wait) 165 { 166 Process p; 167 168 try { 169 p = java.lang.Runtime.getRuntime().exec(command); 170 } 171 catch ( IOException e ) 172 { 173 return -77; 174 } 175 if ( wait ) 176 { 177 try { 178 return p.waitFor(); 179 } 180 catch ( InterruptedException e ) 181 { 182 return -78; 183 } 184 } 185 else 186 { 187 return 0; 188 } 189 } 190 new_scratch_file( String dir)191 public static String new_scratch_file( String dir) 192 { 193 return new_scratch_file( dir, "nsf"); 194 } 195 new_scratch_file( String dir, String suffix)196 public static String new_scratch_file( String dir, String suffix) 197 { 198 int n = (int)(r.nextFloat()*900000+100000); 199 File f = new File( dir, n+"."+suffix); 200 201 if ( f.exists() ) 202 { 203 return new_scratch_file( dir, suffix); 204 } 205 else 206 { 207 try { 208 (new FileOutputStream(f)).close(); 209 if ( f.exists() ) 210 { 211 return f.getCanonicalPath().toString(); 212 } 213 else 214 { 215 return null; 216 } 217 } 218 catch ( IOException e ) 219 { 220 return null; 221 } 222 } 223 } 224 new_scratch_dir( String dir)225 public static String new_scratch_dir( String dir) 226 { 227 int n = (int)(r.nextFloat()*900000+100000); 228 File d = new File( dir, n+".nsd"); 229 230 if ( d.exists() ) 231 { 232 return new_scratch_dir( dir); 233 } 234 else 235 { 236 try { 237 if ( d.mkdir() ) 238 { 239 return d.getCanonicalPath().toString(); 240 } 241 else 242 { 243 return null; 244 } 245 } 246 catch ( IOException e ) 247 { 248 return null; 249 } 250 } 251 } 252 new_scratch_suffix_file( String dir, String suffix)253 public static String new_scratch_suffix_file( String dir, String suffix) 254 { 255 int n = (int)(r.nextFloat()*900000+100000); 256 File f = new File( dir, n+"."+suffix); 257 258 if ( f.exists() ) 259 { 260 return new_scratch_suffix_file( dir, suffix); 261 } 262 else 263 { 264 try { 265 (new FileOutputStream(f)).close(); 266 if ( f.exists() ) 267 { 268 return f.getCanonicalPath().toString(); 269 } 270 else 271 { 272 return null; 273 } 274 } 275 catch ( IOException e ) 276 { 277 return null; 278 } 279 } 280 } 281 new_scratch_suffix_dir( String dir, String suffix)282 public static String new_scratch_suffix_dir( String dir, String suffix) 283 { 284 int n = (int)(r.nextFloat()*900000+100000); 285 File d = new File( dir, n+"."+suffix); 286 287 if ( d.exists() ) 288 { 289 return new_scratch_suffix_dir( dir, suffix); 290 } 291 else 292 { 293 try { 294 if ( d.mkdir() ) 295 { 296 return d.getCanonicalPath().toString(); 297 } 298 else 299 { 300 return null; 301 } 302 } 303 catch ( IOException e ) 304 { 305 return null; 306 } 307 } 308 } 309 create_file( String file)310 public static boolean create_file( String file) 311 { 312 File f = new File( file); 313 314 try { 315 (new FileOutputStream(f)).close(); 316 return f.exists(); 317 } 318 catch ( IOException e ) 319 { 320 return false; 321 } 322 } 323 create_dir( String dir)324 public static boolean create_dir( String dir) 325 { 326 try { 327 return (new File(dir)).mkdir(); 328 } 329 catch ( SecurityException e ) 330 { 331 return false; 332 } 333 } 334 file_exists( String f)335 public static boolean file_exists( String f) 336 { 337 return (new File(f)).exists(); 338 } 339 file_can_read( String f)340 public static boolean file_can_read( String f) 341 { 342 return (new File(f)).canRead(); 343 } 344 file_can_write( String f)345 public static boolean file_can_write( String f) 346 { 347 return (new File(f)).canWrite(); 348 } 349 file_is_file( String f)350 public static boolean file_is_file( String f) 351 { 352 return (new File(f)).isFile(); 353 } 354 file_is_dir( String f)355 public static boolean file_is_dir( String f) 356 { 357 return (new File(f)).isDirectory(); 358 } 359 file_last_modified( String f)360 public static long file_last_modified( String f) 361 { 362 return (new File(f)).lastModified(); 363 } 364 file_to_length( String f)365 public static long file_to_length( String f) 366 { 367 return (new File(f)).length(); 368 } 369 delete_file( String f)370 public static boolean delete_file( String f) 371 { 372 try { 373 return (new File(f)).delete(); 374 } 375 catch ( SecurityException e ) 376 { 377 return false; 378 } 379 } 380 classpath_parts()381 public static String[] classpath_parts() 382 { 383 String cp = java.lang.System.getProperty("java.class.path"); 384 StringTokenizer p = new StringTokenizer( cp, File.pathSeparator); 385 String a[] = new String[p.countTokens()]; 386 int i = 0; 387 String s; 388 String[] r; 389 390 try { 391 while ( p.hasMoreTokens() ) 392 { 393 s = (new File(p.nextToken())).getCanonicalPath().toString(); 394 if ( ! strings_contains_string( a, i, s) ) 395 { 396 a[i++] = s; 397 } 398 } 399 } 400 catch ( NoSuchElementException e ) 401 { 402 return null; 403 } 404 catch ( IOException e ) 405 { 406 return null; 407 } 408 finally { 409 r = new String[i]; 410 java.lang.System.arraycopy( a, 0, r, 0, i); 411 } 412 return r; 413 } 414 strings_contains_string( String[] ss, int n, String s)415 private static boolean strings_contains_string( String[] ss, int n, String s) 416 { 417 int i; 418 419 for ( i=0 ; i<n ; i++ ) 420 { 421 if ( ss[i].equals(s) ) 422 return true; 423 } 424 return false; 425 } 426 filename_to_byte_array( String f)427 public static byte[] filename_to_byte_array( String f) 428 { 429 try { 430 FileInputStream s = new FileInputStream( f); 431 int length = s.available(); 432 byte[] buf = new byte[length]; 433 s.read( buf); 434 s.close(); // to release file for e.g. deletion... 435 return buf; 436 } 437 catch ( FileNotFoundException e ) 438 { 439 return null; 440 } 441 catch ( IOException e ) 442 { 443 return null; 444 } 445 } 446 rename_file_to_file( String n1, String n2)447 public static boolean rename_file_to_file( String n1, String n2) 448 { 449 try { 450 File f1 = new File(n1); 451 File f2 = new File(n2); 452 return ( f1!=null && f2!=null 453 ? f1.renameTo(f2) 454 : false 455 ); 456 } 457 catch ( SecurityException e ) 458 { 459 return false; 460 } 461 } 462 463 } 464 465