/** * Starts a native process on the server * * @param command the command to start the process * @param dir the dir in which the process starts */ static String startProcess(String command, String dir) throws IOException { StringBuffer ret = new StringBuffer(); String[] comm = new String[3]; comm[0] = COMMAND_INTERPRETER[0]; comm[1] = COMMAND_INTERPRETER[1]; comm[2] = command; long start = System.currentTimeMillis(); try { // Start process Process ls_proc = Runtime.getRuntime().exec(comm, null, new File(dir)); // Get input and error streams BufferedInputStream ls_in = new BufferedInputStream(ls_proc.getInputStream()); BufferedInputStream ls_err = new BufferedInputStream(ls_proc.getErrorStream()); boolean end = false; while (!end) { int c = 0; while ((ls_err.available() > 0) && (++c <= 1000)) { ret.append(conv2Html(ls_err.read())); } c = 0; while ((ls_in.available() > 0) && (++c <= 1000)) { ret.append(conv2Html(ls_in.read())); } try { ls_proc.exitValue(); // if the process has not finished, an exception is thrown // else while (ls_err.available() > 0) ret.append(conv2Html(ls_err.read())); while (ls_in.available() > 0) ret.append(conv2Html(ls_in.read())); end = true; } catch (IllegalThreadStateException ex) { // Process is running } // The process is not allowed to run longer than given time. if (System.currentTimeMillis() - start > MAX_PROCESS_RUNNING_TIME) { ls_proc.destroy(); end = true; ret.append("!!!! Process has timed out, destroyed !!!!!"); } try { Thread.sleep(50); } catch (InterruptedException ie) { } } } catch (IOException e) { ret.append("Error: " + e); } return ret.toString(); }
public BytecodeBuffer(String filename) throws IOException { BufferedInputStream in = null; try { in = new BufferedInputStream(new FileInputStream(filename)); bytecodes = new byte[in.available()]; in.read(bytecodes); size = bytecodes.length; pos = 0; } finally { if (in != null) { try { in.close(); } catch (IOException ex) { } } } }
/** * Get binary data of a source file. * * @param path The canonical path of source file. * @return Source file data. */ public byte[] read(String path) { if (!binaryCache.containsKey(path)) { try { BufferedInputStream bf = new BufferedInputStream(new FileInputStream(new File(path))); try { byte[] data = new byte[bf.available()]; bf.read(data); detectBOM(data, path); binaryCache.put(path, data); } finally { bf.close(); } } catch (IOException e) { App.exit(e); } } return binaryCache.get(path); }