0

I am getting a input stream from apache's telnet client.Every time I send a command to telnet client it writes the terminal output back to InputStream,but this stream remains open until the telnet session.

Now I want a way to read the data on this stream till the end.Problem is end can't be determined as the stream is always open.One workaround I found was to read data till a specific character is encountered(which is prompt in most of the cases).but the prompt keeps on changing based on command and I have no way to know what it ll be after command execution.

there is a similar question on SO which explains it better but there is no answer :

Problems with InputStream

Please help...

7
  • If the stream stays open, what exactly do you mean by "the end"? How would you expect to detect the difference between having read everything, and another byte being available in 5 seconds?
    – Jon Skeet
    Oct 30, 2012 at 10:04
  • The only 'real' end there is, is when the InputStream gets closed which results in read() returning -1. Everything else is protocoll specific.
    – Neet
    Oct 30, 2012 at 10:10
  • @JonSkeet by "the end" I mean until data is availbale.any way to find this?there is a method available(),but that is not reliable and cpu intensive too
    – vishesh
    Oct 30, 2012 at 10:26
  • @vishesh: You realize you'll never know when you've really reached the end though, right? There could be some more data coming in a few seconds...
    – Jon Skeet
    Oct 30, 2012 at 10:50
  • @Neet No. The real end is when the peer closes his end of the connection. Closing your own end before that is just a deliberate interruption. Nothing to do with the 'real end'.
    – user207421
    Oct 30, 2012 at 10:56

2 Answers 2

0

You need to spawn a seperate Thread for reading. You cannot just go in a "Ping-Pong"-manner on one thread for exactly the reason you are faced with.

By the way: The question you linked by now has an accepted answer. It suggests not to drive the CPU to 100% load which is really good advice :)

The read Method however blocks, so you just can put it in a loop into a thread and call back when there is something received. End the loop and terminate the thread on IOException or "-1" and live happily everafter :)

2
  • Thanks for help. The answer of linked question suggests not to use available() method.It's just an answer to -why OP was facing problem,not a way to solve it.
    – vishesh
    Oct 30, 2012 at 10:24
  • 1
    It actually suggests not to use it in a loop in a thread. Because this will drive the CPU to 100% load. The read-method blocks, so there is not the danger of spamming the CPU.
    – Fildor
    Oct 30, 2012 at 10:43
0

finally I did using timeout.So basically,I did that ,if character is not available for 1 sec then give up.Instead of inputStream.read() I used this :

private char readChar(final InputStream in){
        ExecutorService executor = Executors.newFixedThreadPool(1);
        //set the executor thread working
        Callable<Integer> task = new Callable<Integer>() {
            public Integer call() {
               try {
                   return in.read();
               } catch (Exception e) {
                  //do nothing
               }
               return null;
            }
         };

         Future<Integer> future = executor.submit(task);
         Integer result =null;
         try {
              result= future.get(1, TimeUnit.SECONDS); //timeout of 1 sec
          } catch (TimeoutException ex) {
              //do nothing
          } catch (InterruptedException e) {
             // handle the interrupts
          } catch (ExecutionException e) {
             // handle other exceptions
          } finally {
              future.cancel(false);
              executor.shutdownNow();
           }

         if(result==null)
             return (char) -1;
        return  (char) result.intValue();
    } 
1
  • You don't need all this stuff. Just call Socket.setSoTimeout() and handle the resulting SocketTimeoutException.
    – user207421
    Apr 12, 2017 at 0:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.