3SP Knowledgebase
Information and FAQs about 3SP products
  
Search  
   
Browse by Category
3SP Knowledgebase .: Trash Can .: A channel's InputStream.available() method always returns zero

A channel's InputStream.available() method always returns zero

Some users have reported that the InputStream's available method implementation within Maverick does not behave as expected. When executing a command, or starting a shell the users state that the available method always returns zero, even if they perform a read and it immediatleyreturns data without blocking.

The Maverick API by default operates in asingle threaded mode, which means there are no background threads to load data into local buffers. When a thread calls the read method it actually blocks on the underlying transport mechanism (usually a Socket) and processes data until it finds some data of its own. Because channels are multiplexed into a single connection, data for multiple channels could be arriving at the same time so using the transports available method is not appropriate. The only way to determine if data is available is to actually block so our available method simply returns the number of bytes available in the local buffer.

With the release of J2SSH Maverick 1.2.0, anoptional buffered mode was introduced for theSshClient which provides abackground thread to load data into the channels independently. Using this mode ensures that a channel's InputStream reports the correct number of bytes available to be read without blocking.

SshConnector con = SshConnector.getInstance();
// Create a buffered connection
SshClient ssh = con.connect(new SocketTransport("cvs", 22),
"lee",
true); // Indicates we're using buffered mode
PasswordAuthentication pwd = new PasswordAuthentication();
pwd.setPassword("xxxxx");

ssh.authenticate(pwd);


SshSession session = ssh.openSessionChannel();
session.executeCommand("find /");

InputStream in = session.getInputStream();
while(!session.isClosed()) {
if(in.available() > 0) {
}
   Thread.sleep(100);





}
ssh.disconnect();





How helpful was this article to you?

User Comments

Add Comment
No comments have been posted.


powered by Lore
© 2008 3SP Ltd. All Rights Reserved