Split util methods from UpdateCheckerTask

This commit is contained in:
Frank van der Heijden 2020-06-28 23:10:59 +02:00
parent 49e0be1b37
commit 7a2d21889a
No known key found for this signature in database
GPG key ID: 26DA56488D314D11
2 changed files with 82 additions and 45 deletions

View file

@ -1,32 +1,22 @@
package net.frankheijden.serverutils.tasks;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Method;
import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.URLConnection;
import java.net.UnknownHostException;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.charset.StandardCharsets;
import java.util.logging.Level;
import net.frankheijden.serverutils.ServerUtils;
import net.frankheijden.serverutils.config.Config;
import net.frankheijden.serverutils.config.Messenger;
import net.frankheijden.serverutils.managers.VersionManager;
import net.frankheijden.serverutils.utils.FileUtils;
import net.frankheijden.serverutils.utils.VersionUtils;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
@ -84,7 +74,7 @@ public class UpdateCheckerTask implements Runnable {
JsonObject jsonObject;
try {
jsonObject = readJsonFromUrl(GITHUB_LINK).getAsJsonObject();
jsonObject = FileUtils.readJsonFromUrl(GITHUB_LINK).getAsJsonObject();
} catch (ConnectException | UnknownHostException | SocketTimeoutException ex) {
plugin.getLogger().severe(String.format(CONNECTION_ERROR, ex.getClass().getSimpleName(), ex.getMessage()));
return;
@ -153,7 +143,7 @@ public class UpdateCheckerTask implements Runnable {
}
try {
download(downloadLink, getPluginFile());
FileUtils.download(downloadLink, getPluginFile());
} catch (IOException ex) {
broadcastDownloadStatus(githubVersion, true);
throw new RuntimeException(DOWNLOAD_ERROR, ex);
@ -193,36 +183,4 @@ public class UpdateCheckerTask implements Runnable {
throw new RuntimeException("Error retrieving current plugin file", ex);
}
}
private void download(String urlString, File target) throws IOException {
try (InputStream is = stream(urlString);
ReadableByteChannel rbc = Channels.newChannel(is);
FileOutputStream fos = new FileOutputStream(target)) {
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
}
}
private String readAll(BufferedReader reader) throws IOException {
StringBuilder sb = new StringBuilder();
int cp;
while ((cp = reader.read()) != -1) {
sb.append((char) cp);
}
return sb.toString();
}
private JsonElement readJsonFromUrl(String url) throws IOException {
try (InputStream is = stream(url)) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
String jsonText = readAll(reader);
return new JsonParser().parse(jsonText);
}
}
private InputStream stream(String url) throws IOException {
URLConnection conn = new URL(url).openConnection();
conn.setRequestProperty("User-Agent", USER_AGENT);
conn.setConnectTimeout(10000);
return conn.getInputStream();
}
}

View file

@ -0,0 +1,79 @@
package net.frankheijden.serverutils.utils;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.charset.StandardCharsets;
public class FileUtils {
private static final String USER_AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:77.0)"
+ " Gecko/20100101"
+ " Firefox/77.0";
/**
* Downloads file from a URL to a file location.
* @param urlString The url to download.
* @param target The location to save the output to.
* @throws IOException If an I/O exception occurs.
*/
public static void download(String urlString, File target) throws IOException {
try (InputStream is = stream(urlString);
ReadableByteChannel rbc = Channels.newChannel(is);
FileOutputStream fos = new FileOutputStream(target)) {
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
}
}
/**
* Opens a stream to an url.
* @param url The url to stream.
* @return An InputStream of the url.
* @throws IOException If an I/O exception occurs.
*/
public static InputStream stream(String url) throws IOException {
URLConnection conn = new URL(url).openConnection();
conn.setRequestProperty("User-Agent", USER_AGENT);
conn.setConnectTimeout(10000);
return conn.getInputStream();
}
/**
* Reads all bytes from a reader and appends them to a string result.
* @param reader The reader to read from.
* @return All byte characters.
* @throws IOException If an I/O exception occurs.
*/
public static String readAll(BufferedReader reader) throws IOException {
StringBuilder sb = new StringBuilder();
int cp;
while ((cp = reader.read()) != -1) {
sb.append((char) cp);
}
return sb.toString();
}
/**
* Reads an url and converts it into a JsonElement.
* @param url The url to read from.
* @return The JsonElement.
* @throws IOException If an I/O exception occurs.
*/
public static JsonElement readJsonFromUrl(String url) throws IOException {
try (InputStream is = stream(url)) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
String jsonText = readAll(reader);
return new JsonParser().parse(jsonText);
}
}
}