后台服务
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DataUtil.java 8.3 KiB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. package com.iformall.utils;
  2. import org.apache.commons.lang3.ArrayUtils;
  3. import org.apache.commons.lang3.StringUtils;
  4. import java.io.File;
  5. import java.io.IOException;
  6. import java.net.MalformedURLException;
  7. import java.net.URL;
  8. import java.security.CodeSource;
  9. import java.security.ProtectionDomain;
  10. import java.util.Collection;
  11. import java.util.Map;
  12. /**
  13. * 常见的辅助类
  14. *
  15. * @author Stormeye
  16. * @since 2011-11-08
  17. */
  18. public final class DataUtil {
  19. private DataUtil() {
  20. }
  21. /**
  22. * 十进制字节数组转十六进制字符串
  23. *
  24. * @param b
  25. * @return
  26. */
  27. public static final String byte2hex(byte[] b) { // 一个字节数,转成16进制字符串
  28. StringBuilder hs = new StringBuilder(b.length * 2);
  29. String stmp = "";
  30. for (int n = 0; n < b.length; n++) {
  31. // 整数转成十六进制表示
  32. stmp = Integer.toHexString(b[n] & 0XFF);
  33. if (stmp.length() == 1)
  34. hs.append("0").append(stmp);
  35. else
  36. hs.append(stmp);
  37. }
  38. return hs.toString(); // 转成大写
  39. }
  40. /**
  41. * 十六进制字符串转十进制字节数组
  42. *
  43. * @param hs
  44. * @return
  45. */
  46. public static final byte[] hex2byte(String hs) {
  47. byte[] b = hs.getBytes();
  48. if ((b.length % 2) != 0)
  49. throw new IllegalArgumentException("长度不是偶数");
  50. byte[] b2 = new byte[b.length / 2];
  51. for (int n = 0; n < b.length; n += 2) {
  52. String item = new String(b, n, 2);
  53. // 两位一组,表示一个字节,把这样表示的16进制字符串,还原成一个十进制字节
  54. b2[n / 2] = (byte) Integer.parseInt(item, 16);
  55. }
  56. return b2;
  57. }
  58. /**
  59. * 这个方法可以通过与某个类的class文件的相对路径来获取文件或目录的绝对路径。 通常在程序中很难定位某个相对路径,特别是在B/S应用中。
  60. * 通过这个方法,我们可以根据我们程序自身的类文件的位置来定位某个相对路径。
  61. * 比如:某个txt文件相对于程序的Test类文件的路径是../../resource/test.txt,
  62. * 那么使用本方法Path.getFullPathRelateClass("../../resource/test.txt",Test.class)
  63. * 得到的结果是txt文件的在系统中的绝对路径。
  64. *
  65. * @param relatedPath 相对路径
  66. * @param cls 用来定位的类
  67. * @return 相对路径所对应的绝对路径
  68. * @throws IOException 因为本方法将查询文件系统,所以可能抛出IO异常
  69. */
  70. public static final String getFullPathRelateClass(String relatedPath, Class<?> cls) {
  71. String path = null;
  72. if (relatedPath == null) {
  73. throw new NullPointerException();
  74. }
  75. String clsPath = getPathFromClass(cls);
  76. File clsFile = new File(clsPath);
  77. String tempPath = clsFile.getParent() + File.separator + relatedPath;
  78. File file = new File(tempPath);
  79. try {
  80. path = file.getCanonicalPath();
  81. } catch (IOException e) {
  82. e.printStackTrace();
  83. }
  84. return path;
  85. }
  86. /**
  87. * 获取class文件所在绝对路径
  88. *
  89. * @param cls
  90. * @return
  91. * @throws IOException
  92. */
  93. public static final String getPathFromClass(Class<?> cls) {
  94. String path = null;
  95. if (cls == null) {
  96. throw new NullPointerException();
  97. }
  98. URL url = getClassLocationURL(cls);
  99. if (url != null) {
  100. path = url.getPath();
  101. if ("jar".equalsIgnoreCase(url.getProtocol())) {
  102. try {
  103. path = new URL(path).getPath();
  104. } catch (MalformedURLException e) {
  105. }
  106. int location = path.indexOf("!/");
  107. if (location != -1) {
  108. path = path.substring(0, location);
  109. }
  110. }
  111. File file = new File(path);
  112. try {
  113. path = file.getCanonicalPath();
  114. } catch (IOException e) {
  115. e.printStackTrace();
  116. }
  117. }
  118. return path;
  119. }
  120. /**
  121. * 判断对象是否Empty(null或元素为0)<br>
  122. * 实用于对如下对象做判断:String Collection及其子类 Map及其子类
  123. *
  124. * @param pObj 待检查对象
  125. * @return boolean 返回的布尔值
  126. */
  127. public static final boolean isEmpty(Object pObj) {
  128. if (pObj == null)
  129. return true;
  130. if (pObj == "")
  131. return true;
  132. if (pObj instanceof String) {
  133. if (((String) pObj).trim().length() == 0) {
  134. return true;
  135. }
  136. } else if (pObj instanceof Collection<?>) {
  137. if (((Collection<?>) pObj).size() == 0) {
  138. return true;
  139. }
  140. } else if (pObj instanceof Map<?, ?>) {
  141. if (((Map<?, ?>) pObj).size() == 0) {
  142. return true;
  143. }
  144. }
  145. return false;
  146. }
  147. /**
  148. * 判断对象是否为NotEmpty(!null或元素>0)<br>
  149. * 实用于对如下对象做判断:String Collection及其子类 Map及其子类
  150. *
  151. * @param pObj 待检查对象
  152. * @return boolean 返回的布尔值
  153. */
  154. public static final boolean isNotEmpty(Object pObj) {
  155. if (pObj == null)
  156. return false;
  157. if (pObj == "")
  158. return false;
  159. if (pObj instanceof String) {
  160. if (((String) pObj).trim().length() == 0) {
  161. return false;
  162. }
  163. } else if (pObj instanceof Collection<?>) {
  164. if (((Collection<?>) pObj).size() == 0) {
  165. return false;
  166. }
  167. } else if (pObj instanceof Map<?, ?>) {
  168. if (((Map<?, ?>) pObj).size() == 0) {
  169. return false;
  170. }
  171. }
  172. return true;
  173. }
  174. /**
  175. * JS输出含有\n的特殊处理
  176. *
  177. * @param pStr
  178. * @return
  179. */
  180. public static final String replace4JsOutput(String pStr) {
  181. pStr = pStr.replace("\r\n", "<br/>&nbsp;&nbsp;");
  182. pStr = pStr.replace("\t", "&nbsp;&nbsp;&nbsp;&nbsp;");
  183. pStr = pStr.replace(" ", "&nbsp;");
  184. return pStr;
  185. }
  186. /**
  187. * 分别去空格
  188. *
  189. * @param paramArray
  190. * @return
  191. */
  192. public static final String[] trim(String[] paramArray) {
  193. if (ArrayUtils.isEmpty(paramArray)) {
  194. return paramArray;
  195. }
  196. String[] resultArray = new String[paramArray.length];
  197. for (int i = 0; i < paramArray.length; i++) {
  198. String param = paramArray[i];
  199. resultArray[i] = StringUtils.trim(param);
  200. }
  201. return resultArray;
  202. }
  203. /**
  204. * 获取类的class文件位置的URL
  205. *
  206. * @param cls
  207. * @return
  208. */
  209. private static URL getClassLocationURL(final Class<?> cls) {
  210. if (cls == null)
  211. throw new IllegalArgumentException("null input: cls");
  212. URL result = null;
  213. final String clsAsResource = cls.getName().replace('.', '/').concat(".class");
  214. final ProtectionDomain pd = cls.getProtectionDomain();
  215. if (pd != null) {
  216. final CodeSource cs = pd.getCodeSource();
  217. if (cs != null)
  218. result = cs.getLocation();
  219. if (result != null) {
  220. if ("file".equals(result.getProtocol())) {
  221. try {
  222. if (result.toExternalForm().endsWith(".jar") || result.toExternalForm().endsWith(".zip"))
  223. result = new URL("jar:".concat(result.toExternalForm()).concat("!/").concat(clsAsResource));
  224. else if (new File(result.getFile()).isDirectory())
  225. result = new URL(result, clsAsResource);
  226. } catch (MalformedURLException ignore) {
  227. }
  228. }
  229. }
  230. }
  231. if (result == null) {
  232. final ClassLoader clsLoader = cls.getClassLoader();
  233. result = clsLoader != null ? clsLoader.getResource(clsAsResource)
  234. : ClassLoader.getSystemResource(clsAsResource);
  235. }
  236. return result;
  237. }
  238. /** 初始化设置默认值 */
  239. public static final <K> K ifNull(K k, K defaultValue) {
  240. if (k == null) {
  241. return defaultValue;
  242. }
  243. return k;
  244. }
  245. public static String unicodeToUtf8(String theString) {
  246. char aChar;
  247. int len = theString.length();
  248. StringBuffer outBuffer = new StringBuffer(len);
  249. for (int x = 0; x < len;) {
  250. aChar = theString.charAt(x++);
  251. if (aChar == '\\') {
  252. aChar = theString.charAt(x++);
  253. if (aChar == 'u') {
  254. // Read the xxxx
  255. int value = 0;
  256. for (int i = 0; i < 4; i++) {
  257. aChar = theString.charAt(x++);
  258. switch (aChar) {
  259. case '0':
  260. case '1':
  261. case '2':
  262. case '3':
  263. case '4':
  264. case '5':
  265. case '6':
  266. case '7':
  267. case '8':
  268. case '9':
  269. value = (value << 4) + aChar - '0';
  270. break;
  271. case 'a':
  272. case 'b':
  273. case 'c':
  274. case 'd':
  275. case 'e':
  276. case 'f':
  277. value = (value << 4) + 10 + aChar - 'a';
  278. break;
  279. case 'A':
  280. case 'B':
  281. case 'C':
  282. case 'D':
  283. case 'E':
  284. case 'F':
  285. value = (value << 4) + 10 + aChar - 'A';
  286. break;
  287. default:
  288. throw new IllegalArgumentException(
  289. "Malformed \\uxxxx encoding.");
  290. }
  291. }
  292. outBuffer.append((char) value);
  293. } else {
  294. if (aChar == 't')
  295. aChar = '\t';
  296. else if (aChar == 'r')
  297. aChar = '\r';
  298. else if (aChar == 'n')
  299. aChar = '\n';
  300. else if (aChar == 'f')
  301. aChar = '\f';
  302. outBuffer.append(aChar);
  303. }
  304. } else
  305. outBuffer.append(aChar);
  306. }
  307. return outBuffer.toString();
  308. }
  309. }