diff --git a/src/main/java/examples/Exec.java b/src/main/java/examples/Exec.java index 76a41374..38676bc1 100644 --- a/src/main/java/examples/Exec.java +++ b/src/main/java/examples/Exec.java @@ -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(); } diff --git a/src/main/java/net/schmizz/sshj/common/IOUtils.java b/src/main/java/net/schmizz/sshj/common/IOUtils.java index 54766a3c..7909c005 100644 --- a/src/main/java/net/schmizz/sshj/common/IOUtils.java +++ b/src/main/java/net/schmizz/sshj/common/IOUtils.java @@ -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; + } + } diff --git a/src/main/java/net/schmizz/sshj/common/StreamCopier.java b/src/main/java/net/schmizz/sshj/common/StreamCopier.java index c1eb799e..c21ca378 100644 --- a/src/main/java/net/schmizz/sshj/common/StreamCopier.java +++ b/src/main/java/net/schmizz/sshj/common/StreamCopier.java @@ -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) { diff --git a/src/main/java/net/schmizz/sshj/connection/channel/direct/Session.java b/src/main/java/net/schmizz/sshj/connection/channel/direct/Session.java index 9b0a5994..8646c529 100644 --- a/src/main/java/net/schmizz/sshj/connection/channel/direct/Session.java +++ b/src/main/java/net/schmizz/sshj/connection/channel/direct/Session.java @@ -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. */ diff --git a/src/main/java/net/schmizz/sshj/connection/channel/direct/SessionChannel.java b/src/main/java/net/schmizz/sshj/connection/channel/direct/SessionChannel.java index 3abf5a12..b25f775e 100644 --- a/src/main/java/net/schmizz/sshj/connection/channel/direct/SessionChannel.java +++ b/src/main/java/net/schmizz/sshj/connection/channel/direct/SessionChannel.java @@ -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(); + } + } diff --git a/src/main/java/net/schmizz/sshj/xfer/scp/SCPEngine.java b/src/main/java/net/schmizz/sshj/xfer/scp/SCPEngine.java index 0499fe4d..15652083 100644 --- a/src/main/java/net/schmizz/sshj/xfer/scp/SCPEngine.java +++ b/src/main/java/net/schmizz/sshj/xfer/scp/SCPEngine.java @@ -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);