aspose file tools*
The moose likes I/O and Streams and the fly likes org.apache.commons.net.io.Util.copyStream usage to parse a live telnet session Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "org.apache.commons.net.io.Util.copyStream usage to parse a live telnet session" Watch "org.apache.commons.net.io.Util.copyStream usage to parse a live telnet session" New topic
Author

org.apache.commons.net.io.Util.copyStream usage to parse a live telnet session

Piter Smith
Ranch Hand

Joined: Feb 25, 2009
Posts: 31
The Apache WeatherTelnet example uses org.apache.commons.net.io.Util.copyStream to read and concurrently. The threading is done in examples.util.IOUtil, which I would like to change the behaviour of.

Currently, IOUtil sends a copy of the stream to System.out, which works fine for a telnet session.

However, I would like to add a new thread, which "intercepts" or reads the stream *live*.

I can't think of an approach which allows the processing of a stream while the stream is still open and while bytes are being read.

links:


http://svn.apache.org/repos/asf/commons/proper/net/trunk/src/main/java/examples/util/IOUtil.java

http://svn.apache.org/repos/asf/commons/proper/net/tags/NET_1_0_0/src/java/examples/weatherTelnet.java

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

Joined: Oct 14, 2005
Posts: 18541
    
    8

Well, just off the top of my head what I would try would be to call System.setOut to redirect System.out to some other PrintStream of your choosing. I'm not clear on what you want to do with this data, so I can't really suggest what kind of PrintStream you'd want to use.
Piter Smith
Ranch Hand

Joined: Feb 25, 2009
Posts: 31
Paul Clapham wrote:Well, just off the top of my head what I would try would be to call System.setOut to redirect System.out to some other PrintStream of your choosing. I'm not clear on what you want to do with this data, so I can't really suggest what kind of PrintStream you'd want to use.


That sounds like the right approach. It's for a a MUD client:

http://en.wikipedia.org/wiki/Mud_client

For now, if I get it into a String, that would be sufficient. This sounds like a good idea, I was thinking along those lines.

Can I send System.setOut to a String, then send the String to the screen? There has to be some sort of output to the screen.
Luan Cestari
Ranch Hand

Joined: Feb 07, 2010
Posts: 162

I think you could use http://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/output/TeeOutputStream.html , creating it using both the Output to the file and for the standard output, then just setting this object into System.out (using a new PrintStream). Another option is to create your own OutputStream with your own business requirement.

Regards,
Luan Cestari


Please, visit me for some cool tech post at www.ourdailycodes.com
Piter Smith
Ranch Hand

Joined: Feb 25, 2009
Posts: 31
Luan Cestari wrote:I think you could use http://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/output/TeeOutputStream.html , creating it using both the Output to the file and for the standard output, then just setting this object into System.out (using a new PrintStream). Another option is to create your own OutputStream with your own business requirement.

Regards,
Luan Cestari


where were you last week!? Good link, thank you
Luan Cestari
Ranch Hand

Joined: Feb 07, 2010
Posts: 162

Piter Smith wrote:
Luan Cestari wrote:I think you could use http://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/output/TeeOutputStream.html , creating it using both the Output to the file and for the standard output, then just setting this object into System.out (using a new PrintStream). Another option is to create your own OutputStream with your own business requirement.

Regards,
Luan Cestari


where were you last week!? Good link, thank you


Welcome I feel very happy to hear that =) So it worked fine, right? If you got any problem, just post here
Piter Smith
Ranch Hand

Joined: Feb 25, 2009
Posts: 31
I'm working on regex now, but will definitely go back and change that. Right now it's threaded, and it might need threads, but might run fine without:

https://github.com/THUFIR/TelnetConsole


so, I'm assuming, for now that it works. The API documentation is pretty clear.
Piter Smith
Ranch Hand

Joined: Feb 25, 2009
Posts: 31
Luan Cestari wrote:

Welcome I feel very happy to hear that =) So it worked fine, right? If you got any problem, just post here




Sadly, no I don't see a provision in that Apache API for anything like the functionality of the tee command. On the plus, the real tee command could fit the bill and remove 2/3 of, and most of the complexity to, the project!

If I "telnet mymud.com 123 | tee processThis.txt" then I can, with relative ease, parse the end of that file?

When I run tail -f processThis.txt it *seems* to write output concurrently, as the man page says it will, or close enough! If you see a provision in the Apache API to actually *get* the split output, please let me know, but I see nothing along those lines. Apache will copy the stream, but there's no mechanism to get the copied stream(??).


-Piter
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: org.apache.commons.net.io.Util.copyStream usage to parse a live telnet session