Solved

How to detect Internet speed ??

Posted on 2002-05-19
5
260 Views
Last Modified: 2008-03-17
I have a pure java program and it will downlod some file from server. When it download files from server, simetime it will take long time if files size is big. I plan to use JProgressBar to show user the progress. But I have a problem to calculate the total time for download. Actually I have below two question:
1) how to detect the Internet speed
2) how to get file size in server side--suppose I have URL for file in server side.

If I can handle above two problems, I can get total download time. Then I can use JProgressBar. Tahnks
gary
0
Comment
Question by:garyshi
  • 3
5 Comments
 
LVL 1

Expert Comment

by:kckchak
ID: 7020792
Hi,
For your second question: we can create and URLConnection object and call 'getContentLength()', after 'connect()', which will return the length of the file or content.

Hope this is useful for you.

bye,
kckchak
0
 
LVL 3

Expert Comment

by:msterjev
ID: 7020951
You are detecting Internet speed with the information you already have (bytes received). So you must take the start time and calculate the estimated time after each retrieval of chunk of bytes. I would provide you in a short example code.
0
 
LVL 3

Accepted Solution

by:
msterjev earned 50 total points
ID: 7021124
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
import java.text.DecimalFormat;

public class Downloader extends JFrame implements ActionListener,Runnable
{
     JButton connect;
     JTextArea content;
     JProgressBar progress;
     JTextField address;
     JPanel navigator,progressPanel;
     JLabel estimatedTime;
     Thread thread;
     
     public Downloader()
     {
          super("DOWNLOADER");
          setSize(500,600);
          getContentPane().setLayout(new BorderLayout());
          connect=new JButton("Connect");
          address=new JTextField(30);
          navigator=new JPanel();
          navigator.add(address);
          navigator.add(connect);
          content=new JTextArea(25,50);
          progress=new JProgressBar();
          progress.setForeground(Color.red);
          estimatedTime=new JLabel();
          progressPanel=new JPanel(new GridLayout(2,1));
          progressPanel.add(estimatedTime);
          progressPanel.add(progress);
          getContentPane().add(navigator,BorderLayout.NORTH);
          getContentPane().add(new JScrollPane(content),BorderLayout.CENTER);
          getContentPane().add(progressPanel,BorderLayout.SOUTH);
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          pack();
          connect.addActionListener(this);
          setVisible(true);
     }
     
     public void actionPerformed(ActionEvent e)
     {
          if(e.getSource()==connect)
          {
               if(thread!=null)
               {
                    thread.stop();
                    thread=null;
               }
               progress.setValue(0);
               content.setText("Loading...");
               estimatedTime.setText("");
               thread=new Thread(this);
               thread.start();
          }
     }
     
     public void run()
     {
          DecimalFormat df=new DecimalFormat("#,###.00");
          try
          {
               URL url=new URL(address.getText());
               HttpURLConnection urlConnection=(HttpURLConnection)url.openConnection();
               InputStream in=urlConnection.getInputStream();
               ByteArrayOutputStream baos=new ByteArrayOutputStream();
               BufferedOutputStream bos=new BufferedOutputStream(baos);
               byte[] buffer=new byte[10];
               int contentLength=urlConnection.getContentLength();
               int c,readed;
               readed=0;
               long start=System.currentTimeMillis();
               long end;
               float speed;
               while((c=in.read(buffer,0,10))!=-1)
               {
                    end=System.currentTimeMillis();
                    readed+=c;
                    if((end-start)!=0)
                    {
                         speed=(end-start)/c;
                         start=end;
                         estimatedTime.setText("Estimated time left: "+df.format((contentLength-readed)/(speed*1000.0))+" seconds...");
                    }
                    bos.write(buffer,0,c);
                    progress.setValue((int) (readed*100/contentLength));
                    if(readed==contentLength)
                         break;
               }
               estimatedTime.setText("Finished.");
               in.close();
               urlConnection.disconnect();
               content.setText(new String(baos.toByteArray()));
          }
          catch(Exception e)
          {
               content.setText(e.getClass()+":"+e.getMessage());
          }
     }
     public static void main(String[] args)
     {
          Downloader downloader=new Downloader();
     }
}
0
 
LVL 3

Expert Comment

by:msterjev
ID: 7021125
0
 

Author Comment

by:garyshi
ID: 7022132
Hi Msterjev,
Thanks a lot. I got idea to handle this problem. Also thanks all other reponsers.
Gary
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
maven module vs maven project 3 69
Java basic valueOf question 1 32
Cisco ASA: Java web start no go, asdm launcher no go 3 56
Java Inheritance super keyword use 8 30
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question