Solved

How to detect Internet speed ??

Posted on 2002-05-19
5
266 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

724 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