diff --git a/src/main/java/net/frankheijden/serverutils/tasks/UpdateCheckerTask.java b/src/main/java/net/frankheijden/serverutils/tasks/UpdateCheckerTask.java index dcf0e05..e9cf191 100644 --- a/src/main/java/net/frankheijden/serverutils/tasks/UpdateCheckerTask.java +++ b/src/main/java/net/frankheijden/serverutils/tasks/UpdateCheckerTask.java @@ -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(); - } } diff --git a/src/main/java/net/frankheijden/serverutils/utils/FileUtils.java b/src/main/java/net/frankheijden/serverutils/utils/FileUtils.java new file mode 100644 index 0000000..8ec2dfe --- /dev/null +++ b/src/main/java/net/frankheijden/serverutils/utils/FileUtils.java @@ -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); + } + } +}