I created an object called Obj which sends messages over a Secure Shell channel. I can send characters in UTF-8, which means that the output stream must handle multi-byte characters. That is why I decided to use a buffered output stream writer. That is not relevant, so the code only references that part of the code with a comment.
I'd like the input stream to also handle multibyte characters. I know that the read() function of the InputStreamReader will return integers that correspond to UTF-16 code points or -1. My current implementation loops and tests for -1. It loops endlessly. Why?
Here is my source code:
public class Obj {
public String sendMessage(char[] message) {
final int CONNECTION_TIMEOUT_MILLISECONDS = 300;
final int IN_BUFFER_SIZE = 2048;
int codePoint;
StringBuilder sbResponse = new StringBuilder();
try {
ChannelShell channel = SshChannelFactory.getChannel(this);
if (channel == null) {
System.out.println("Unable to acquire channel for object \"{}\"", this);
throw new RuntimeException();
}
channel.connect(CONNECTION_TIMEOUT_MILLISECONDS);
System.out.println("Successfully opened channel to \"{}\"", channel.getHost());
}
/**
* Write some stuff in this try block.
*/
//try {
//} catch
/**
* Read the response in this try block.
*/
char[] buffer = new char[IN_BUFFER_SIZE];
int bytesReadOffset = 0;
try {
BufferedReader fromServerStream = new BufferedReader(new InputStreamReader(channel.getInputStream(), Charset.forName("UTF-8")), IN_BUFFER_SIZE);
while ((codePoint = fromServerStream.read()) != -1) {
sbResponse.append((char) codePoint);
} catch (IOException e) {
e.printStackTrace();
}
return sbResponse.toString();
}
public class SshChannelFactory {
public static ChannelShell getChannel(Obj obj) {
return createSshChannel(obj);
}
private static Session createSshSession(Obj obj) {
final JSch jsch = new JSch();
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
try {
Session session = jsch.getSession(obj.getUser(), obj.getHost(), obj.getPort());
session.connect();
return session;
} catch (JschException e) {
e.printStackTrace();
throw new RuntimeException();
}
}
private static ChannelShell createSshChannel() {
Session session = SSHSessionFactory.createSshSession(Obj obj)
try {
ChannelShell channel = (ChannelShell) session.openChannel("shell");
channel.setPty(true);
channel.setInputStream(null);
channel.setOutputStream(null);
return channel;
} catch (JSchException e) {
e.printStackTrace();
throw new RuntimeException();
}
}
}