00001 package jpl.util;
00002
00003 import java.io.File;
00004 import java.io.FileInputStream;
00005 import java.io.FileNotFoundException;
00006 import java.io.FileOutputStream;
00007 import java.io.IOException;
00008 import java.util.NoSuchElementException;
00009 import java.util.Random;
00010 import java.util.StringTokenizer;
00011
00012 public class Util
00013 {
00014 private static Random r = new Random();
00015
00016 public static String[] dir_to_members( String dir)
00017 {
00018 String[] ns = (new File(dir)).list();
00019 int len = ns.length;
00020 String[] ps = new String[len];
00021 for ( int i=0 ; i<len ; i++ )
00022 {
00023 try {
00024 ps[i] = (new File(dir,ns[i])).getCanonicalPath().toString();
00025 }
00026 catch ( IOException e )
00027 {
00028 ps[i] = "";
00029 }
00030 }
00031 return ps;
00032 }
00033
00034 public static String[] dir_to_member_paths( String dir)
00035 {
00036 String[] ns = (new File(dir)).list();
00037 int len = ns.length;
00038 String[] ps = new String[len];
00039 for ( int i=0 ; i<len ; i++ )
00040 {
00041 try {
00042 ps[i] = (new File(dir,ns[i])).getCanonicalPath();
00043 }
00044 catch ( IOException e )
00045 {
00046 ps[i] = "";
00047 }
00048 }
00049 return ps;
00050 }
00051
00052 public static String[] dir_to_member_names( String dir)
00053 {
00054 String[] ns = (new File(dir)).list();
00055 int len = ns.length;
00056 String[] ps = new String[len];
00057 for ( int i=0 ; i<len ; i++ )
00058 {
00059 ps[i] = (new File(dir,ns[i])).getName();
00060 }
00061 return ps;
00062 }
00063
00064 public static int spawn_in_out_err( String command, String infile, String outfile, String errfile)
00065 {
00066 Process p;
00067
00068 try {
00069 p = Runtime.getRuntime().exec(command);
00070 }
00071 catch ( IOException e)
00072 {
00073 return -1;
00074 }
00075
00076 try {
00077 if ( infile != null )
00078 {
00079 ( new Xfer( new FileInputStream(infile),
00080 p.getOutputStream()
00081 )).start();
00082 }
00083 if ( outfile != null )
00084 {
00085 ( new Xfer( p.getInputStream(),
00086 new FileOutputStream(outfile)
00087 )).start();
00088 }
00089 if ( errfile != null )
00090 {
00091 ( new Xfer( p.getErrorStream(),
00092 new FileOutputStream(errfile)
00093 )).start();
00094 }
00095 return p.waitFor();
00096 }
00097 catch ( FileNotFoundException e )
00098 {
00099 return -2;
00100 }
00101 catch ( InterruptedException e )
00102 {
00103 return -4;
00104 }
00105 }
00106
00107 public static int spawn_in_out_err_wait(
00108 String command,
00109 String infile,
00110 String outfile,
00111 String errfile,
00112 boolean wait)
00113 {
00114 Process p;
00115
00116 try {
00117 p = Runtime.getRuntime().exec(command);
00118 }
00119 catch ( IOException e)
00120 {
00121 return -1;
00122 }
00123
00124 try {
00125 if ( infile != null )
00126 {
00127 ( new Xfer( new FileInputStream(infile),
00128 p.getOutputStream()
00129 )).start();
00130 }
00131 if ( outfile != null )
00132 {
00133 ( new Xfer( p.getInputStream(),
00134 new FileOutputStream(outfile)
00135 )).start();
00136 }
00137 if ( errfile != null )
00138 {
00139 ( new Xfer( p.getErrorStream(),
00140 new FileOutputStream(errfile)
00141 )).start();
00142 }
00143 if ( wait )
00144 return p.waitFor();
00145 else
00146 return 0;
00147 }
00148 catch ( FileNotFoundException e )
00149 {
00150 return -2;
00151 }
00152 catch ( InterruptedException e )
00153 {
00154 return -4;
00155 }
00156 }
00157
00158 public static int spawn( String command)
00159 {
00160
00161 return spawn_wait( command, true);
00162 }
00163
00164 public static int spawn_wait( String command, boolean wait)
00165 {
00166 Process p;
00167
00168 try {
00169 p = java.lang.Runtime.getRuntime().exec(command);
00170 }
00171 catch ( IOException e )
00172 {
00173 return -77;
00174 }
00175 if ( wait )
00176 {
00177 try {
00178 return p.waitFor();
00179 }
00180 catch ( InterruptedException e )
00181 {
00182 return -78;
00183 }
00184 }
00185 else
00186 {
00187 return 0;
00188 }
00189 }
00190
00191 public static String new_scratch_file( String dir)
00192 {
00193 return new_scratch_file( dir, "nsf");
00194 }
00195
00196 public static String new_scratch_file( String dir, String suffix)
00197 {
00198 int n = (int)(r.nextFloat()*900000+100000);
00199 File f = new File( dir, n+"."+suffix);
00200
00201 if ( f.exists() )
00202 {
00203 return new_scratch_file( dir, suffix);
00204 }
00205 else
00206 {
00207 try {
00208 (new FileOutputStream(f)).close();
00209 if ( f.exists() )
00210 {
00211 return f.getCanonicalPath().toString();
00212 }
00213 else
00214 {
00215 return null;
00216 }
00217 }
00218 catch ( IOException e )
00219 {
00220 return null;
00221 }
00222 }
00223 }
00224
00225 public static String new_scratch_dir( String dir)
00226 {
00227 int n = (int)(r.nextFloat()*900000+100000);
00228 File d = new File( dir, n+".nsd");
00229
00230 if ( d.exists() )
00231 {
00232 return new_scratch_dir( dir);
00233 }
00234 else
00235 {
00236 try {
00237 if ( d.mkdir() )
00238 {
00239 return d.getCanonicalPath().toString();
00240 }
00241 else
00242 {
00243 return null;
00244 }
00245 }
00246 catch ( IOException e )
00247 {
00248 return null;
00249 }
00250 }
00251 }
00252
00253 public static String new_scratch_suffix_file( String dir, String suffix)
00254 {
00255 int n = (int)(r.nextFloat()*900000+100000);
00256 File f = new File( dir, n+"."+suffix);
00257
00258 if ( f.exists() )
00259 {
00260 return new_scratch_suffix_file( dir, suffix);
00261 }
00262 else
00263 {
00264 try {
00265 (new FileOutputStream(f)).close();
00266 if ( f.exists() )
00267 {
00268 return f.getCanonicalPath().toString();
00269 }
00270 else
00271 {
00272 return null;
00273 }
00274 }
00275 catch ( IOException e )
00276 {
00277 return null;
00278 }
00279 }
00280 }
00281
00282 public static String new_scratch_suffix_dir( String dir, String suffix)
00283 {
00284 int n = (int)(r.nextFloat()*900000+100000);
00285 File d = new File( dir, n+"."+suffix);
00286
00287 if ( d.exists() )
00288 {
00289 return new_scratch_suffix_dir( dir, suffix);
00290 }
00291 else
00292 {
00293 try {
00294 if ( d.mkdir() )
00295 {
00296 return d.getCanonicalPath().toString();
00297 }
00298 else
00299 {
00300 return null;
00301 }
00302 }
00303 catch ( IOException e )
00304 {
00305 return null;
00306 }
00307 }
00308 }
00309
00310 public static boolean create_file( String file)
00311 {
00312 File f = new File( file);
00313
00314 try {
00315 (new FileOutputStream(f)).close();
00316 return f.exists();
00317 }
00318 catch ( IOException e )
00319 {
00320 return false;
00321 }
00322 }
00323
00324 public static boolean create_dir( String dir)
00325 {
00326 try {
00327 return (new File(dir)).mkdir();
00328 }
00329 catch ( SecurityException e )
00330 {
00331 return false;
00332 }
00333 }
00334
00335 public static boolean file_exists( String f)
00336 {
00337 return (new File(f)).exists();
00338 }
00339
00340 public static boolean file_can_read( String f)
00341 {
00342 return (new File(f)).canRead();
00343 }
00344
00345 public static boolean file_can_write( String f)
00346 {
00347 return (new File(f)).canWrite();
00348 }
00349
00350 public static boolean file_is_file( String f)
00351 {
00352 return (new File(f)).isFile();
00353 }
00354
00355 public static boolean file_is_dir( String f)
00356 {
00357 return (new File(f)).isDirectory();
00358 }
00359
00360 public static long file_last_modified( String f)
00361 {
00362 return (new File(f)).lastModified();
00363 }
00364
00365 public static long file_to_length( String f)
00366 {
00367 return (new File(f)).length();
00368 }
00369
00370 public static boolean delete_file( String f)
00371 {
00372 try {
00373 return (new File(f)).delete();
00374 }
00375 catch ( SecurityException e )
00376 {
00377 return false;
00378 }
00379 }
00380
00381 public static String[] classpath_parts()
00382 {
00383 String cp = java.lang.System.getProperty("java.class.path");
00384 StringTokenizer p = new StringTokenizer( cp, File.pathSeparator);
00385 String a[] = new String[p.countTokens()];
00386 int i = 0;
00387 String s;
00388 String[] r;
00389
00390 try {
00391 while ( p.hasMoreTokens() )
00392 {
00393 s = (new File(p.nextToken())).getCanonicalPath().toString();
00394 if ( ! strings_contains_string( a, i, s) )
00395 {
00396 a[i++] = s;
00397 }
00398 }
00399 }
00400 catch ( NoSuchElementException e )
00401 {
00402 return null;
00403 }
00404 catch ( IOException e )
00405 {
00406 return null;
00407 }
00408 finally {
00409 r = new String[i];
00410 java.lang.System.arraycopy( a, 0, r, 0, i);
00411 }
00412 return r;
00413 }
00414
00415 private static boolean strings_contains_string( String[] ss, int n, String s)
00416 {
00417 int i;
00418
00419 for ( i=0 ; i<n ; i++ )
00420 {
00421 if ( ss[i].equals(s) )
00422 return true;
00423 }
00424 return false;
00425 }
00426
00427 public static byte[] filename_to_byte_array( String f)
00428 {
00429 try {
00430 FileInputStream s = new FileInputStream( f);
00431 int length = s.available();
00432 byte[] buf = new byte[length];
00433 s.read( buf);
00434 s.close();
00435 return buf;
00436 }
00437 catch ( FileNotFoundException e )
00438 {
00439 return null;
00440 }
00441 catch ( IOException e )
00442 {
00443 return null;
00444 }
00445 }
00446
00447 public static boolean rename_file_to_file( String n1, String n2)
00448 {
00449 try {
00450 File f1 = new File(n1);
00451 File f2 = new File(n2);
00452 return ( f1!=null && f2!=null
00453 ? f1.renameTo(f2)
00454 : false
00455 );
00456 }
00457 catch ( SecurityException e )
00458 {
00459 return false;
00460 }
00461 }
00462
00463 }
00464