1 21 22 25 package net.sf.launch4j; 26 27 import java.io.BufferedReader ; 28 import java.io.File ; 29 import java.io.IOException ; 30 import java.io.InputStream ; 31 import java.io.InputStreamReader ; 32 import java.io.OutputStream ; 33 import java.io.Reader ; 34 import java.io.Writer ; 35 import java.util.regex.Matcher ; 36 import java.util.regex.Pattern ; 37 38 41 public class Util { 42 public static final boolean WINDOWS_OS = System.getProperty("os.name") 43 .toLowerCase().startsWith("windows"); 44 45 private Util() {} 46 47 public static File createTempFile(String suffix) throws IOException { 48 String tmpdir = System.getProperty("launch4j.tmpdir"); 49 if (tmpdir != null) { 50 if (tmpdir.indexOf(' ') != -1) { 51 throw new IOException (Messages.getString("Util.tmpdir")); 52 } 53 return File.createTempFile("launch4j", suffix, new File (tmpdir)); 54 } else { 55 return File.createTempFile("launch4j", suffix); 56 } 57 } 58 59 65 public static File getJarBasedir() { 66 String url = Util.class.getClassLoader() 67 .getResource(Util.class.getName().replace('.', '/') + ".class") 68 .getFile() 69 .replaceAll("%20", " "); 70 if (url.startsWith("file:")) { 71 String jar = url.substring(5, url.lastIndexOf('!')); 72 int x = jar.lastIndexOf('/'); 73 if (x == -1) { 74 x = jar.lastIndexOf('\\'); 75 } 76 String basedir = jar.substring(0, x + 1); 77 return new File (basedir); 78 } else { 79 return new File ("."); 80 } 81 } 82 83 public static File getAbsoluteFile(File basepath, File f) { 84 return f.isAbsolute() ? f : new File (basepath, f.getPath()); 85 } 86 87 public static String getExtension(File f) { 88 String name = f.getName(); 89 int x = name.lastIndexOf('.'); 90 if (x != -1) { 91 return name.substring(x); 92 } else { 93 return ""; 94 } 95 } 96 97 public static void exec(String cmd, Log log) throws ExecException { 98 BufferedReader is = null; 99 try { 100 if (WINDOWS_OS) { 101 cmd = cmd.replaceAll("/", "\\\\"); 102 } 103 Process p = Runtime.getRuntime().exec(cmd); 104 is = new BufferedReader (new InputStreamReader (p.getErrorStream())); 105 String line; 106 int errLine = -1; 107 Pattern pattern = Pattern.compile(":\\d+:"); 108 while ((line = is.readLine()) != null) { 109 log.append(line); 110 Matcher matcher = pattern.matcher(line); 111 if (matcher.find()) { 112 errLine = Integer.valueOf( 113 line.substring(matcher.start() + 1, matcher.end() - 1)) 114 .intValue(); 115 if (line.matches("(?i).*unrecognized escape sequence")) { 116 log.append(Messages.getString("Util.use.double.backslash")); 117 } 118 break; 119 } 120 } 121 is.close(); 122 p.waitFor(); 123 if (errLine != -1) { 124 throw new ExecException(Messages.getString("Util.exec.failed") 125 + ": " + cmd, errLine); 126 } 127 if (p.exitValue() != 0) { 128 throw new ExecException(Messages.getString("Util.exec.failed") 129 + "(" + p.exitValue() + "): " + cmd); 130 } 131 } catch (IOException e) { 132 close(is); 133 throw new ExecException(e); 134 } catch (InterruptedException e) { 135 close(is); 136 throw new ExecException(e); 137 } 138 } 139 140 public static void close(final InputStream o) { 141 if (o != null) { 142 try { 143 o.close(); 144 } catch (IOException e) { 145 System.err.println(e); } 147 } 148 } 149 150 public static void close(final OutputStream o) { 151 if (o != null) { 152 try { 153 o.close(); 154 } catch (IOException e) { 155 System.err.println(e); } 157 } 158 } 159 160 public static void close(final Reader o) { 161 if (o != null) { 162 try { 163 o.close(); 164 } catch (IOException e) { 165 System.err.println(e); } 167 } 168 } 169 170 public static void close(final Writer o) { 171 if (o != null) { 172 try { 173 o.close(); 174 } catch (IOException e) { 175 System.err.println(e); } 177 } 178 } 179 180 public static boolean delete(File f) { 181 return (f != null) ? f.delete() : false; 182 } 183 } 184 | Popular Tags |