Reputation: 941
I want to create an updater, but I am not sure how I want to do this part. I want that my updater checks for updates and then count all the updates it has to do, so like: You still need 3/5 updates. or something like that. I know have that it checks if there is an update available and if there is an update then it downloads that update directly and after that it checks for another update and downloads that, but I want it to check all updates and after that download them. This is the Launcher class
package com;
import com.check.Checker;
import com.download.DownloadData.DownloadFiles;
public class Launcher {
public static void main(final String[] args) {
for (DownloadFiles df: DownloadFiles.values()) {
String fileName = df.fileName;
String URL = df.URL;
switch(Checker.isLatest(fileName, URL)) {
case 0:
System.out.println("Downloading " + fileName + ".");
break;
case 1:
System.out.println("Updating " + fileName + ".");
break;
case 2:
System.out.println(fileName + " is up to date.");
break;
}
}
}
}
and this is the isLatest method:
public static byte isLatest(String fileName, String downloadUrl) {
if (!hasFile(fileName)) {
System.out.println("[" + fileName + "]" + "There is no file yet.");
return 0;
}
try {
if (DigestUtils.md5Hex(new FileInputStream(Settings.saveDir)).equalsIgnoreCase(DigestUtils.md5Hex(new URL(downloadUrl).openStream()))) {
System.out.println("[" + fileName + "]" + "Your file is up to date.");
return 2;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("[" + fileName + "]" + "Your file needs to be updated.");
return 1;
}
So my question is: How do I make the launcher do it in this order: check the updates. List the updates and after that download the updates.
Upvotes: 3
Views: 232
Reputation: 1116
I am not entirely sure that I understood your question, but maybe you want to do sth like:
//in Launcher
EnumSet<DownloadFileS> filesToUpdate = checkForUpdates(DownloadFiles.values());
listFilesToUpdate(filesToUpdate);
downloadFiles(filesToUpdate);
Then in checkForUpdates
you just create the list of files which have changed since last sync, in listFilesToUpdate
you simply print them out and in downloadFiles(filesToUpdate)
you download them. Basically you already have everything in your isLatest
method - you just need to split it by responsibility.
UPDATE
public List<DownloadFiles> checkForUpdates(List<DownloadFiles> allFiles) {
allFiles.stream()
.filter(file -> !hasFile(file.getFilename()) || !fileUpToDate(file))
.collect(toList())
private boolean isFileUpToDate(DownloadFiles file) {
return DigestUtils.md5Hex(new FileInputStream(Settings.saveDir))
.equalsIgnoreCase(DigestUtils.md5Hex(new URL(downloadUrl).openStream())));
NEXT UPDATE
public static void main(String[] args) {
List<DownloadFiles> filesToUpdate = checkForUpdates(DownloadFiles.values());
listFilesToUpdate(filesToUpdate);
downloadFiles(filesToUpdate);
}
private static void downloadFiles(List<DownloadFiles> filesToUpdate) {
filesToUpdate.stream()
.forEach(file -> downloadFile(file));
}
private static void downloadFile(DownloadFiles file) {
try {
URL website = new URL(file.URL);
ReadableByteChannel rbc = Channels.newChannel(website.openStream());
FileOutputStream fos = new FileOutputStream(file.filename);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private static void listFilesToUpdate(List<DownloadFiles> filesToUpdate) {
System.out.println("Following files will be updated");
filesToUpdate.stream()
.forEach(System.out::println);
}
public static List<DownloadFiles> checkForUpdates(DownloadFiles[] allFiles) {
return Arrays.asList(allFiles).stream()
.filter(file -> !hasFile(file.filename) || !isFileUpToDate(file))
.collect(Collectors.toList());
}
private static boolean isFileUpToDate(DownloadFiles file) {
try (InputStream is = new URL(file.URL).openStream()) {
return DigestUtils.md5Hex(new FileInputStream(Settings.saveDir))
.equalsIgnoreCase(DigestUtils.md5Hex(is));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
Upvotes: 5
Reputation: 121
I'm not sure in terms of code but you would want to have the client connect to the server and check some method of what version of updates it is on. e.g. Client is on 1.0.3 and server is on 1.0.5 so x must need updating. Then you would need to download/update the files and install them or whatever.
Finally you would need to update the version listing in the client and store it somewhere (text file?) so that the process can start from the current point next time.
[Can't comment. Sorry]
Upvotes: 2