Skip to content

Instantly share code, notes, and snippets.

@GMatrixGames
Created August 29, 2020 23:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GMatrixGames/af8d917310c904732eea3fdc8e9de992 to your computer and use it in GitHub Desktop.
Save GMatrixGames/af8d917310c904732eea3fdc8e9de992 to your computer and use it in GitHub Desktop.
My common Helper class used in a lot of my projects
package com.gmatrixgames.fn.discord.utils;
import kotlin.text.Charsets;
import org.apache.commons.io.IOUtils;
import org.apache.wink.json4j.JSONException;
import org.apache.wink.json4j.JSONObject;
import javax.net.ssl.HttpsURLConnection;
import java.awt.*;
import java.io.*;
import java.math.BigDecimal;
import java.net.URL;
import java.nio.channels.ReadableByteChannel;
import java.nio.charset.StandardCharsets;
import java.text.DecimalFormat;
import java.time.Instant;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Stream;
public class Helper {
public static String toStringUnixTime(Instant i) {
BigDecimal nanos = BigDecimal.valueOf(i.getNano(), 9);
BigDecimal seconds = BigDecimal.valueOf(i.getEpochSecond());
BigDecimal total = seconds.add(nanos);
DecimalFormat df = new DecimalFormat("#.#########");
return df.format(total);
}
public static Color hex2Rgb(String colorStr) {
return new Color(Integer.valueOf(colorStr.substring(1, 3), 16), Integer.valueOf(colorStr.substring(3, 5), 16), Integer.valueOf(colorStr.substring(5, 7), 16));
}
public static String center(String text, int len) {
String out = String.format("%" + len + "s%s%" + len + "s", "", text, "");
float mid = (out.length() / 2f);
float start = mid - (len / 2f);
float end = start + len;
return out.substring((int) start, (int) end);
}
public static String postToHastebin(String text, boolean raw) throws IOException {
byte[] postData = text.getBytes(StandardCharsets.UTF_8);
int postDataLength = postData.length;
String requestURL = "https://hasteb.in/documents";
URL url = new URL(requestURL);
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setInstanceFollowRedirects(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("User-Agent", "Hastebin Java Api");
conn.setRequestProperty("Content-Length", Integer.toString(postDataLength));
conn.setUseCaches(false);
String response = null;
DataOutputStream wr;
try {
wr = new DataOutputStream(conn.getOutputStream());
wr.write(postData);
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
response = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (response.contains("\"key\"")) {
response = response.substring(response.indexOf(":") + 2, response.length() - 2);
String postURL = raw ? "https://hasteb.in/raw/" : "https://hasteb.in/";
response = postURL + response;
}
return response;
}
public static String streamAsString(InputStream st) {
Scanner sc = new Scanner(st);
StringBuilder sb = new StringBuilder();
while (sc.hasNext()) {
sb.append(sc.nextLine());
}
return sb.toString();
}
public static String alphabeticalOrder(List<?> list, Boolean newline) {
Object[] array = new Object[list.size()];
StringBuilder sb = new StringBuilder();
for (int i = 0; i < list.size(); i++) {
array[i] = list.get(i);
}
array = Stream.of(array).sorted().toArray(Object[]::new);
for (Object s : array) {
if (newline) {
sb.append(s).append("\n");
} else {
sb.append(s);
}
}
return sb.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment