Solved

How to detect Internet speed ??

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
varialbe initialization 11 37
Opening PDF on button click and fill new document 2 47
printf performancy 11 69
java example issue 3 23
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

856 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