开发者

InputStream from file, OutputStream to a file

开发者 https://www.devze.com 2023-03-06 12:04 出处:网络
I have this code in the main class: IOUtil.readWrite(telnet.getInputStream(), telnet.getOutputStream(),

I have this code in the main class:

IOUtil.readWrite(telnet.getInputStream(), telnet.getOutputStream(),
    System.in, System.out);

This works very well, since System.in gets the inputs from the user nad System.out prints all the outputs.

I was trying to change this, so instead of System.in could be another InputStream object that reads one line from a file each time asks for an input, and also System.out could be an Object that writes all the output to a file.

the IOUtil class is the following:

package examples;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.commons.net.io.Util;

public final class IOUtil
{

public  final static void readWrite(final InputStream remoteInput,
                                   final OutputStream remoteOutput,
                                   final InputStream localInput,
                                   final OutputStream localOutput)
{
    Thread reader, writer;

    reader = new Thread()
             {
                 public void run()
                 {
                     int ch;

                     try
                     {
                         while (!interrupted() && (ch = localInput.read()) != -1)
                         {
                             remoteOutput.write(ch);
                             remoteOutput.flush();
                         }
                     }
                     catch (IOException e)
                     {
                         //e.printStackTrace();
                     }
                 }
             }
             ;

    writer = new Thread()
             {
                 public void run()
                 {
                     try
                     {
                         Util.copyStream(remoteInput, localOutput);                                
                     }
                     catch (IOException e)
开发者_StackOverflow社区                     {
                         e.printStackTrace();
                         System.exit(1);
                     }
                 }
             };


    writer.setPriority(Thread.currentThread().getPriority() + 1);

    writer.start();
    reader.setDaemon(true);
    reader.start();

    try
    {
        writer.join();
        reader.interrupt();
    }
    catch (InterruptedException e)
    {
    }
}


For the replacement for System.out, simply use a FileOutputStream.

For the replacement for System.in, you could use a FileInputStream. This doesn't deliver input a line at a time, but I expect that the remote telnet server can handle type-ahead, so that shouldn't matter.

If type-ahead does matter, then you've got a difficult problem. The input side of your telnet client has to synchronize with the output side, and wait until the remote server / shell is expecting the next line before sending it.

  • The IOUtil.readWrite method (as written) doesn't do that.
  • Doing it requires the output side to notice command prompts (or something) and tell the input side to write the next line of input. That's tricky ... and fragile for the following reasons:
    • You don't know for sure what the command prompt(s) will look like.
    • One of the commands could change the command prompts on the fly.
    • The command output could look like a shell command prompt.

I had a brief look at the telnet protocol, and I could see nothing that says that the client has to send data a line at a time.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号