Created
June 18, 2013 17:52
-
-
Save kentcdodds/5807683 to your computer and use it in GitHub Desktop.
Short methods to help with Java stuff
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/************************************************ | |
* Execute commands | |
************************************************ | |
*/ | |
private String executeCommand(ChannelExec channel, String... commands) | |
throws IOException, JSchException { | |
InputStream in = channel.getInputStream(); | |
StringBuilder output = new StringBuilder(); | |
for (int i = 0; i < commands.length; i++) { | |
channel.setCommand(commands[i]); | |
channel.connect(); | |
while (true) { | |
readInCommandOutput(in, output); | |
if (channel.isClosed()) { | |
System.out.println("exit-status: " | |
+ channel.getExitStatus()); | |
break; | |
} | |
sleep(2000); | |
} | |
channel.disconnect(); | |
} | |
return output.toString(); | |
} | |
private void readInCommandOutput(InputStream in, StringBuilder output) | |
throws IOException { | |
int bufferSize = 1024; | |
byte[] tmp = new byte[bufferSize]; | |
while (in.available() > 0) { | |
int i = in.read(tmp, 0, 1024); | |
if (i < 0) { | |
break; | |
} | |
String line = new String(tmp, 0, i); | |
// System.out.println(line); | |
output.append(line); | |
} | |
} | |
private void sleep(int time) { | |
try { | |
Thread.sleep(time); | |
} catch (Exception ee) { | |
} | |
} | |
/***************************************************************** | |
* | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment