Begin network rewrite

This commit is contained in:
Banks Troutman
2019-10-04 15:55:45 -04:00
parent 2d32fa21ce
commit 7699766091
20 changed files with 338 additions and 83 deletions

View File

@@ -0,0 +1,25 @@
package com.chameleonvision.util;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
public class FileHelper {
private FileHelper() {} // no construction, utility class
public static void CheckPath(String path) {
if (path.equals("")) return;
Path realPath = Path.of(path);
CheckPath(realPath);
}
public static void CheckPath(Path path) {
if (!Files.exists(path)) {
try {
Files.createDirectories(path);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

View File

@@ -0,0 +1,46 @@
package com.chameleonvision.util;
public class MemoryManager {
private static final long MEGABYTE_FACTOR = 1024L * 1024L;
private int collectionThreshold;
private int lastUsedMb = 0;
public MemoryManager(int collectionThreshold) {
this.collectionThreshold = collectionThreshold;
}
public void setCollectionThreshold(int collectionThreshold) {
this.collectionThreshold = collectionThreshold;
}
public static long getUsedMemory() {
return Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
}
public static int getUsedMemoryMB() {
return (int) (getUsedMemory() / MEGABYTE_FACTOR);
}
private static void collect() {
System.gc();
System.runFinalization();
}
public void run() { run(false); }
public void run(boolean print) {
var usedMem = getUsedMemoryMB();
if (usedMem != lastUsedMb) {
lastUsedMb = usedMem;
if (print) System.out.printf("Memory usage: %dMB\n", usedMem);
}
if (usedMem >= collectionThreshold) {
collect();
if (print) System.out.printf("Garbage collected at %dMB\n", usedMem);
}
}
}

View File

@@ -1,5 +1,8 @@
package com.chameleonvision.util;
import java.util.ArrayList;
import java.util.List;
public class Utilities {
private Utilities() {}
@@ -8,4 +11,30 @@ public class Utilities {
return ip.matches(PATTERN);
}
public static List<Byte> getDigitBytes(int num) {
List<Byte> digits = new ArrayList<>();
collectDigitBytes(num, digits);
return digits;
}
private static void collectDigitBytes(int num, List<Byte> digits) {
if (num / 10 > 0) {
collectDigitBytes( num / 10, digits);
}
digits.add((byte) (num % 10));
}
public static List<Integer> getDigits(int num) {
List<Integer> digits = new ArrayList<Integer>();
collectDigits(num, digits);
return digits;
}
private static void collectDigits(int num, List<Integer> digits) {
if(num / 10 > 0) {
collectDigits(num / 10, digits);
}
digits.add(num % 10);
}
}