Solved

How to detect Internet speed ??

Posted on 2002-05-19
5
252 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
0
 

Author Comment

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

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now