私は ApacheIOUtil
サンプルを少し異なる方法でWeatherTelnet
サンプルと連携するように調整しました。WeatherTelnet
状態のコメントは次のとおりです。
単独で使用される TelnetClient クラスは、インタラクティブな使用ではなく、Telnet リソースへのアクセスの自動化を主な目的としています。
Apache を使用して出力を分割したいのですTeeOutputStream
が、APIは分岐しますがOutputStream
、TelnetClient
「出力」は の形式であるInputStream
ため、もちろん読み取ることができます。便利なことに、ApachecopyStream
ユーティリティ メソッドは InputStream を OutputStream にコピーします。
1.) をinまたはにコピーするInputStream
にはどうすればよいですか? 2.)をファイルに書き込む、またはティーにするにはどうすればよいですか?OutputStream
printKindaWorks
printToFile
OutputStream
秘訣は、ユーザーが気象サーバーと対話している間に、これらの操作をライブで行うことです。
頑張ってソースコードを見copyStream
て参考にしたのですが、どうもうまくいきません。ティーの Apache 実装の内部構造はまだ見ていません。
ユーティリティ クラス:
package apache;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.net.io.Util;
public final class IOUtil {
private static final Logger log = Logger.getLogger(IOUtil.class.getName());
private static void readFromConsole(final OutputStream outputStream) {
Thread read = new Thread() {
@Override
public void run() {
int ch;
try {
while ((ch = System.in.read()) != -1) {
outputStream.write(ch);
outputStream.flush();
}
} catch (IOException ioe) {
log.warning(ioe.toString());
}
}
};
read.start();
}
private static void writeToConsole(final InputStream inputStream) {
Thread write = new Thread() {
@Override
public void run() {
try {
Util.copyStream(inputStream, System.out);
} catch (IOException ioe) {
log.warning(ioe.toString());
}
}
};
write.start();
}
private static void printKindaWorks(final InputStream inputStream) {
Thread write = new Thread() {
@Override
public void run() {
PrintStream printStream = null;
try {
File file = new File("weather.log");
FileOutputStream fos = new FileOutputStream(file, true);
printStream = new PrintStream(fos);
Util.copyStream(inputStream, printStream);
} catch (IOException ioe) {
log.warning(ioe.toString());
}
}
};
write.start();
}
// TeeOutputStream tee = new TeeOutputStream(inputStream, bis);
private static void writeToFile(final InputStream inputStream) throws FileNotFoundException, IOException {
final String fname = "whether.log";
File f = new File(fname);
f.createNewFile();
Thread fileWriter = new Thread() {
@Override
public void run() {
char c = 0;
int r = 0;
try {
while ((r = inputStream.read()) != -1) {
c = (char) r;
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(fname, true)));
out.print(c);
out.close();
}
} catch (IOException ex) {
Logger.getLogger(IOUtil.class.getName()).log(Level.SEVERE, null, ex);
}
}
};
fileWriter.start();
}
public static void readWriteLog(final InputStream inputStream, final OutputStream outputStream) throws FileNotFoundException, IOException {
readFromConsole(outputStream);
writeToConsole(inputStream);
writeToFile(inputStream); //doesn't write much
// printKindaWorks(inputStream); //blocks writeToConsole ?
}
}
そしてドライバー:
package weather;
import apache.IOUtil;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import org.apache.commons.net.telnet.TelnetClient;
public class Weather {
public Weather() {
}
public static void main(String[] args) throws UnknownHostException, IOException {
int port = 3000;
InetAddress host = InetAddress.getByName("rainmaker.wunderground.com");
TelnetClient telnetClient = new TelnetClient();
telnetClient.connect(host, port);
IOUtil.readWriteLog(telnetClient.getInputStream(), telnetClient.getOutputStream());
}
}
ASL の下のコードを検討してください。
私は「ログを記録」してInputStream
いますが、ログの問題に取り組んでいるわけではありません。ファイルへの書き込みは、説明のためだけのものです。InputStream
ユーザーが天気予報サーバーとやり取りしている間に分割したいだけです。