0

org.apache.commons.net.io.Util在流终止之前InputStream无法实时解析的用途。这是正确的还是不正确的?

IOUtil类对我来说是一个黑匣子。它使用org.apache.commons.net.io.Util,但这同样不透明。

具体来说,这条线Util.copyStream(remoteInput, localOutput);IOUtil有趣:

copyStream

public static final long copyStream(InputStream source,
              OutputStream dest)
                             throws CopyStreamException

Same as copyStream(source, dest, DEFAULT_COPY_BUFFER_SIZE);

Throws:
    CopyStreamException

我如何读取原始流或它的副本?实时 telnet 连接将有一个InputStream不会终止的。我在 API 中看不到这样的功能。

或者,重新实现 Apacheexamples.util.IOUtil会导致回到原来的问题:

package weathertelnet;

import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Logger;

public class StreamReader {

    private final static Logger LOG = Logger.getLogger(StreamReader.class.getName());
    private StringBuilder stringBuilder = new StringBuilder();
    private InputStream inputStream;

    public StreamReader() {
    }

    public void setInputStream(InputStream inputStream) throws IOException {
        this.inputStream = inputStream;
        readWrite();
    }

    public void readWrite() throws IOException {
        Thread reader = new Thread() {

            @Override
            public void run() {
                do {
                    try {
                        char ch = (char) inputStream.read();
                        stringBuilder.append(ch);
                    } catch (IOException ex) {
                    }
                } while (true);  //never stop reading the stream..
            }
        };

        Thread writer = new Thread() {

            @Override
            public void run() {
                //Util.copyStream(remoteInput, localOutput);
                //somehow write the *live* stream to file *as* it comes in
                //or, use org.apache.commons.net.io.Util to "get the data"
            }
        };
    }
}

要么我有一个根本的误解,要么没有重新实现(或使用反射,也许)这些 API 不允许处理实时的、未终止 InputStream的.

我真的不倾向于在这里使用反射,我认为下一个阶段是开始分解它是什么org.apache.commons.net.io.Util以及它是如何做到的,但这真的是在兔子洞里。它在哪里结束?

http://commons.apache.org/proper/commons-net/apidocs/org/apache/commons/net/io/Util.html#copyStream%28java.io.InputStream,%20java.io.OutputStream%29

4

2 回答 2

1

您可以“实时”复制 Stream ,但 InputStream 在没有更多输入时可能会阻塞。

你可以在这里看到代码org.apache.commons.net.io.Util#copyStream(...)

于 2013-08-31T02:06:26.070 回答
0

先输出:

thufir@dur:~$ 
thufir@dur:~$ java -jar NetBeansProjects/SSCCE/dist/SSCCE.jar 
print..
makeString..
cannot remove       java.util.NoSuchElementException
------------------------------------------------------------------------------
*               Welcome to THE WEATHER UNDERGROUND telnet service!            *
------------------------------------------------------------------------------
*                                                                            *
*   National Weather Service information provided by Alden Electronics, Inc. *
*    and updated each minute as reports come in over our data feed.          *
*                                                                            *
*   **Note: If you cannot get past this opening screen, you must use a       *
*   different version of the "telnet" program--some of the ones for IBM      *
*   compatible PC's have a bug that prevents proper connection.              *
*                                                                            *
*           comments: jmasters@wunderground.com                              *
------------------------------------------------------------------------------

Press Return to continue:finally -- waiting for more data..





cannot remove       java.util.NoSuchElementException
finally -- waiting for more data..

------------------------------------------------------------------------------
*               Welcome to THE WEATHER UNDERGROUND telnet service!            *
------------------------------------------------------------------------------
*                                                                            *
*   National Weather Service information provided by Alden Electronics, Inc. *
*    and updated each minute as reports come in over our data feed.          *
*                                                                            *
*   **Note: If you cannot get past this opening screen, you must use a       *
*   different version of the "telnet" program--some of the ones for IBM      *
*   compatible PC's have a bug that prevents proper connection.              *
*                                                                            *
*           comments: jmasters@wunderground.com                              *
------------------------------------------------------------------------------

Press Return to continue:



cannot remove       java.util.NoSuchElementException
^Cthufir@dur:~$ 
thufir@dur:~$ 

然后代码:

thufir@dur:~$ cat NetBeansProjects/SSCCE/src/weathertelnet/Telnet.java 
package weathertelnet;

import static java.lang.System.out;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.logging.Logger;
import org.apache.commons.net.telnet.TelnetClient;

public final class Telnet {

    private final static Logger LOG = Logger.getLogger(Telnet.class.getName());
    private TelnetClient telnetClient = new TelnetClient();

    public Telnet() throws SocketException, IOException {
        InetAddress host = InetAddress.getByName("rainmaker.wunderground.com");
        int port = 3000;
        telnetClient.connect(host, port);

        final InputStream inputStream = telnetClient.getInputStream();
        final ConcurrentLinkedQueue<Character> clq = new ConcurrentLinkedQueue();
        final StringBuilder sb = new StringBuilder();

        Thread print = new Thread() {

            @Override
            public void run() {
                out.println("print..");
                try {
                    char ch = (char) inputStream.read();
                    while (255 > ch && ch >= 0) {
                        clq.add(ch);
                        out.print(ch);
                        ch = (char) inputStream.read();
                    }
                } catch (IOException ex) {
                    out.println("cannot read inputStream:\t" + ex);
                }
            }
        };

        Thread makeString = new Thread() {

            @Override
            public void run() {
                out.println("makeString..");
                do {
                    try {
                        do {
                            char ch = clq.remove();
                            sb.append(ch);
                            // out.println("appended\t" + ch);
                        } while (true);
                    } catch (java.util.NoSuchElementException | ClassCastException e) {
                        out.println("cannot remove\t\t" + e);
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException interruptedException) {
                            out.println("cannot sleep1\t\t" + interruptedException);
                        }
                    } finally {
                        out.println("finally -- waiting for more data..\n\n" + sb + "\n\n\n");
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException interruptedException) {
                            out.println("cannot sleep1\t\t" + interruptedException);
                        }
                    }
                } while (true);
            }
        };
        print.start();
        makeString.start();
    }

    private void cmd(String cmd) throws IOException {//haven't tested yet..
        byte[] b = cmd.getBytes();
        System.out.println("streamreader has\t\t" + cmd);
        int l = b.length;
        for (int i = 0; i < l; i++) {
            telnetClient.sendCommand(b[i]);
        }
    }

    public static void main(String[] args) throws SocketException, IOException {
        new Telnet();
    }
}thufir@dur:~$ 
thufir@dur:~$ 
于 2013-08-31T20:17:01.573 回答