Skip to content

Commit

Permalink
SpeedTest: Testfile no longer gets stored on Harddrive
Browse files Browse the repository at this point in the history
-) Thanks to Eigs @ LTEForum.at
--) If speedtest is run from a network drive, the speedtest would download the file and then upload it to the network drive. This might influence the speedtest, as the network drive might not be able to store the data as fast as the data is being read from the Host. This might get event worse if the Network Drive is not located in the local network.
-- ) Solutions: Don't store the Testfile to the Drive ( Just do nothing with the data received.
  • Loading branch information
Myrinia committed Jan 8, 2020
1 parent ed3c73c commit 3826ff2
Showing 1 changed file with 5 additions and 36 deletions.
41 changes: 5 additions & 36 deletions src/NetworkToolkit/BackgroundDownloadThread.java
Expand Up @@ -3,16 +3,10 @@
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.DirectoryNotEmptyException;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Paths;

import javax.swing.Timer;


Expand All @@ -33,61 +27,36 @@ public void run() {
}

private void startDownload() {
URL url;

try {
url = new URL(m_DownloadUrl);
URL url = new URL(m_DownloadUrl);
setTargetFileSize(url);
m_TestRunning = true;
m_CountdownTimer.start();
try (BufferedInputStream in = new BufferedInputStream(url.openStream());
FileOutputStream fileOutputStream = new FileOutputStream("TestFile")) {
byte dataBuffer[] = new byte[524288];
try {
BufferedInputStream in = new BufferedInputStream(url.openStream());
byte dataBuffer[] = new byte[524288];
int bytesRead;

while (m_TestRunning && (bytesRead = in.read(dataBuffer, 0, 524288)) != -1) {
m_BytesLoaded += bytesRead;
Main.m_StatisticHandler.addHostSpeed(m_DownloadHostName, m_CurrentRunTime , m_BytesLoaded);
fileOutputStream.write(dataBuffer, 0, bytesRead);
}

fileOutputStream.flush();
fileOutputStream.close();

} catch (IOException e) {
System.out.println("Download Error");
m_CountdownTimer.stop();
// handle exception
}

m_CountdownTimer.stop();
deleteTestfile();

} catch (MalformedURLException e1) {
m_CountdownTimer.stop();
m_TestRunning = false;
}
}

private void deleteTestfile() {
try {
if(Files.exists(Paths.get("TestFile")))
{
Files.delete(Paths.get("TestFile"));
System.out.println("TestFile removed!");
}else
{
System.out.println("TestFile Does not exist!");
}
} catch(NoSuchFileException e) {
System.out.println("No such file/directory exists");
} catch(DirectoryNotEmptyException e) {
System.out.println("Directory is not empty.");
} catch(IOException e)
{
System.out.println("Invalid permissions.");
}
}

private void setTargetFileSize(URL url) {
HttpURLConnection conn = null;
try {
Expand Down

0 comments on commit 3826ff2

Please sign in to comment.