00001 package edu.ksu.cis.bandera.util;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 import java.util.*;
00036 import java.io.*;
00037
00038 public class BanderaUtil {
00039 private static final int BUFSIZE = 50000;
00040 private static byte[] buffer = new byte[BUFSIZE];
00041 private static Runtime runtime = Runtime.getRuntime();
00042 public static boolean copyFile(File src, File dest)
00043 {
00044 if (!src.exists()) return false;
00045 if (src.isFile() && dest.isDirectory())
00046 {
00047 dest = new File (dest.getAbsolutePath()+File.separator+src.getName());
00048 }
00049 if (dest.exists()) dest.delete();
00050 FileInputStream in = null;
00051 FileOutputStream out = null;
00052 try {
00053 in = new FileInputStream(src);
00054 out = new FileOutputStream(dest);
00055 byte[] buf = new byte[64*1024];
00056 int i = 0;
00057 while ((i = in.read(buf)) != -1)
00058 out.write(buf, 0, i);
00059 in.close(); out.close();
00060 } catch (Exception e)
00061 {
00062 try { if (in != null) in.close(); } catch (Exception ex) {}
00063 try { if (out != null) out.close(); } catch (Exception ex) {}
00064 return false;
00065 }
00066 return true;
00067 }
00068
00069
00070
00071
00072
00073 public static boolean deleteDir(File dir) {
00074 if (!dir.exists()) return true;
00075
00076 File[] files = dir.listFiles();
00077 if (files == null) return true;
00078 for (int i = 0; i < files.length; i++) {
00079 if (files[i].isDirectory()) {
00080 deleteDir(files[i]);
00081 } else {
00082 if (!files[i].delete()) {
00083 return false;
00084 }
00085 }
00086 }
00087
00088 return dir.delete();
00089 }
00090 public static boolean deleteDir(String s) { return deleteDir(new File(s)); }
00091 public static String exec(String command, StringBuffer output, StringBuffer error, int verbosity)
00092 {
00093 try {
00094 Process p = runtime.exec(command);
00095 InputStream commandErr = p.getErrorStream();
00096 InputStream commandOut = p.getInputStream();
00097 String ln = System.getProperty("line.separator");
00098
00099 StringBuffer sb = new StringBuffer();
00100 if (verbosity > 5)
00101 {
00102 System.out.println(command);
00103 output.append(command+ln);
00104 }
00105 int charsRead = 0;
00106 int charsAvail = 0;
00107 while (charsRead != -1) {
00108 if (commandErr.available() > 0) {
00109 charsRead = commandErr.read(buffer);
00110 String s = new String(buffer, 0, charsRead);
00111 error.append(s);
00112 sb.append(s);
00113 }
00114 charsRead = commandOut.read(buffer);
00115 if (charsRead > 0)
00116 {
00117 String s = new String(buffer, 0, charsRead);
00118 output.append(s);
00119 sb.append(s);
00120 }
00121 }
00122 p.waitFor();
00123 String result = sb.toString();
00124 if (verbosity > 5) System.out.println(result);
00125 return result;
00126 } catch (Exception e) {
00127 throw new RuntimeException("exec of command '" + command + "' was aborted: \n" + e);
00128 }
00129 }
00130 public static String exec(String command, boolean verbose)
00131 {
00132 StringBuffer sb = new StringBuffer();
00133 return exec(command, sb, sb, 9);
00134 }
00135 public static void main(String[] args)
00136 {
00137
00138 moveFile (new File("Driver.java"), new File("output"));
00139 }
00140 public static boolean mkdirs(String s)
00141 {
00142 File tempDir = new File(s);
00143 String temp = "Error: Can't create temporary directory '" + tempDir.getAbsolutePath() + "'";
00144
00145 if (tempDir.exists() && !deleteDir(tempDir)) return false;
00146 try {
00147 if (!tempDir.mkdirs()) return false;
00148 if (!tempDir.exists()) System.out.println("Strangely enough, it doesn't exists!");
00149 } catch (Exception e) {
00150 return false;
00151 }
00152 return true;
00153 }
00154 public static void moveFile(File src, File dest)
00155 {
00156 copyFile(src,dest); src.delete();
00157 }
00158 public static void moveFile(String file, String dir) {
00159 File f = new File(file);
00160 if (!f.exists() || dir == null || ("."+File.separator).equals(dir) || ".".equals(dir)) return;
00161 moveFile(f, new File(dir));
00162 }
00163
00164
00165
00166
00167
00168 public static void runJavac(String classpath, String filename) {
00169 String cmdLine = "javac -g -classpath " + classpath + " " + filename;
00170 System.out.println(cmdLine);
00171 sun.tools.javac.Main javac = new sun.tools.javac.Main(System.out, "Bandera");
00172 if (!javac.compile(new String[] { "-g", "-classpath", classpath, filename })) {
00173 System.out.println("Error while compiling the model");
00174 }
00175 }
00176 }