houzhongjian
2024-11-06 7412dd652c0ac48c5a17b5d9b61d5d2a0f686137
提交 | 用户 | 时间
149dd0 1 package com.iailab.netsdk.lib;
H 2
3 import com.sun.jna.IntegerType;
4 import com.sun.jna.Native;
5 import com.sun.jna.Platform;
6 import com.sun.jna.Structure;
7
8 import java.lang.reflect.Field;
9 import java.lang.reflect.Modifier;
10 import java.text.SimpleDateFormat;
11 import java.util.ArrayList;
12 import java.util.List;
13
14 public class Utils {
15     public Utils() {
16
17     }
18
19     /**
20      * 获取系统对应的编码
21      *
22      */
23     public static String getPlatformEncode() {
24         String encode = "";
25         String osPrefix = getOsPrefix();
26         if (osPrefix.toLowerCase().startsWith("win32")) {
27             encode = "GBK";
28         } else if (osPrefix.toLowerCase().startsWith("linux")) {
29             encode = "UTF-8";
30         }else if(osPrefix.toLowerCase().startsWith("mac")){
31             encode="UTF-8";
32         }
33         if(encode.isEmpty()){
34             encode="UTF-8";
35         }
36         return encode;
37     }
38
39     // 获取操作平台信息
40     public static String getOsPrefix() {
41         String arch = System.getProperty("os.arch").toLowerCase();
42         final String name = System.getProperty("os.name");
43         String osPrefix;
44         switch (Platform.getOSType()) {
45             case Platform.WINDOWS: {
46                 if ("i386".equals(arch))
47                     arch = "x86";
48                 osPrefix = "win32-" + arch;
49             }
50             break;
51             case Platform.LINUX: {
52                 if ("x86".equals(arch)) {
53                     arch = "i386";
54                 } else if ("x86_64".equals(arch)) {
55                     arch = "amd64";
56                 }
57                 osPrefix = "linux-" + arch;
58             }
59             break;
60             case Platform.MAC: {
61                 //mac系统的os.arch都是ppc(老版本的mac是powerpc,已经基本不用)看不出系统位数,使用下面的参数表示
62                 arch = System.getProperty("sun.arch.data.model");
63                 osPrefix = "mac-" + arch;
64             }
65             break;
66             default: {
67                 osPrefix = name.toLowerCase();
68                 if ("x86".equals(arch)) {
69                     arch = "i386";
70                 }
71                 if ("x86_64".equals(arch)) {
72                     arch = "amd64";
73                 }
74                 int space = osPrefix.indexOf(" ");
75                 if (space != -1) {
76                     osPrefix = osPrefix.substring(0, space);
77                 }
78                 osPrefix += "-" + arch;
79             }
80             break;
81
82         }
83
84         return osPrefix;
85     }
86
87     public static String getOsName() {
88         String osName = "";
89         String osPrefix = getOsPrefix();
90         if (osPrefix.toLowerCase().startsWith("win32-x86")
91                 || osPrefix.toLowerCase().startsWith("win32-amd64")) {
92             osName = "win";
93         } else if (osPrefix.toLowerCase().startsWith("linux-i386")
94                 || osPrefix.toLowerCase().startsWith("linux-amd64")) {
95             osName = "linux";
96         } else if (osPrefix.toLowerCase().startsWith("mac-64")
97                 || osPrefix.toLowerCase().startsWith("mac-32")) {
98             osName = "mac";
99         }
100
101         return osName;
102     }
103
104     // 获取加载库
105     public static String getLoadLibrary(String library) {
106         if (isChecking()) {
107             return null;
108         }
109
110         String loadLibrary = "";
111         String osPrefix = getOsPrefix();
112         if (osPrefix.toLowerCase().startsWith("win32-x86")) {
113             loadLibrary = "./libs/win32/";
114         } else if (osPrefix.toLowerCase().startsWith("win32-amd64")) {
115             loadLibrary = "./libs/win64/";
116         } else if (osPrefix.toLowerCase().startsWith("linux-i386")) {
117             loadLibrary = "";
118         } else if (osPrefix.toLowerCase().startsWith("linux-amd64")) {
119             loadLibrary = "";
120         } else if (osPrefix.toLowerCase().startsWith("mac-64")) {
121             loadLibrary = "";
122         }
123 //        else if(osPrefix.toLowerCase().startsWith("mac-32")) {
124 //            loadLibrary = "";
125 //        }  32位mac版本基本没人用了,暂时不用
126
127         System.out.printf("加载库[%s]\n", loadLibrary + library);
128         return loadLibrary + library;
129     }
130
131     private static boolean checking = false;
132
133     public static void setChecking() {
134         checking = true;
135     }
136
137     public static void clearChecking() {
138         checking = false;
139     }
140
141     public static boolean isChecking() {
142         return checking;
143     }
144
145     public static class LLong extends IntegerType {
146         private static final long serialVersionUID = 1L;
147
148         /**
149          * Size of a native long, in bytes.
150          */
151         public static int size;
152
153         static {
154             size = Native.LONG_SIZE;
155             if (Utils.getOsPrefix().toLowerCase().equals("linux-amd64")
156                     || Utils.getOsPrefix().toLowerCase().equals("win32-amd64")
157                     || Utils.getOsPrefix().toLowerCase().equals("mac-64")) {
158                 size = 8;
159             } else if (Utils.getOsPrefix().toLowerCase().equals("linux-i386")
160                     || Utils.getOsPrefix().toLowerCase().equals("win32-x86")) {
161                 size = 4;
162             }
163         }
164
165         /**
166          * Create a zero-valued LLong.
167          */
168         public LLong() {
169             this(0);
170         }
171
172         /**
173          * Create a LLong with the given value.
174          */
175         public LLong(long value) {
176             super(size, value);
177         }
178     }
179
180     public static class SdkStructure extends Structure {
181         @Override
182         protected List<String> getFieldOrder() {
183             List<String> fieldOrderList = new ArrayList<String>();
184             for (Class<?> cls = getClass();
185                  !cls.equals(NetSDKLib.SdkStructure.class);
186                  cls = cls.getSuperclass()) {
187                 Field[] fields = cls.getDeclaredFields();
188                 int modifiers;
189                 for (Field field : fields) {
190                     modifiers = field.getModifiers();
191                     if (Modifier.isStatic(modifiers) || !Modifier.isPublic(modifiers)) {
192                         continue;
193                     }
194                     fieldOrderList.add(field.getName());
195                 }
196             }
197             //            System.out.println(fieldOrderList);
198
199             return fieldOrderList;
200         }
201     }
202
203     // 获取当前时间
204     public static String getDate() {
205         SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
206         return simpleDate.format(new java.util.Date())
207                 .replace(" ", "_").replace(":", "-");
208     }
209
210 }