private void writeFileToOutputStream(File f) throws IOException { FileInputStream fis = new FileInputStream(f); byte[] buffer = new byte[2048]; for (; ; ) { int nBytes = fis.read(buffer); if (nBytes <= 0) break; socket.write(buffer, 0, nBytes); } socket.flush(); fis.close(); }
private String collectResponse() throws IOException { String result = ""; String line = socket.readline(); while (line != null) { result += line; line = socket.readline(); if (line != null) { result += "\n"; } } return result; }
public int waitFor() throws InterruptedException, IOException, NovacomException { join(); socket.flush(); if (stdinRedirect == null && stdoutRedirect == null) { socket.closeInputOutput(); } socket.close(); if (ioException != null) { throw ioException; } if (ncException != null) { throw ncException; } return exitCode; }
private void writeInputStreamToFile(File f) throws IOException { FileOutputStream fos = new FileOutputStream(f); byte[] buffer = new byte[2048]; for (; ; ) { int nBytes = socket.read(buffer); if (nBytes <= 0) break; fos.write(buffer, 0, nBytes); } fos.flush(); fos.close(); }
@Override public void run() { try { socket.write(command.toString() + "\n"); String reply = socket.readline(); socket.setPacketMode(true); if (reply == null) { ncException = new NovacomException("No data to read from socket"); } else if (reply.startsWith("ok")) { if (stdinRedirect != null) { writeFileToOutputStream(stdinRedirect); socket.closeInputOutput(); try { exitCode = socket.getExitCode(); } catch (Exception e1) { } } else if (stdoutRedirect != null) { writeInputStreamToFile(stdoutRedirect); socket.closeInputOutput(); try { exitCode = socket.getExitCode(); } catch (Exception e2) { } } else { response = collectResponse(); try { exitCode = socket.getExitCode(); } catch (NovacomException ne) { ncException = ne; } } } else { ncException = new NovacomException(reply); } } catch (IOException e) { System.err.println(e.getMessage()); ioException = e; exitCode = -1; } }