diff options
author | Wyvest <45589059+Wyvest@users.noreply.github.com> | 2022-05-25 21:21:42 +0700 |
---|---|---|
committer | Wyvest <45589059+Wyvest@users.noreply.github.com> | 2022-05-25 21:21:42 +0700 |
commit | 60b9f99d260361824424b3f3c866e95f4869c262 (patch) | |
tree | d9fe06f8af0839fd7b5a9538d776a40be70719fc /src/main/java/cc/polyfrost/oneconfig/utils/InternetUtils.java | |
parent | 368ba3ba0859e00ec245a64c94c1eab1f96d23bd (diff) | |
download | OneConfig-60b9f99d260361824424b3f3c866e95f4869c262.tar.gz OneConfig-60b9f99d260361824424b3f3c866e95f4869c262.tar.bz2 OneConfig-60b9f99d260361824424b3f3c866e95f4869c262.zip |
OC-33 OC-35, additions to ColorUtils, and JsonUtils
Diffstat (limited to 'src/main/java/cc/polyfrost/oneconfig/utils/InternetUtils.java')
-rw-r--r-- | src/main/java/cc/polyfrost/oneconfig/utils/InternetUtils.java | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/src/main/java/cc/polyfrost/oneconfig/utils/InternetUtils.java b/src/main/java/cc/polyfrost/oneconfig/utils/InternetUtils.java index aa13c96..db83825 100644 --- a/src/main/java/cc/polyfrost/oneconfig/utils/InternetUtils.java +++ b/src/main/java/cc/polyfrost/oneconfig/utils/InternetUtils.java @@ -1,4 +1,86 @@ package cc.polyfrost.oneconfig.utils; +import com.google.gson.JsonElement; +import org.apache.commons.io.IOUtils; + +import java.io.*; +import java.net.HttpURLConnection; +import java.net.URL; +import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; + public class InternetUtils { + private static InputStream setupConnection(String url, String userAgent, int timeout, boolean useCaches) throws IOException { + HttpURLConnection connection = ((HttpURLConnection) new URL(url).openConnection()); + connection.setRequestMethod("GET"); + connection.setUseCaches(useCaches); + connection.addRequestProperty("User-Agent", userAgent); + connection.setReadTimeout(timeout); + connection.setConnectTimeout(timeout); + connection.setDoOutput(true); + return connection.getInputStream(); + } + + public static String getString(String url, String userAgent, int timeout, boolean useCaches) { + try (InputStreamReader input = new InputStreamReader(setupConnection(url, userAgent, timeout, useCaches), StandardCharsets.UTF_8)) { + return IOUtils.toString(input); + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + + public static String getString(String url) { + return getString(url, "OneConfig/1.0.0", 5000, false); + } + + public static JsonElement getJsonElement(String url, String userAgent, int timeout, boolean useCaches) { + return JsonUtils.parseString(getString(url, userAgent, timeout, useCaches)); + } + + public static JsonElement getJsonElement(String url) { + return getJsonElement(url, "OneConfig/1.0.0", 5000, false); + } + + + public static boolean downloadFile(String url, File file) { + return downloadFile(url, file, "OneConfig/1.0.0", 5000, false); + } + + public static boolean downloadFile(String url, File file, String userAgent, int timeout, boolean useCaches) { + url = url.replace(" ", "%20"); + try (FileOutputStream fileOut = new FileOutputStream(file); BufferedInputStream in = new BufferedInputStream(setupConnection(url, userAgent, timeout, useCaches))) { + IOUtils.copy(in, fileOut); + } catch (Exception e) { + e.printStackTrace(); + return false; + } + return true; + } + + public static String getFileChecksum(String filename) { + try (FileInputStream inputStream = new FileInputStream(filename)) { + MessageDigest digest = MessageDigest.getInstance("SHA-256"); + byte[] bytesBuffer = new byte[1024]; + int bytesRead; + + while ((bytesRead = inputStream.read(bytesBuffer)) != -1) { + digest.update(bytesBuffer, 0, bytesRead); + } + + return convertByteArrayToHexString(digest.digest()); + } catch (Exception e) { + e.printStackTrace(); + } + return ""; + } + + private static String convertByteArrayToHexString(byte[] arrayBytes) { + StringBuilder stringBuffer = new StringBuilder(); + for (byte arrayByte : arrayBytes) { + stringBuffer.append(Integer.toString((arrayByte & 0xff) + 0x100, 16) + .substring(1)); + } + return stringBuffer.toString(); + } } |