shz.core.Help Maven / Gradle / Ivy
package shz.core;
import shz.core.constant.ArrayConstant;
import shz.core.io.IOHelp;
import shz.core.tag.LLTag;
import shz.core.structure.CharIndex;
import java.io.File;
import java.io.IOException;
import java.util.Comparator;
import java.util.function.Consumer;
public final class Help {
private Help() {
throw new IllegalStateException();
}
public static boolean isTrue(Object obj) {
if (obj == null) return false;
if (obj instanceof Boolean) return (boolean) obj;
if (obj instanceof CharSequence) {
String s = obj.toString();
return "1".equals(s) || "true".equalsIgnoreCase(s);
}
if (obj instanceof Number) return ((Number) obj).intValue() == 1;
if (obj instanceof Character) return ((char) obj) == '1';
return false;
}
public static boolean isFalse(Object obj) {
return !isTrue(obj);
}
public static Comparator emptyComparator() {
return (o1, o2) -> 0;
}
/**
* 标记类型
*/
public static byte[] tagType(String type, byte[] data) {
int typeLen, dataLen;
if ((typeLen = NullHelp.length(type)) == 0 || (dataLen = NullHelp.length(data)) == 0)
return ArrayConstant.EMPTY_BYTE_ARRAY;
byte[] bytes = new byte[2 + typeLen + dataLen];
bytes[0] = (byte) (typeLen >>> 8);
bytes[1] = (byte) ((typeLen & 255) - 128);
for (int i = 0; i < typeLen; ++i) bytes[i + 2] = (byte) CharIndex.C94.idx(type.charAt(i));
System.arraycopy(data, 0, bytes, 2 + typeLen, dataLen);
return bytes;
}
/**
* 解析类型
*/
public static LLTag analyzeType(byte[] bytes) {
int len;
if ((len = NullHelp.length(bytes)) < 4) return null;
int typeLen = (bytes[0] << 8) + bytes[1] + 128;
char[] chars = new char[typeLen];
for (int i = 0; i < typeLen; ++i) chars[i] = ArrayConstant.CHAR_ARRAY_94[bytes[i + 2]];
byte[] data = new byte[len - 2 - typeLen];
System.arraycopy(bytes, 2 + typeLen, data, 0, data.length);
return new LLTag<>(new String(chars), data);
}
public static void exec(String command, String[] envp, File file, Consumer onAccept) {
Process process;
try {
process = Runtime.getRuntime().exec(command, envp, file == null ? null : file.getParentFile());
} catch (IOException e) {
throw PRException.of(e);
}
if (onAccept != null) IOHelp.read(IOHelp.newBufferedReader(process.getInputStream()), onAccept);
}
public static void exec(String command, Consumer onAccept) {
exec(command, null, null, onAccept);
}
public static void exec(String command) {
exec(command, null, null, null);
}
}