Skip to content

Commit cb60a20

Browse files
committed
Initial commit
0 parents  commit cb60a20

34 files changed

+1583
-0
lines changed
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package com.jgcomptech.tools;
2+
3+
import com.jgcomptech.tools.osinfo.CheckIf;
4+
import com.sun.jna.platform.win32.WinDef;
5+
import org.jetbrains.annotations.Nullable;
6+
7+
import java.io.*;
8+
import java.util.*;
9+
10+
public class CommandInfo {
11+
/** Output object that is returned after the command has completed */
12+
public static class Output {
13+
/** Returns the text result of the command. */
14+
public final ArrayList<String> Result = new ArrayList<>();
15+
16+
/** Returns the exit code. Returns 0 if no error occurred. */
17+
public int ExitCode = 0;
18+
19+
public void print() {
20+
for(String line : this.Result) {
21+
System.out.println(line);
22+
}
23+
}
24+
}
25+
26+
/**
27+
* Runs command and returns results to ArrayList in Output object.
28+
* @param command Command to run.
29+
* @param args Arguments to pass to command.
30+
* @return Output object
31+
*/
32+
public static Output Run(String command, String args)
33+
{
34+
return Run(command, args, false, true, false);
35+
}
36+
37+
/**
38+
* Runs command elevated, shows cmd window and pauses window when command is complete.
39+
* If "elevate" parameter is false, it is ignored and and results will be saved to Output object.
40+
* If OS is not Windows, "elevate" parameter is ignored and results will be saved to Output object.
41+
* @param command Command to run.
42+
* @param args Arguments to pass to command.
43+
* @param elevate Boolean to set if command should be run elevated. If true Output object will be empty.
44+
* @return Output object
45+
*/
46+
public static Output Run(String command, String args, boolean elevate)
47+
{
48+
if(elevate) {
49+
return Run(command, args, true, false, true);
50+
}
51+
return Run(command, args);
52+
}
53+
54+
/**
55+
* Runs command according to parameters. Will only open cmd window if OS is Windows.
56+
* If OS is not Windows, all boolean parameters are ignored and results will be saved to Output object.
57+
* @param command Command to run.
58+
* @param args Arguments to pass to command.
59+
* @param elevate Boolean to set if command should be run elevated.
60+
* If true Output object will be empty.
61+
* @param hideWindow If true, cmd window will be hidden.
62+
* If true, and elevate is false, results will be saved to Output object.
63+
* @param keepWindowOpen If true, pauses cmd window and forces it to stay open after command is completed.
64+
* If false and "elevate" is true, cmd window will close after command is completed.
65+
*
66+
* This parameter is ignored if "hidewindow" is true.
67+
* This prevents cmd window from staying open when hidden and unnecessarily using RAM.
68+
* @return Output object
69+
*/
70+
@Nullable
71+
public static Output Run(String command, String args, boolean elevate, boolean hideWindow, boolean keepWindowOpen) {
72+
Output newOutput = new Output();
73+
74+
if((elevate || !hideWindow) && CheckIf.isWindows()) {
75+
ShellExecute(command, args, elevate, hideWindow, keepWindowOpen );
76+
}
77+
else {
78+
final Process process;
79+
try {
80+
if(CheckIf.isWindows()) {
81+
process = Runtime.getRuntime().exec("cmd /C \"" + command + " " + args + "\"");
82+
} else {
83+
process = Runtime.getRuntime().exec(command);
84+
}
85+
86+
assert process != null;
87+
final BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
88+
89+
String line;
90+
while((line = br.readLine()) != null) {
91+
newOutput.Result.add(line);
92+
}
93+
94+
process.waitFor();
95+
96+
newOutput.ExitCode = process.exitValue();
97+
} catch(IOException | InterruptedException e) {
98+
e.printStackTrace();
99+
}
100+
}
101+
return newOutput;
102+
}
103+
104+
private static void ShellExecute(String command, String args, boolean elevate, boolean hideWindow, boolean keepWindowOpen) {
105+
FileWriter writer;
106+
try {
107+
writer = new FileWriter("my.bat");
108+
writer.write("@Echo off" + System.getProperty("line.separator"));
109+
writer.write("\"" + command + "\" " + args + System.getProperty("line.separator"));
110+
if(keepWindowOpen && !hideWindow) { writer.write("pause"); }
111+
writer.close();
112+
} catch(IOException e) {
113+
e.printStackTrace();
114+
}
115+
116+
int WindowStatus = hideWindow ? 0 : 1;
117+
String operation = elevate ? "runas" : "open";
118+
119+
WinDef.HWND hw = null;
120+
String filename = "my.bat";
121+
NativeMethods.Shell32.INSTANCE.ShellExecute(hw, operation, filename, null, null, WindowStatus);
122+
try {
123+
Thread.sleep(2000);
124+
} catch(InterruptedException e) {
125+
e.printStackTrace();
126+
}
127+
File file = new File("my.bat");
128+
file.delete();
129+
}
130+
}

src/com/jgcomptech/tools/Misc.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.jgcomptech.tools;
2+
3+
import java.text.DecimalFormat;
4+
5+
public class Misc {
6+
/** Returns the conversion from bytes to the correct version. Ex. 1024 bytes = 1 KB */
7+
public static String ConvertBytes(Double input) {
8+
DecimalFormat df = new DecimalFormat("#.##");
9+
if (input >= 1024)
10+
{
11+
input = input / 1024;
12+
if (input >= 1024)
13+
{
14+
input = input / 1024;
15+
if (input >= 1024)
16+
{
17+
input = input / 1024;
18+
if (input >= 1024)
19+
{
20+
input = input / 1024;
21+
return df.format(input) + " TB";
22+
}
23+
return df.format(input) + " GB";
24+
}
25+
return df.format(input) + " MB";
26+
}
27+
return df.format(input) + " KB";
28+
}
29+
return df.format(input) + " Bytes";
30+
}
31+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.jgcomptech.tools;
2+
3+
import com.sun.jna.Native;
4+
import com.sun.jna.WString;
5+
import com.sun.jna.platform.win32.WinDef;
6+
import com.sun.jna.platform.win32.WinNT;
7+
import com.sun.jna.ptr.IntByReference;
8+
import com.sun.jna.win32.W32APIOptions;
9+
10+
public class NativeMethods {
11+
public interface Kernel32 extends com.sun.jna.platform.win32.Kernel32 {
12+
Kernel32 INSTANCE = (Kernel32)
13+
Native.loadLibrary("kernel32", Kernel32.class, W32APIOptions.DEFAULT_OPTIONS);
14+
15+
boolean GetProductInfo(
16+
int dwOSMajorVersion,
17+
int dwOSMinorVersion,
18+
int dwSpMajorVersion,
19+
int dwSpMinorVersion,
20+
IntByReference pdwReturnedProductType);
21+
22+
boolean GetSystemMetrics(int nIndex);
23+
}
24+
25+
public interface Shell32 extends com.sun.jna.platform.win32.Shell32 {
26+
Shell32 INSTANCE = (Shell32)
27+
Native.loadLibrary("shell32", Shell32.class, W32APIOptions.DEFAULT_OPTIONS);
28+
29+
WinDef.HINSTANCE ShellExecuteW(WinDef.HWND hwnd,
30+
String lpOperation,
31+
WString lpFile,
32+
String lpParameters,
33+
String lpDirectory,
34+
int nShowCmd);
35+
}
36+
37+
public interface Secur32 extends com.sun.jna.platform.win32.Secur32 {
38+
Secur32 INSTANCE = (Secur32)
39+
Native.loadLibrary("secur32", Secur32.class, W32APIOptions.DEFAULT_OPTIONS);
40+
}
41+
42+
public static boolean getVersionInfoFailed(WinNT.OSVERSIONINFOEX osVersionInfo) {
43+
return !Kernel32.INSTANCE.GetVersionEx(osVersionInfo);
44+
}
45+
46+
public static boolean getSystemMetrics(int nIndex) { return Kernel32.INSTANCE.GetSystemMetrics(nIndex); }
47+
48+
public static int getProductInfo(int Major, int Minor) {
49+
IntByReference strProductType = new IntByReference();
50+
Kernel32.INSTANCE.GetProductInfo(Major, Minor, 0, 0, strProductType);
51+
return strProductType.getValue();
52+
}
53+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.jgcomptech.tools;
2+
3+
public @interface NotImplemented {
4+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.jgcomptech.tools;
2+
3+
import com.sun.jna.platform.win32.Advapi32Util;
4+
import com.sun.jna.platform.win32.WinReg;
5+
import org.jetbrains.annotations.NotNull;
6+
7+
public class RegistryInfo {
8+
public enum HKEY
9+
{
10+
CLASSES_ROOT,
11+
CURRENT_USER,
12+
LOCAL_MACHINE,
13+
USERS,
14+
PERFORMANCE_DATA,
15+
CURRENT_CONFIG
16+
}
17+
18+
@NotNull
19+
public static String getStringValue(HKEY hkey, String key, String value) {
20+
WinReg.HKEY keyobj = null;
21+
22+
switch (hkey)
23+
{
24+
case CLASSES_ROOT:
25+
keyobj = WinReg.HKEY_CLASSES_ROOT;
26+
break;
27+
case CURRENT_USER:
28+
keyobj = WinReg.HKEY_CURRENT_USER;
29+
break;
30+
case LOCAL_MACHINE:
31+
keyobj = WinReg.HKEY_LOCAL_MACHINE;
32+
break;
33+
case USERS:
34+
keyobj = WinReg.HKEY_USERS;
35+
break;
36+
case PERFORMANCE_DATA:
37+
keyobj = WinReg.HKEY_PERFORMANCE_DATA;
38+
break;
39+
case CURRENT_CONFIG:
40+
keyobj = WinReg.HKEY_CURRENT_CONFIG;
41+
break;
42+
}
43+
44+
if(Advapi32Util.registryValueExists(keyobj, key, value))
45+
{
46+
return Advapi32Util.registryGetStringValue(keyobj, key, value);
47+
}
48+
return "";
49+
}
50+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.jgcomptech.tools.hwinfo;
2+
3+
import com.jgcomptech.tools.RegistryInfo;
4+
import com.jgcomptech.tools.osinfo.CheckIf;
5+
import org.jetbrains.annotations.NotNull;
6+
7+
/** Returns information about the system BIOS. */
8+
public class BIOS {
9+
/** Returns the system BIOS release date stored in the registry. */
10+
@NotNull
11+
public static String getReleaseDate() {
12+
if(CheckIf.isWindows()) {
13+
String key = "HARDWARE\\DESCRIPTION\\System\\BIOS";
14+
String value = "BIOSReleaseDate";
15+
return RegistryInfo.getStringValue(RegistryInfo.HKEY.LOCAL_MACHINE, key, value);
16+
}
17+
return "Unknown";
18+
}
19+
20+
/** Returns the system BIOS version stored in the registry. */
21+
@NotNull
22+
public static String getVersion() {
23+
if(CheckIf.isWindows()) {
24+
String key = "HARDWARE\\DESCRIPTION\\System\\BIOS";
25+
String value = "BIOSVersion";
26+
return RegistryInfo.getStringValue(RegistryInfo.HKEY.LOCAL_MACHINE, key, value);
27+
}
28+
return "Unknown";
29+
}
30+
31+
/** Returns the system BIOS vendor name stored in the registry. */
32+
@NotNull
33+
public static String getVendor() {
34+
if(CheckIf.isWindows()) {
35+
String key = "HARDWARE\\DESCRIPTION\\System\\BIOS";
36+
String value = "BIOSVendor";
37+
return RegistryInfo.getStringValue(RegistryInfo.HKEY.LOCAL_MACHINE, key, value);
38+
}
39+
return "Unknown";
40+
}
41+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.jgcomptech.tools.hwinfo;
2+
3+
import java.io.BufferedReader;
4+
import java.io.InputStreamReader;
5+
import java.net.InetAddress;
6+
import java.net.URL;
7+
8+
/**
9+
* Network Information
10+
*/
11+
public class Network {
12+
/**
13+
* Returns the Internal IP Address.
14+
*/
15+
public static String getInternalIPAddress() {
16+
try
17+
{
18+
String ip = (InetAddress.getLocalHost().getHostAddress()).trim();
19+
if(ip.equals("127.0.0.1")) return "N/A";
20+
return ip;
21+
}
22+
catch(Exception ex) { return "ERROR"; }
23+
}
24+
25+
/**
26+
* Returns the External IP Address by connecting to "http://api.ipify.org".
27+
*
28+
*/
29+
public static String getExternalIPAddress() {
30+
try {
31+
URL url = new URL("http://api.ipify.org");
32+
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
33+
return (in.readLine()).trim();
34+
}
35+
catch (Exception ex) {
36+
if (ex.toString().contains("java.net.UnknownHostException:")) { return "N/A"; }
37+
return ex.toString();
38+
}
39+
}
40+
41+
/**
42+
* Returns status of internet connection
43+
*/
44+
public static boolean isConnectedToInternet() { return !getExternalIPAddress().equals("N/A"); }
45+
}

0 commit comments

Comments
 (0)