title: Measuring Download speed with Eclipse
body: <p>A couple of days ago i've got suggested this code for measuring download speed. However I don't quite understand what is required in URL &amp; outputfile for the code to work. Can someone explain to me? I only have a very basic level of understanding of java. Thanks in advance.</p>
  
  <pre><code>public class Speed {
      @SuppressWarnings("resource")
      public double getSpeed() throws IOException{
          URL website = new URL("https://www.youtube.com/"); //The source website
          ReadableByteChannel rbc = Channels.newChannel(website.openStream());
          File outputFile = new File("output.jpg"); //The output file
          outputFile.createNewFile();
          FileOutputStream fos = new FileOutputStream(outputFile);
          long startTime = System.nanoTime(); //Measure when you start to download the file, we know that the time it takes to download a file is endTime-startTime
          fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
  
          long endTime = System.nanoTime(); //Measure when we're done downloading the file
          long fileBytes = outputFile.length();
          double downloadTimeSeconds = ((double)(endTime-startTime))/1000000000; //1 billion nanoseconds in a second
          double bytesPerSecond = ((double)fileBytes)/downloadTimeSeconds;
          return bytesPerSecond;          
      }
  }
  </code></pre>
  
  <p>I have tested the code with download.html as file output &amp; <a href="http://www.thinkbroadband.com/download.html" rel="nofollow">http://www.thinkbroadband.com/download.html</a> as the URL. However it is just returning values between 3KB - 300KB regardless if I am downloading a file or not...</p>