InputStream复制到需要OutputStream的Apa

InputStream复制到需要OutputStream的Apa

本文介绍了如何将InputStream复制到需要OutputStream的Apache TeeOutputStream?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我修改了Apache IOUtil 来处理 WeatherTelnet 有点不同。虽然 WeatherTelnet 的注释表明:

我想使用Apache TeeOutputStream ,但是 API分支 OutputStream ,而 TelnetClient output一个 InputStream 的形式,所以当然可以读取。方便的话,Apache copyStream 实用程序方法将复制一个InputStream到一个OutputStream。

printKindaWorks 或<$ c中将 InputStream 复制到 OutputStream $ C> printToFile ?
2.)我如何将 OutputStream 写入一个文件,或者将它打开?

诀窍是在用户与天气服务器交互时执行这些操作 live



我做了我的最好看看 copyStream 的源代码,并用它作为参考,但它只是简单的不起作用。



实用程序类:

  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;
导入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; ((ch = System.in.read())!= -1){
outputStream.write(ch);

尝试{
while
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;
尝试{
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)抛出FileNotFoundException,IOException {
final String fname =if.log;
文件f =新文件(fname);
f.createNewFile();
Thread fileWriter = new Thread(){

@Override
public void run(){
char c = 0;
int r = 0; ((r = inputStream.read())!= -1){
c =(char)r;
尝试{
while
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();

$ b $ public static void readWriteLog(final InputStream inputStream,final OutputStream outputStream)throws FileNotFoundException,IOException {
readFromConsole(outputStream);
writeToConsole(inputStream);
writeToFile(inputStream); //不会写太多
// printKindaWorks(inputStream); //阻止writeToConsole?


$ / code $ / pre

和驱动程序

 包装天气; 

导入apache.IOUtil;
import java.io.IOException;
导入java.net.InetAddress;
import java.net.UnknownHostException;
导入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 分开。 方案

你需要实例化一个包裹System.out和日志文件的TeeOutputStream。然后创建一个将Telnet InputStream复制到TeeOutputStream的线程。 (你只能有一个消耗telnet InputStream的线程)

I've adapted the Apache IOUtil sample to work with the WeatherTelnet sample a little differently. While the comments for WeatherTelnet state that:

I would like to split the output using Apache TeeOutputStream, but the API branches OutputStream whereas TelnetClient "output" is in the form of an InputStream, so that, of course, it can be read. Conveniently, the Apache copyStream utility method will copy an InputStream to an OutputStream.

1.) How do I copy the InputStream to an OutputStream in printKindaWorks or printToFile? 2.) How do I either write the OutputStream to a file, or tee it?

the trick is to do these operations live, while the user interacts with the weather server.

I did my best to look at the source code for copyStream and used that for reference, but it just plain doesn't work. I haven't yet looked at the internals for the Apache implementation of tee.

The utility class:

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 ?
    }
}

and the driver:

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());
    }
}

Please consider the code under the ASL.

While I am "logging" the InputStream I'm not not working on a logging problem, writing to a file is just for illustration purposes. I just want to split the InputStream while the user interacts with the weather server.

解决方案

You need to instantiate a TeeOutputStream which wraps System.out and the log file. Then create a thread which copies the telnet InputStream to the TeeOutputStream. (you can only have a single thread consuming the telnet InputStream)

这篇关于如何将InputStream复制到需要OutputStream的Apache TeeOutputStream?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 04:41