Skip to content

Instantly share code, notes, and snippets.

@kentcdodds
Created June 18, 2013 17:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kentcdodds/5807683 to your computer and use it in GitHub Desktop.
Save kentcdodds/5807683 to your computer and use it in GitHub Desktop.
Short methods to help with Java stuff
/************************************************
* 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