1 Reply Latest reply: Jul 25, 2008 8:42 AM by 843810 RSS

    Getting output from StreamRedirectThread.java

    843810
      Hi,
      For the purpose of my work, I am trying to store the output that is produced by StreamRedirectThread.java into a vector as shown in the code below. When I try to print out that vector the output is not as the same as it is printed in the StreamRedirectThread.java program. For example if the output form the program prints out the numbers form 0 to 5, the vector output is doubled in size and prints:
      index = 0 content=0
      index = 1 content=

      index = 2 content=1
      index = 3 content=

      index = 4 content=2
      index = 5 content=

      index = 6 content=3
      index = 7 content=

      index = 8 content=4
      index = 9 content=

      index = 10 content=5
      index = 11 content=

      Can anybody advice me what to do about it. I tried but couldn't fix it. The code of StreamRedirectThread.java is as
      import java.io.*;*
      *import java.util.*;
      
      import com.sun.jdi.*;*
      *import com.sun.jdi.request.*;
      import com.sun.jdi.event.*;*
      *import com.sun.jdi.connect.*;
      
      class StreamRedirectThread extends Thread {
      
      private final Reader in;
      private final Writer out;
      private static Vector outvector = new Vector(0); // victor of objects that contains output result
      private static final int BUFFER_SIZE = 2048;
      
      /**
      * Set up.
      * @param name Name of the thread
      * @param in Stream to copy from
      * @param out Stream to copy to
      */
      StreamRedirectThread(String name, InputStream in, OutputStream out) {
      super(name);
      this.in = new InputStreamReader(in);
      this.out = new OutputStreamWriter(out);
      setPriority(Thread.MAX_PRIORITY-1);
      }
      
      public void run() {
      try {
      char[] cbuf = new char[BUFFER_SIZE];
      int count;
      while ((count = in.read(cbuf, 0, BUFFER_SIZE)) >= 0) {
      //out.write(cbuf,0,count);
      String s = new String(cbuf,0,count);
      if (!(s.equals(null)))
      TemplateApp.vv.addElement(s);
      outvector.addElement(s); out.write(s);
      out.flush();
      }
      
      } catch(IOException exc) {
      System.err.println("Child I/O Transfer - " + exc);
      }
      }
      public synchronized static Vector getOutVector()
      {
      return outvector ;
      }
      }
      Thanks a lot for help
        • 1. Re: Getting output from StreamRedirectThread.java
          843810
          The problem was with endline stored in the vector. I did the follwing and it worked for me.
          String separator = System.getProperty("line.separator");
            while ((count = in.read(cbuf, 0, BUFFER_SIZE)) >= 0) {
                       if ( count == 0 ) {
                          continue;
                       } 
                       String s = new String(cbuf,0,count);
                       out.write(s);
                       out.flush();
                       s = s.replace(separator, "");
                       if (s.length() !=0 )
                                  outvector.addElement(s);
                                          
                 }