mirror of
https://github.com/hierynomus/sshj.git
synced 2025-12-06 23:30:55 +03:00
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:
@@ -16,6 +16,7 @@
|
|||||||
package examples;
|
package examples;
|
||||||
|
|
||||||
import net.schmizz.sshj.SSHClient;
|
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;
|
||||||
import net.schmizz.sshj.connection.channel.direct.Session.Command;
|
import net.schmizz.sshj.connection.channel.direct.Session.Command;
|
||||||
|
|
||||||
@@ -36,13 +37,12 @@ public class Exec {
|
|||||||
final Session session = ssh.startSession();
|
final Session session = ssh.startSession();
|
||||||
try {
|
try {
|
||||||
final Command cmd = session.exec("ping -c 1 google.com");
|
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);
|
cmd.join(5, TimeUnit.SECONDS);
|
||||||
System.out.println("\n** exit status: " + cmd.getExitStatus());
|
System.out.println("\n** exit status: " + cmd.getExitStatus());
|
||||||
} finally {
|
} finally {
|
||||||
session.close();
|
session.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
} finally {
|
} finally {
|
||||||
ssh.disconnect();
|
ssh.disconnect();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -38,8 +38,10 @@ package net.schmizz.sshj.common;
|
|||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.Closeable;
|
import java.io.Closeable;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
|
||||||
public class IOUtils {
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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() {
|
private static final ErrorCallback NULL_CALLBACK = new ErrorCallback() {
|
||||||
@Override
|
@Override
|
||||||
public void onError(IOException ioe) {
|
public void onError(IOException ioe) {
|
||||||
|
|||||||
@@ -43,16 +43,6 @@ public interface Session
|
|||||||
interface Command
|
interface Command
|
||||||
extends Channel {
|
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. */
|
/** Returns the command's {@code stderr} stream. */
|
||||||
InputStream getErrorStream();
|
InputStream getErrorStream();
|
||||||
|
|
||||||
@@ -81,16 +71,6 @@ public interface Session
|
|||||||
*/
|
*/
|
||||||
Boolean getExitWasCoreDumped();
|
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.
|
* Send a signal to the remote command.
|
||||||
*
|
*
|
||||||
@@ -101,6 +81,12 @@ public interface Session
|
|||||||
void signal(Signal signal)
|
void signal(Signal signal)
|
||||||
throws TransportException;
|
throws TransportException;
|
||||||
|
|
||||||
|
@Deprecated
|
||||||
|
String getOutputAsString() throws IOException;
|
||||||
|
|
||||||
|
@Deprecated
|
||||||
|
String getErrorAsString() throws IOException;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Shell API. */
|
/** Shell API. */
|
||||||
|
|||||||
@@ -35,7 +35,11 @@
|
|||||||
*/
|
*/
|
||||||
package net.schmizz.sshj.connection.channel.direct;
|
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.Connection;
|
||||||
import net.schmizz.sshj.connection.ConnectionException;
|
import net.schmizz.sshj.connection.ConnectionException;
|
||||||
import net.schmizz.sshj.connection.channel.ChannelInputStream;
|
import net.schmizz.sshj.connection.channel.ChannelInputStream;
|
||||||
@@ -47,9 +51,7 @@ import java.util.Collections;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
/**
|
/** {@link Session} implementation. */
|
||||||
* {@link Session} implementation.
|
|
||||||
*/
|
|
||||||
public class SessionChannel
|
public class SessionChannel
|
||||||
extends AbstractDirectChannel
|
extends AbstractDirectChannel
|
||||||
implements Session, Session.Command, Session.Shell, Session.Subsystem {
|
implements Session, Session.Command, Session.Shell, Session.Subsystem {
|
||||||
@@ -122,12 +124,6 @@ public class SessionChannel
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getErrorAsString()
|
|
||||||
throws IOException {
|
|
||||||
return StreamCopier.copyStreamToString(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public InputStream getErrorStream() {
|
public InputStream getErrorStream() {
|
||||||
return err;
|
return err;
|
||||||
@@ -148,12 +144,6 @@ public class SessionChannel
|
|||||||
return exitStatus;
|
return exitStatus;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getOutputAsString()
|
|
||||||
throws IOException {
|
|
||||||
return StreamCopier.copyStreamToString(getInputStream());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handleRequest(String req, SSHPacket buf)
|
public void handleRequest(String req, SSHPacket buf)
|
||||||
throws ConnectionException, TransportException {
|
throws ConnectionException, TransportException {
|
||||||
@@ -254,4 +244,16 @@ public class SessionChannel
|
|||||||
throw new SSHRuntimeException("This session channel is all used up");
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -77,7 +77,7 @@ class SCPEngine {
|
|||||||
int code = scp.getInputStream().read();
|
int code = scp.getInputStream().read();
|
||||||
switch (code) {
|
switch (code) {
|
||||||
case -1:
|
case -1:
|
||||||
String stderr = scp.getErrorAsString();
|
String stderr = IOUtils.pipeStream(scp.getErrorStream()).toString();
|
||||||
if (!stderr.isEmpty())
|
if (!stderr.isEmpty())
|
||||||
stderr = ". Additional info: `" + stderr + "`";
|
stderr = ". Additional info: `" + stderr + "`";
|
||||||
throw new SCPException("EOF while expecting response to protocol message" + stderr);
|
throw new SCPException("EOF while expecting response to protocol message" + stderr);
|
||||||
|
|||||||
Reference in New Issue
Block a user