summaryrefslogtreecommitdiff
path: root/src/misc/Utils.java
diff options
context:
space:
mode:
authorLeonard Kugis <leonard@kug.is>2022-04-25 18:36:30 +0200
committerLeonard Kugis <leonard@kug.is>2022-04-25 18:36:30 +0200
commit84e220b332bfffb0f2dcc39b9697a6fd6691d265 (patch)
tree0b861a9a099017ffd9bf00bae12e33ed6a309bbe /src/misc/Utils.java
Initial commitHEADmaster
Diffstat (limited to 'src/misc/Utils.java')
-rwxr-xr-xsrc/misc/Utils.java73
1 files changed, 73 insertions, 0 deletions
diff --git a/src/misc/Utils.java b/src/misc/Utils.java
new file mode 100755
index 0000000..a3d5b0a
--- /dev/null
+++ b/src/misc/Utils.java
@@ -0,0 +1,73 @@
+package misc;
+
+
+import java.io.UnsupportedEncodingException;
+
+public class Utils {
+
+ public static int randomInt(int from, int to) {
+ return (int) (from + Math.round(Math.random() * to));
+ }
+
+ public static String randomStringAN(int length) {
+ String output = "";
+ char[] table = new char[] {
+ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
+ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
+ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
+ };
+ for(int i = 0; i<length; i++)
+ output += table[randomInt(0, table.length-1)];
+ return output;
+ }
+
+ public static String randomStringUTF8(int length) {
+ byte[] b = new byte[length];
+ while(true) {
+ for(int i = 0; i<length; i++)
+ b[i] = (byte) Utils.randomInt(0, 255);
+ try {
+ return new String(b, "UTF-8");
+ } catch (UnsupportedEncodingException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+
+ public static int byteArrayToInt(Byte[] b)
+ {
+ return b[3] & 0xFF |
+ (b[2] & 0xFF) << 8 |
+ (b[1] & 0xFF) << 16 |
+ (b[0] & 0xFF) << 24;
+ }
+
+ public static Byte[] intToByteArray(int a)
+ {
+ return new Byte[] {
+ (byte) ((a >> 24) & 0xFF),
+ (byte) ((a >> 16) & 0xFF),
+ (byte) ((a >> 8) & 0xFF),
+ (byte) (a & 0xFF)
+ };
+ }
+
+ public static int byteArrayToInt(byte[] b)
+ {
+ return b[3] & 0xFF |
+ (b[2] & 0xFF) << 8 |
+ (b[1] & 0xFF) << 16 |
+ (b[0] & 0xFF) << 24;
+ }
+
+ public static byte[] intTobyteArray(int a)
+ {
+ return new byte[] {
+ (byte) ((a >> 24) & 0xFF),
+ (byte) ((a >> 16) & 0xFF),
+ (byte) ((a >> 8) & 0xFF),
+ (byte) (a & 0xFF)
+ };
+ }
+
+}