Deprecated Command#getOutputAsString() and Command#getErrorAsString() - same thing can now be done without any decoding ambiguity via IOUtils.pipeStream(InputStream) -> ByteArrayOutputStream

This commit is contained in:
Shikhar Bhushan
2011-04-24 19:18:09 +01:00
parent ab705d7f2a
commit 4de741359e
6 changed files with 40 additions and 48 deletions

View File

@@ -16,6 +16,7 @@
package examples;
import net.schmizz.sshj.SSHClient;
import net.schmizz.sshj.common.IOUtils;
import net.schmizz.sshj.connection.channel.direct.Session;
import net.schmizz.sshj.connection.channel.direct.Session.Command;
@@ -36,13 +37,12 @@ public class Exec {
final Session session = ssh.startSession();
try {
final Command cmd = session.exec("ping -c 1 google.com");
System.out.print(cmd.getOutputAsString());
System.out.println(IOUtils.pipeStream(cmd.getInputStream()).toString());
cmd.join(5, TimeUnit.SECONDS);
System.out.println("\n** exit status: " + cmd.getExitStatus());
} finally {
session.close();
}
} finally {
ssh.disconnect();
}

View File

@@ -38,8 +38,10 @@ package net.schmizz.sshj.common;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
public class IOUtils {
@@ -55,4 +57,15 @@ public class IOUtils {
}
}
public static ByteArrayOutputStream pipeStream(InputStream stream)
throws IOException {
final ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int read;
while ((read = (stream.read(buf))) != -1)
bos.write(buf, 0, read);
bos.flush();
return bos;
}
}

View File

@@ -49,15 +49,6 @@ public class StreamCopier
};
}
public static String copyStreamToString(InputStream stream)
throws IOException {
final StringBuilder sb = new StringBuilder();
int read;
while ((read = stream.read()) != -1)
sb.append((char) read);
return sb.toString();
}
private static final ErrorCallback NULL_CALLBACK = new ErrorCallback() {
@Override
public void onError(IOException ioe) {

View File

@@ -43,16 +43,6 @@ public interface Session
interface Command
extends Channel {
/**
* Read from the command's {@code stderr} stream into a string (blocking).
*
* @return the commands {@code stderr} output as a string
*
* @throws IOException if error reading from the stream
*/
String getErrorAsString()
throws IOException;
/** Returns the command's {@code stderr} stream. */
InputStream getErrorStream();
@@ -81,16 +71,6 @@ public interface Session
*/
Boolean getExitWasCoreDumped();
/**
* Read from the command's {@code stdout} stream into a string (blocking).
*
* @return the command's {@code stdout} output as a string
*
* @throws IOException if error reading from the stream
*/
String getOutputAsString()
throws IOException;
/**
* Send a signal to the remote command.
*
@@ -101,6 +81,12 @@ public interface Session
void signal(Signal signal)
throws TransportException;
@Deprecated
String getOutputAsString() throws IOException;
@Deprecated
String getErrorAsString() throws IOException;
}
/** Shell API. */

View File

@@ -35,7 +35,11 @@
*/
package net.schmizz.sshj.connection.channel.direct;
import net.schmizz.sshj.common.*;
import net.schmizz.sshj.common.Buffer;
import net.schmizz.sshj.common.IOUtils;
import net.schmizz.sshj.common.SSHException;
import net.schmizz.sshj.common.SSHPacket;
import net.schmizz.sshj.common.SSHRuntimeException;
import net.schmizz.sshj.connection.Connection;
import net.schmizz.sshj.connection.ConnectionException;
import net.schmizz.sshj.connection.channel.ChannelInputStream;
@@ -47,9 +51,7 @@ import java.util.Collections;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
* {@link Session} implementation.
*/
/** {@link Session} implementation. */
public class SessionChannel
extends AbstractDirectChannel
implements Session, Session.Command, Session.Shell, Session.Subsystem {
@@ -122,12 +124,6 @@ public class SessionChannel
return this;
}
@Override
public String getErrorAsString()
throws IOException {
return StreamCopier.copyStreamToString(err);
}
@Override
public InputStream getErrorStream() {
return err;
@@ -148,12 +144,6 @@ public class SessionChannel
return exitStatus;
}
@Override
public String getOutputAsString()
throws IOException {
return StreamCopier.copyStreamToString(getInputStream());
}
@Override
public void handleRequest(String req, SSHPacket buf)
throws ConnectionException, TransportException {
@@ -254,4 +244,16 @@ public class SessionChannel
throw new SSHRuntimeException("This session channel is all used up");
}
@Override
@Deprecated
public String getOutputAsString() throws IOException {
return IOUtils.pipeStream(getInputStream()).toString();
}
@Override
@Deprecated
public String getErrorAsString() throws IOException {
return IOUtils.pipeStream(getErrorStream()).toString();
}
}

View File

@@ -77,7 +77,7 @@ class SCPEngine {
int code = scp.getInputStream().read();
switch (code) {
case -1:
String stderr = scp.getErrorAsString();
String stderr = IOUtils.pipeStream(scp.getErrorStream()).toString();
if (!stderr.isEmpty())
stderr = ". Additional info: `" + stderr + "`";
throw new SCPException("EOF while expecting response to protocol message" + stderr);