Note that there are some explanatory texts on larger screens.

plurals
  1. POjava clear (not close) InputStream
    text
    copied!<p>I wrote a server/client application in java. Now I have to create a testing component to interact with the server interactively. The testing component itself consists of a class to handle client-instances (a ProcessBuilder for each one). </p> <p>Now to the problem: to parse the server-responses (find the end of the response), I close the specific lines within escape sequences (e.g. \t\b). This works as expected, but before I send a specific command to the server, I have to clear the InputStream (discard all things within the stream, like log-entries, status-msg and so on).</p> <pre><code>//input=process.getInputStream(); //out = new BufferedWriter(new OutputStreamWriter(process.getOutputStream())); public String send(String command){ log.info("execute: " + command); try{ //clear old output int chars = input.available(); log.info("Skip " + chars + " characters from process output."); input.skip(chars); out.write(command + "\n"); out.flush(); }catch (IOException e){ log.error("could not write to process", e); } BufferedReader reader = new BufferedReader(new InputStreamReader(input)); StringBuilder str = new StringBuilder(); String line; try { while( (line = reader.readLine()) != null ){ log.info("new line " + line); if(line.contains("\t\b")){ //skip others input.skip(input.available()); break; } } } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } return str.toString(); } </code></pre> <p>But this doesn't work.</p> <p><strong>Example Output</strong></p> <pre><code>0 [main] INFO loadtesting.ClientHandler - ClientHandler started 1543748404 (localhost 9000 ) 6 [main] INFO loadtesting.ClientHandler - execute: somecommand 6 [main] INFO loadtesting.ClientHandler - Skip 0 characters from process output. 112 [main] INFO loadtesting.ClientHandler - new line Connection successfully 113 [main] INFO loadtesting.ClientHandler - new line no entries available\t\b </code></pre> <p>"Connection successfully" should be discarded.</p> <pre><code>113 [main] INFO loadtesting.ClientHandler - execute: somecommand 113 [main] INFO loadtesting.ClientHandler - Skip 0 characters from process output. 114 [main] INFO loadtesting.ClientHandler - new line no entries available\t\b 114 [main] INFO Test - Test: close 114 [main] INFO loadtesting.ClientHandler - ClientHandler Closed 1543748404 114 [main] INFO loadtesting.ClientHandler - und ciao </code></pre> <p><strong>Update</strong> I found the problem, but no solution. It's because the available method of InputStream always returns 0:</p> <blockquote> <p>The available method for class InputStream always returns 0.</p> </blockquote> <p>Now I will search for an child of IS, who implements available().</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload