以下は、プロジェクトで使用するコード スニペットの一部です。
public String fetchFromStream()
{
try
{
int charVal;
StringBuffer sb = new StringBuffer();
while((charVal = inputStream.read()) > 0) {
sb.append((char)charVal);
}
return sb.toString();
} catch (Exception e)
{
m_log.error("readUntil(..) : " + e.getMessage());
return null;
} finally {
System.out.println("<<<<<<<<<<<<<<<<<<<<<< Called >>>>>>>>>>>>>>>>>>>>>>>>>>>");
}
}
最初、while ループはかなりうまく機能し始めます。しかし、おそらく最後の文字がストリームから読み取られた後、-1 の戻り値を取得することを期待していました。しかし、これが私の問題の始まりです。finally ブロックが実行されなくても、コードはハングします。
実行時に実際に何が起こっているかを確認するために、Eclipse でこのコードをデバッグしていました。while ループ内にポインター (デバッグ) を設定し、Char 値が 1 つずつ入力される StringBuffer を常に監視していました。しかし、while ループ内の状態を確認しているときに、突然、デバッグ コントロールが失われ、コードがハングアップ状態になるポイントが発生します。例外もスローされません!!
ここで何が起きてるの ?
編集::
これが、InputStream を取得する方法です。基本的に、Telnet には Apache Commons Net を使用しています。
private TelnetClient getTelnetSession(String hostname, int port)
{
TelnetClient tc = new TelnetClient();
try
{
tc.connect(hostname, port != 0 ? port : 23);
//These are instance variables
inputStream = tc.getInputStream();
outputStream = new PrintStream(tc.getOutputStream());
//More codes...
return tc;
} catch (SocketException se)
{
m_log.error("getTelnetSession(..) : " + se.getMessage());
return null;
} catch (IOException ioe)
{
m_log.error("getTelnetSession(..) : " + ioe.getMessage());
return null;
} catch (Exception e)
{
m_log.error("getTelnetSession(..) : " + e.getMessage());
return null;
}
}