Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 44 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# JavaDownloadLibrary
A java library to download files and process the download speed,progress and other things
A Java library to download files and process the download speed, progress and other things

Features:

Expand All @@ -8,37 +8,61 @@ Features:
- Check download progress
- Directly cast download objects
- Download Text
- Convert file sizes (MB;GB;KB...)
- Use (Netscape format) cookies to download files that require login, optionally filter which domain name to load (to check if the cookie file contains the domain name that the programme expects)
- Able to cancel during download
- Get JSON (e.g. from a website API) as String objects
- Convert file sizes from bytes to KB, MB, GB dynamically

# Download:
https://github.com/MrMarnic/JDL/releases/download/1.0/JDL.jar


# Getting Started:

# Download File:
https://github.com/MrMarnic/JavaDownloadLibrary/releases/

# Example Usage:
**Simple download, with auto filename retrieval (without cookies/progress/speed/custom filename/custom user agent string)**
Note that a '\\' is expected at the end of the file path
```
Downloader downloader = new Downloader(false);
downloader.downloadFileToLocation("https://github.com/MrMarnic/JIconExtract/releases/download/1.0/JIconExtract.jar","C:\\Downloads\\download.zip");
Downloader downloader = new Downloader(true);
downloader.downloadFileToLocation("https://github.com/MrMarnic/JIconExtractReloaded/releases/download/v1.0/JIconExtractReloaded.jar", "C:\\demo\\test downloads\\");
```
# Add Handler (Check Speed,progress...):

**Download with file size preview, progress and speed display**
```
Downloader downloader = new Downloader(false);
downloader.setDownloadHandler(new CombinedSpeedProgressDownloadHandler(downloader) {
@Override
public void onDownloadStart() {
super.onDownloadStart();
// define custom actions to do before download starts
System.out.println("Download starting...");
}
@Override
public void onDownloadSpeedProgress(int downloaded, int maxDownload, int percent, int bytesPerSec) {
System.out.println(SizeUtil.toMBFB(bytesPerSec)+"/s " + percent + "%");
// define actions to do on each progress update
// (by default updates once every 250ms as defined in CombinedSpeedProgressDownloadHandler's onDownloadStart())
System.out.println(SizeUtil.toHumanReadableFromBytes(bytesPerSec) + "/s " + percent + "%");
}

@Override
public void onDownloadFinish() {
super.onDownloadFinish();
// define custom actions to do after download finishes
System.out.println("Download finished");
}
});
downloader.downloadFileToLocation("https://github.com/MrMarnic/JIconExtract/releases/download/1.0/JIconExtract.jar","C:\\Downloads\\download.zi");
System.out.println(SizeUtil.toHumanReadableFromBytes(downloader.getDownloadLength("https://github.com/MrMarnic/JIconExtractReloaded/releases/download/v1.0/JIconExtractReloaded.jar")));
downloader.downloadFileToLocation("https://github.com/MrMarnic/JIconExtractReloaded/releases/download/v1.0/JIconExtractReloaded.jar", "C:\\demo\\test downloads\\");
```

**Download files that require login using cookies, with custom file name**
```
Downloader downloader = new Downloader(true);
downloader.setCookies(new File("C:\\demo\\cookies-github-com.txt"));
downloader.downloadFileToLocation("https://github.com/settings/profile", "C:\\demo\\test downloads\\", "GitHub Settings page.html");
```

**Use custom agent string to pass through sites that block Java**
```
Downloader downloader = new Downloader(true);
downloader.setCustomUserAgentString("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36");
downloader.downloadFileToLocation("https://www.whatismybrowser.com/detect/what-is-my-user-agent/", "C:\\demo\\test downloads\\", "UserAgentTest.html");
```

# Handler
Expand Down Expand Up @@ -76,12 +100,14 @@ public class ExampleDownloadHandler extends DownloadHandler{

# Convert File sizes

Syntax: SizeUtil.toMBFB() = toMegaBytesFromBytes
SizeUtil.toGBFB() = toGigiaBytesFromBytes


**Explaination:**
SizeUtil.toMBFB() = toMegaBytesFromBytes
SizeUtil.toGBFB() = toGigiaBytesFromBytes
SizeUtil.toHumanReadableFromBytes() = convert bytes to KB, MB, GB (e.g. for displaying download size / download speed);

**Usage**
```
double mb = SizeUtil.toMBFB(2000000000);
double kb = SizeUtil.toKBFB(1000000);
String size = SizeUtil.toHumanReadableFromBytes(4096);
```
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void run() {
onDownloadProgress(downloader.downloadedBytes,downloader.downloadLength,percent);
onDownloadSpeedProgress(downloader.downloadedBytes,downloader.downloadLength,percent,deltaDownload);
}
},0,1000);
},0,250);
}

public void onDownloadTickPerSec(int bytesPerSec) {}
Expand Down
2 changes: 1 addition & 1 deletion src/me/marnic/jdl/DownloadProgressDownloadHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void onDownloadStart() {
public void run() {
onDownloadProgress(downloader.downloadedBytes,downloader.downloadLength,(int)(((double)downloader.downloadedBytes/downloader.downloadLength)*100));
}
},0,1000);
},0,250);
}

public abstract void onDownloadProgress(int downloaded,int maxDownload,int percent);
Expand Down
2 changes: 1 addition & 1 deletion src/me/marnic/jdl/DownloadSpeedDownloadHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void run() {
onDownloadTickPerSec(deltaDownload);
lastDownloadSize = downloader.downloadedBytes;
}
},0,1000);
},0,250);
}

public abstract void onDownloadTickPerSec(int bytesPerSec);
Expand Down
Loading