mirror of
https://github.com/magefree/mage.git
synced 2025-12-21 02:52:02 -08:00
[Updater] more refactoring
This commit is contained in:
parent
d819774cc6
commit
7b1ffb6ced
3 changed files with 55 additions and 51 deletions
|
|
@ -1,6 +1,10 @@
|
||||||
package com.magefree.update;
|
package com.magefree.update;
|
||||||
|
|
||||||
import java.io.*;
|
import com.magefree.update.helpers.ChechsumHelper;
|
||||||
|
import com.magefree.update.helpers.FileHelper;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
import java.net.HttpURLConnection;
|
import java.net.HttpURLConnection;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.net.URLConnection;
|
import java.net.URLConnection;
|
||||||
|
|
@ -166,43 +170,13 @@ public class Updater {
|
||||||
urlConnection.connect();
|
urlConnection.connect();
|
||||||
|
|
||||||
if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
|
if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
|
||||||
downloadFile(filename, urlConnection);
|
FileHelper.downloadFile(filename, urlConnection);
|
||||||
} else {
|
} else {
|
||||||
System.out.println(filename + " error status : " + urlConnection.getResponseMessage());
|
System.out.println(filename + " error status : " + urlConnection.getResponseMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Downloads specified file.
|
|
||||||
*
|
|
||||||
* @param filename
|
|
||||||
* @param urlConnection
|
|
||||||
*/
|
|
||||||
private void downloadFile(String filename, HttpURLConnection urlConnection) {
|
|
||||||
System.out.println("Downloading " + filename);
|
|
||||||
try {
|
|
||||||
InputStream in = urlConnection.getInputStream();
|
|
||||||
File f = new File(filename);
|
|
||||||
if (!f.exists()) {
|
|
||||||
f.getParentFile().mkdirs();
|
|
||||||
System.out.println("Directories have been created: " + f.getParentFile().getPath());
|
|
||||||
}
|
|
||||||
|
|
||||||
FileOutputStream out = new FileOutputStream(filename);
|
|
||||||
byte[] buf = new byte[4 * 1024];
|
|
||||||
int bytesRead;
|
|
||||||
|
|
||||||
while ((bytesRead = in.read(buf)) != -1) {
|
|
||||||
out.write(buf, 0, bytesRead);
|
|
||||||
}
|
|
||||||
|
|
||||||
System.out.println("File has been updated: " + filename);
|
|
||||||
} catch (IOException e) {
|
|
||||||
System.out.println("i/o exception - " + e.getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Removes files from the list.
|
* Removes files from the list.
|
||||||
*
|
*
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package com.magefree.update;
|
package com.magefree.update.helpers;
|
||||||
|
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
package com.magefree.update;
|
package com.magefree.update.helpers;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.*;
|
||||||
import java.io.FilenameFilter;
|
import java.net.HttpURLConnection;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
|
@ -15,6 +15,26 @@ public class FileHelper {
|
||||||
private FileHelper() {
|
private FileHelper() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Filters out dirs.
|
||||||
|
*/
|
||||||
|
private static final FilenameFilter anyFileFilter = new FilenameFilter() {
|
||||||
|
@Override
|
||||||
|
public boolean accept(File dir, String name) {
|
||||||
|
return dir.isFile();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Filters out jars.
|
||||||
|
*/
|
||||||
|
private static final FilenameFilter jarFileFilter = new FilenameFilter() {
|
||||||
|
@Override
|
||||||
|
public boolean accept(File dir, String name) {
|
||||||
|
return name.endsWith(".jar");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets .jar files from specified folder.
|
* Gets .jar files from specified folder.
|
||||||
*
|
*
|
||||||
|
|
@ -67,22 +87,32 @@ public class FileHelper {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Filters out dirs.
|
* Downloads specified file.
|
||||||
|
*
|
||||||
|
* @param filename
|
||||||
|
* @param urlConnection
|
||||||
*/
|
*/
|
||||||
private static final FilenameFilter anyFileFilter = new FilenameFilter() {
|
public static void downloadFile(String filename, HttpURLConnection urlConnection) {
|
||||||
@Override
|
System.out.println("Downloading " + filename);
|
||||||
public boolean accept(File dir, String name) {
|
try {
|
||||||
return dir.isFile();
|
InputStream in = urlConnection.getInputStream();
|
||||||
|
File f = new File(filename);
|
||||||
|
if (!f.exists()) {
|
||||||
|
f.getParentFile().mkdirs();
|
||||||
|
System.out.println("Directories have been created: " + f.getParentFile().getPath());
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
FileOutputStream out = new FileOutputStream(filename);
|
||||||
* Filters out jars.
|
byte[] buf = new byte[4 * 1024];
|
||||||
*/
|
int bytesRead;
|
||||||
private static final FilenameFilter jarFileFilter = new FilenameFilter() {
|
|
||||||
@Override
|
while ((bytesRead = in.read(buf)) != -1) {
|
||||||
public boolean accept(File dir, String name) {
|
out.write(buf, 0, bytesRead);
|
||||||
return name.endsWith(".jar");
|
}
|
||||||
|
|
||||||
|
System.out.println("File has been updated: " + filename);
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.out.println("i/o exception - " + e.getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
|
||||||
}
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue