Solved

How to detect Internet speed ??

Posted on 2002-05-19
5
257 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

776 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