2 Replies Latest reply: Feb 7, 2004 7:05 AM by 807582 RSS

    Synchronize JProgressBar and OutputStream

    807582
      Hello;

      I read a file via a FileInputStream and I write byte[] in a DataOutputStream over the network (via HTTPURLconnection and not a Java socket). I implements a JProgressBar to know the size is send, but it seems that the progressbar read the size is read and not the size is send. My question is: is there a way to know exactelly the byte size really send over a HttpUrlConnection at a time ???

      thanks a lot

        • 1. Re: Synchronize JProgressBar and OutputStream
          807582

          Here's some psuedocode on how you should be doing it..

          regards,
          Owen
          progressbar.setMaximum ( 0 );
          progressbar.setMaximum ( 100 );
          
          long fileSize = file.getSize();
          long sentByteCount = 0;
          
          while ( sent != fileSize )
          {
              sentByteCount + = outstream.write ( whatever );
              // working in percentages
              progressbar.setValue ( (int) ( (sent / fileSize) * 100.0 );
          }
          progressbar.setValue ( 100 );
          • 2. Re: Synchronize JProgressBar and OutputStream
            807582
            You might want to create a thread that asks the outputstream for its progress periodically instead. If you write to a file, for instance, and update the progressbar every time write read a byte, you might end up with about a million calls to repaint, which isn't good.

            So create an outputstream:
            public class ProgressOutputStream extends OutputStream
            {
               OutputStream source;
               int bytesWritten;
            
               public ProgressOutputStream(OutputStream source)
               {
                  this.source = source;
               }
            
               public void write(int n) throws IOException
               {
                  bytesWritten += 1;
                  source.write(n);
               }
            
               public int getBytesWritten()
               {
                  return bytesWritten;
               }
            }
            And this:
            public class StreamProgressBar extends JProgressBar
            {
            
                OutputStream stream;
            
               public StreamProgressBar(OutputStream out, int size)
               {
                  stream = new ProgressOutputStream(out);
                  setMinimumValue(0);
                  setMaximumValue(size);
               }
            
               private class ProgressMonitorThread extends Thread
               {
                  public void run()
                  {
                      try{Thread.sleep(100);}catch(Exception e){e.printStackTrace();}
                      setValue(out.getBytesWritten());
                  }
               }
            
            }
            This is just an example, and can be made much nicer...

            Regards,
            Nille